修复运行时错误'91:对象变量或未设置块变量

时间:2015-12-07 18:41:24

标签: excel vba excel-vba

我被要求修改excel中的宏。宏应该将excel单元格中的值传递给word中的字段或书签。除了

之外,一切都很好
  

“运行错误91:对象变量或未设置块变量”。

Set wdDoc ...

这是一个间歇性的错误,大部分时间我得到它,但很少有其他我没有;我不知道为什么不一致。我在Microsoft Excel 2011,Mac OSX中工作。我已经通过Tools ==>包含了对Excel和Word库的引用。引用。

我是编程的新手,实际上我没有编写这段代码,只是我改变的是使用Forms而不是Bookmarks将值传递给MS Word,到目前为止一直有效。

感谢您的帮助!!!!

Sub Button91_Click()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then 'Word isn’t already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

Set wdDoc = wdApp.Documents.Open("file:/Users/myUserName/Desktop/folderName/subFolder/documentName.docx", ReadOnly:=True) 'In this whole like is where I get the error. Again, few times it works perfectly, most of the time it doesn't.

wdApp.Visible = True

With wdDoc.FormFields
  .Item("fldNombre").Result = Worksheets("Persona Fisica").Range("B3") + " " + Worksheets("Persona Fisica").Range("B4")
'Here I add more fields.

End With

0 个答案:

没有答案