[英]How to OpenDataSource for Word MailMerge from Excel Worksheet
I am trying to automate the creation of a word document using the OpenDataSource
from MailMerge
and using as source a worksheet where previously the data was saved.我正在尝试使用MailMerge
中的OpenDataSource
自动创建 Word 文档,并使用以前保存数据的工作表作为源。
The problem is that everytime the wdocSource.MailMerge.OpenDataSource
is called the excel pauses with the execution.问题是每次调用wdocSource.MailMerge.OpenDataSource
时,excel 都会暂停执行。 The process WINWORD.EXE
is running but Excel doesn't continue as it were waiting for something to happen and I have to kill the process to make it respond.进程WINWORD.EXE
正在运行,但 Excel 没有继续,因为它正在等待发生某些事情,我必须终止该进程以使其响应。
I checked these questions but I cannot make it work:我检查了这些问题,但我无法让它发挥作用:
Mailmerge from Excel using Word template VBA 使用 Word 模板 VBA 从 Excel 进行邮件合并
Executing Word Mail Merge 执行 Word 邮件合并
Running a MS Word mail merge from excel 从 excel 运行 MS Word 邮件合并
Const sTempSourceSheet = "TempSourceSheet"
Creating worksheet source创建工作表源
Sub PrintArray(Data, SheetName, StartRow)
Dim Destination As range
Set Destination = range("A" & StartRow)
Set Destination = Destination.Resize(1, UBound(Data))
Destination.FormulaR1C1 = Data
End Sub
''''''''''''''''''''''''''''''''''''''''
' SaveSourceSheet
Public Sub SaveSourceSheet(cols() As String, arr() As String)
On Error GoTo error
Dim ws As Worksheet
With ActiveWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.count)).Name = sTempSourceSheet
End With
PrintArray cols, sTempSourceSheet, 1
PrintArray arr, sTempSourceSheet, 2
done:
Exit Sub
error:
With ActiveWorkbook
.Sheets(sTempSourceSheet).Delete
End With
Resume done
End Sub
And the code for runnig the MailMerge以及运行 MailMerge 的代码
Sub Contract(wordfile As String)
Dim wd As Object
Dim wdocSource As Object
Dim excelfile As String
Dim strWorkbookName As String
excelfile = ThisWorkbook.path & "\" & ThisWorkbook.Name
On Error Resume Next
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wdocSource = wd.Documents.Open(wordfile)
wdocSource.MailMerge.MainDocumentType = wdFormLetters
wdocSource.MailMerge.OpenDataSource Name:= _
excelfile, ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, format:=wdOpenFormatAuto, _
Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";" & _
"User ID=Admin;" & _
"Data Source=" & excelfile & ";" & _
"Mode=Read;Extended Properties=" & _
"HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";" _
, SQLStatement:="SELECT * FROM `TempSourceSheet$`", SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
With wdocSource.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
wd.visible = True
wdocSource.Close SaveChanges:=False
Set wdocSource = Nothing
Set wd = Nothing
End Sub
Any idea?任何想法?
update更新
After the changes proposed by @macropod I still have some issues:在@macropod 提出的更改之后,我仍然有一些问题:
In the line .OpenDataSource
word shows this message:在.OpenDataSource
行中,字显示此消息:
Any of the options throws an error:任何选项都会引发错误:
I checked and the Excel file is present and contains a worksheet with the proper name.我检查并发现 Excel 文件存在并且包含一个具有正确名称的工作表。
« The problem is that everytime the wdocSource.MailMerge.OpenDataSource is called the excel pauses with the execution. «问题是每次调用 wdocSource.MailMerge.OpenDataSource 时,excel 都会暂停执行。 The process WINWORD.EXE is running but Excel doesn't continue as it were waiting for something to happen and I have to kill the process to make it respond.进程 WINWORD.EXE 正在运行,但 Excel 没有继续,因为它正在等待发生某些事情,我必须终止该进程以使其响应。 » »
That indicates that the document you're trying to open is probably already a mailmerge main document and the code is waiting for you to respond to the SQL query Word produces when opening such documents.这表明您尝试打开的文档可能已经是邮件合并主文档,并且代码正在等待您响应 Word 在打开此类文档时生成的 SQL 查询。
Alternatively, if the document contains auto macros, it could be waiting for a user response.或者,如果文档包含自动宏,它可能正在等待用户响应。
Your code also contains:您的代码还包含:
ReadOnly:=False, LinkToSource:=True
which should be:应该是:
ReadOnly:=True, LinkToSource:=False
I'd also suggest changing the provider, to:我还建议将提供者更改为:
Microsoft.ACE.OLEDB.12.0
Try the following code:试试下面的代码:
Sub Contract(wordfile As String)
Dim wdApp As Object, wdDoc As Object
Dim StrMMSrc As String: StrMMSrc = ActiveWorkbook.FullName
If Dir(wordfile) = "" Then
MsgBox "Cannot find:" & vbCr & wordfile, vbExclamation
Exit Sub
End If
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
With wdApp
.Visible = True
.WordBasic.DisableAutoMacros
.DisplayAlerts = 0 ' wdAlertsNone
Set wdDoc = .Documents.Open(wordfile)
With wdDoc
With .MailMerge
.MainDocumentType = wdFormLetters
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.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 `TempSourceSheet$`", SubType:=wdMergeSubTypeAccess
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
.Close SaveChanges:=False
End With
End With
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.