简体   繁体   English

自动字 VBA MailMerge

[英]Automated Word VBA MailMerge

I was looking at the possibility of having a Word Mailmerge file that on opening the file a dialog box opens for the data source (excel file with different names).我正在研究拥有一个 Word Mailmerge 文件的可能性,该文件在打开文件时会为数据源(具有不同名称的 Excel 文件)打开一个对话框。 It doesnt have to be mailmerge.... i am beating my head against wall every way I have tried fails...它不必是邮件合并......我尝试过的每一种方式都在用头撞墙......

You could use a Document_Open macro like:您可以使用 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 Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM