I need to run a macro from Excel that can reference a bookmark defined in a Word document. The macro must insert a LINK FIELD into this bookmark. I'm trying to use the code below, which I wrote and which work if run from a Macro residing in a Word document.
Unfortunately, when I try running the same code fragment in a Macro residing in an Excel spreadsheet, I get a variety of error messages, depending on exactly what portion of the code I try "tweaking." The error messages all say stuff like "application-defined or object-defined error" or "method 'range of object '_global' failed" or "method not supported for object" or other similar things.
I HAVE turned on the reference to the MS Word object library in VBE/Tools/References. And my Excel macro IS able to successfully open the Word document. In fact, I can copy/paste other information from the Excel sheet into the Word document just fine ... I just can't get this LINK FIELD to work.
Here's the code that works from a Word Macro:
Set BMRange = ActiveDocument.Bookmarks("WordBM").Range
Selection.Goto What:=wdGoToBookmark, Name:="WordBM"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:= "LINK Excel.Sheet.8 " & docname & " Sheet1!ExcelRange" & _
formatting, PreserveFormatting:=False
ActiveDocument.Bookmarks.Add Range:=BMRange, Name:="WordBM"
Wonder what I need to change to get it to work from within Excel?
oh, BMRange is Dim'd "as range" in the word macro, and "as Word.Range" in the Excel macro. Docname and Formatting are just Strings of text
With the exact code above, I'm getting "run time error #438 - object doesn't support this property or method" error pointing to the Selection.Goto line -- when trying it from within Excel.
Unfortunately, when I try running the same code fragment in a Macro residing in an Excel spreadsheet, I get a variety of error messages, depending on exactly what portion of the code I try "tweaking." The error messages all say stuff like "application-defined or object-defined error" or "method 'range of object '_global' failed" or "method not supported for object" or other similar things.
I HAVE turned on the reference to the MS Word object library in VBE/Tools/References. And my Excel macro IS able to successfully open the Word document. In fact, I can copy/paste other information from the Excel sheet into the Word document just fine ... I just can't get this LINK FIELD to work.
Here's the code that works from a Word Macro:
Set BMRange = ActiveDocument.Bookmarks("WordBM").Range
Selection.Goto What:=wdGoToBookmark, Name:="WordBM"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:= "LINK Excel.Sheet.8 " & docname & " Sheet1!ExcelRange" & _
formatting, PreserveFormatting:=False
ActiveDocument.Bookmarks.Add Range:=BMRange, Name:="WordBM"
Wonder what I need to change to get it to work from within Excel?
oh, BMRange is Dim'd "as range" in the word macro, and "as Word.Range" in the Excel macro. Docname and Formatting are just Strings of text
With the exact code above, I'm getting "run time error #438 - object doesn't support this property or method" error pointing to the Selection.Goto line -- when trying it from within Excel.