我在更改部分代码后,在Excel中收到错误“对象不支持此属性或方法”。引发错误的行是“使用odoc.MailMerge”以下是代码:
strBookName = "\" & "PM MailMerge.xlsm"
strBookPath = ActiveWorkbook.Path
strBook = strBookPath & strBookName
'Opens Word.Application
Set ObjWord = CreateObject("Word.Application")
Set odoc = GetObject(strBook)
'Executes a Hidden Mail Merge
ObjWord.Visible = False
With odoc.MailMerge
.Destination = wdSendToNewDocument
With .DataSource
.FirstRecord = 1
.LastRecord = 1
odoc.MailMerge.Execute
Set odoc2 = odoc.Application.documents("Form Letters1")
odoc.Close True
End With
End With
答案 0 :(得分:0)
尝试
option explicit
Dim objWord as Word.Application
Dim oDoc as As Word.Document
Set ObjWord = CreateObject("Word.Application")
set oDoc = objWord.documents.Open strBook
不要忘记在菜单中引用Word库:tools>引用。
顺便说一句:将所有变量调暗(强制显示选项)。
编辑: 注意:您在代码示例中提到的文件是Excel .xlsm,而不是Word .doc ...
答案 1 :(得分:0)
设置odoc = GetObject(strBook)这是excel的工作簿吗?如果是这样,工作簿对象没有MailMerge方法。您需要引用word文档以使用Document objet的MailMerge方法