我正在研究是否可能有一个Word Mailmerge文件,在打开该文件时,会为数据源打开一个对话框(excel文件具有不同的名称)。不一定要邮件合并..。我每一次尝试都失败了.
发布于 2020-05-02 04:15:14
您可以使用Document_Open宏,如:
Private Sub Document_Open()
Application.ScreenUpdating = False
Dim StrMMSrc As String
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
.Title = "Data Source Selector"
.AllowMultiSelect = False
.Filters.Add "Documents", "*.xls; *.xlsx; *.xlsm", 1
.InitialFileName = ""
If .Show = -1 Then
StrMMSrc = .SelectedItems(1)
Else
GoTo ErrExit
End If
End With
With ActiveDocument.MailMerge
.OpenDataSource Name:=StrMMSrc, ReadOnly:=True, AddToRecentFiles:=False, _
LinkToSource:=False, Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;" & _
"Data Source=StrMMSrc;Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
SQLStatement:="SELECT * FROM `Sheet1$`"
End With
ErrExit:
Application.ScreenUpdating = True
End Subhttps://stackoverflow.com/questions/61547489
复制相似问题