微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何打开文件对话框而不是指向文件

如何解决如何打开文件对话框而不是指向文件

这里是Word中可以生成条码的marco,但是我记录marco时我选择了一个文件(t1.csv),我如何让用户选择一个文件? 我尝试了一些关于打开文件对话框的 vb 代码,但对我不起作用

Sub To_Bar_Code()
'
' To_Bar_Code Macro
'
'
    ActiveDocument.MailMerge.MainDocumentType = wdFormletters
    ActiveDocument.MailMerge.OpenDataSource Name:= _
        "C:\Users\jiqi9\Desktop\BC\BarCode\t1.csv",ConfirmConversions:=False,_
        ReadOnly:=False,LinkToSource:=True,AddToRecentFiles:=False,_
        PasswordDocument:="",PasswordTemplate:="",WritePasswordDocument:="",_
        WritePasswordTemplate:="",Revert:=False,Format:=wdOpenFormatAuto,_
        Connection:="",sqlStatement:="",sqlStatement1:="",SubType:= _
        wdMergeSubTypeOther
    With ActiveDocument.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = wdDefaultFirstRecord
            .LastRecord = wdDefaultLastRecord
        End With
        .Execute Pause:=False
    End With
    Windows("LabelFinal").Activate
End Sub

解决方法

例如:

Sub To_Bar_Code()
Dim MMSrc As String
Application.DisplayAlerts = wdAlertsNone
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  .Title = "Select the mailmerge source file"
  .Filters.Add "CSV Files","*.csv"
  .AllowMultiSelect = False
  If .Show = -1 Then
    MMSrc = .SelectedItems(1)
  Else
    MsgBox "No source file selected. Exiting",vbExclamation
    Exit Sub
  End If
End With
With ActiveDocument.MailMerge
  .MainDocumentType = wdFormLetters
  .OpenDataSource Name:=MMSrc,ConfirmConversions:=False,ReadOnly:=True,_
    LinkToSource:=False,AddToRecentFiles:=False,Revert:=False,_
    Format:=wdOpenFormatAuto,Connection:="",SQLStatement:="",_
    SQLStatement1:="",SubType:=wdMergeSubTypeOther
  .Destination = wdSendToNewDocument
  .SuppressBlankLines = True
  With .DataSource
    .FirstRecord = wdDefaultFirstRecord
    .LastRecord = wdDefaultLastRecord
  End With
  .Execute Pause:=False
End With
Application.DisplayAlerts = wdAlertsAll
Windows("LabelFinal").Activate
End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。