我一直试图“绕过”当您使用Excel对Word文档进行MailMerge时出现的MailMerge警报。
我试图保存每个文档,但“没有附加数据源,但修改每个文档中的VBA代码以执行适当的OpenDataSource”,如excel VBA to Automatically select Yes when prompted during mail merge中所述。
但仍有消息要求选择Sheet1$。
另外,DisplayAlerts=0不是为我工作的。我猜就像@Ashton一样,“我对定位有问题。当我在设置wdDoc.DisplayAlerts = 0之前设置wdDoc = GetObject(wdInputName, "Word.document")时,它显然不能工作,因为wdDoc没有设置。但是如果我把它放在一行后面,那就太晚了,因为这个词只有在这时才会打开--所以就像How to use VBA to say 'Yes' to any SQL prompts in word?中所说的”太晚了“。
此代码的思想是避免任何可能的错误来自用户。因此,必须尽可能少地从运算符中“单击”。包括MsgBox.
同样重要的是,我正在使用Office 2007对此进行编程。由于将使用该代码的多个用户和计算机,HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Word\Options不是一个选项。
以下是“守则”的一个例子:
Option Explicit
Sub wrd()
Dim name As String
Dim wrdapp As Word.Application
Dim wrddoc As Word.Document
Set wrdapp = CreateObject("word.application")
wrdapp.Visible = True
'wrdapp.DisplayAlerts = False
Word.Application.DisplayAlerts = False
Set wrddoc = wrdapp.Documents.Open("C:\............\3.2_CTO. A.FIN..docx")
With wrddoc
'.Application.DisplayAlerts = wdAlertsNone
.MailMerge.OpenDataSource ("C:\.......\3.1_Base de datos_FIN.xlsx")
.MailMerge.HighlightMergeFields = True
.MailMerge.ViewMailMergeFieldCodes = False
.MailMerge.DataSource.ActiveRecord = wdLastDataSourceRecord
name = Hoja14.Range("a2")
.SaveAs ("C:\...........\Clientes\Persona Física\Cto. " & (tipo) & "# " & (name) + ".docx")
wrdapp.Quit
Set wrddoc = Nothing
Set wrdapp = Nothing
Application.DisplayAlerts = True
End With
End Sub 发布于 2016-11-25 20:38:22
.DisplayAlerts = FalseDisplayAlerts是一个布尔表达式,而不是第一个。另外,如果您提供了代码片段,它将帮助我们所有人更好地排除故障。
您也可以尝试.EnableEvents = False。这通常适用于“保存更改”提示符,但也可能适用于您的情况。同样,在这种情况下,代码引用也会有所帮助。
编辑:看到代码后,我会尝试将这个SQLStatement添加到.MailMerge.OpenDataSource对象中:
SQLStatement:="SELECT * FROM Sheet1$"
示例:
.MailMerge.OpenDataSource "C:\.......\3.1_Base de datos_FIN.xlsx",_
SQLStatement:="SELECT * FROM 'Sheet1$'"发布于 2016-12-07 19:45:42
终于成功了。
代码的结尾是这样的:
'After defining variables and opening word app and word doc:
.MailMerge.OpenDataSource "C:\...\3.1_Base de datos.xlsx",_
LinkToSource:=True, _
SQLStatement:="SELECT * FROM `Sheet1$`"
.MailMerge.ViewMailMergeFieldCodes = False
.MailMerge.DataSource.ActiveRecord = wdLastDataSourceRecord
.MailMerge.MainDocumentType = wdNotAMergeDocument
.SaveAs ("E:\_...\Cto. " & (tipo) & "# " & (name) + ".docx")
.Close
wrdapp.Quit
Set wrddoc = Nothing
Set wrdapp = Nothing
'Remember that you need to establish in the VisualBasic Editos
' /Tools/References/Microsoft Word Object Library in order to access
'Word VBA's references.它不能工作的原因是因为ReadOnly属性位于DataBase所在的一个文件夹中。显然,更改文件夹属性并不总是那么简单。因此,对我来说,的解决方案是从这些文件夹中删除DataBase并将其存储在一个新的中.
我希望这可能会带来有用的结果。
谢谢你的帮助萨尔瓦多瓦伊顺。
https://stackoverflow.com/questions/40811945
复制相似问题