Sorry for the three posts today ... but I'm above my head here and need one last bit of help ... I have the following code in a Word VBA macro and it works fine: inserts a LINK FIELD in a specific bookmark, then adds the bookmark range (which now includes the LINK FIELD) back to the document. I say "it works fine" ... this is true when I invoke the macro from Word. But now I'm trying to run the same code from Excel, and it does not work. I cannot figure out why. And I'm a VBA novice so I feel like I'm just running around in circles here. Can someone please take a look at the code below and see if you can spot the problem?
Here's the snippet from the Word macro that works:
Set BMRange = ActiveDocument.Bookmarks("AcceptedByPerson").Range
Selection.GoTo What:=wdGoToBookmark, Name:="AcceptedByPerson"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"LINK Excel.Sheet.8 " & docname & " ""EstimatingInput!TLFEstimator""" & formatting, _
PreserveFormatting:=False
ActiveDocument.Bookmarks.Add Range:=BMRange, Name:="AcceptedByPerson"
And here's the full SUB from Excel. In the middle there are comments from me saying "this one works" and "this one doesn't work". The "this one works' lines do work, but they only do a straight text paste. I need to paste in a LINK FIELD, which is a little different. I've included the entire SUB because there may be something in there that looks innocent to me that's causing the problem. I basically cobbled together the "Word Automation" stuff from other posts on this forum. If you see something in this code that's opening/closing Word, which may cause other problems for me I'd appreciate a "heads up" on that, too.
Sub ExportToWord()
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 _
("C:\Data\Contracts\TestInsert.doc")
wdApp.Visible = True
Dim SourceRange As Range
Dim BMRange As Word.Range
Dim formatting As String
Dim docname As String
formatting = " \a \r \* MERGEFORMAT"
docname = "C:\\Data\\Contracts\\Test13.xls"
Application.ScreenUpdating = False
Set SourceRange = Range("CustomerName")
SourceRange.Copy
Set BMRange = ActiveDocument.Bookmarks("BM1").Range
' This works ...
'With BMRange
' .Select
' .PasteSpecial Link:=False, DataType:=wdPasteText, _
' Placement:=wdInLine, DisplayAsIcon:=False
' End With
' This does not work ...
BMRange.Select
wdApp.Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"LINK Excel.Sheet.8 " & docname & " ""EstimatingInput!TLFEstimator""" & formatting, _
PreserveFormatting:=False
wdApp.ActiveDocument.Bookmarks.Add Range:=BMRange, Name:="BM1"
MsgBox (" Text inserted")
With wdApp.ActiveDocument
.Save
.Close
End With
wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
MsgBox "The report succesfully transferred to the document", vbInformation
End Sub 'ExportToWord
Here's the snippet from the Word macro that works:
Set BMRange = ActiveDocument.Bookmarks("AcceptedByPerson").Range
Selection.GoTo What:=wdGoToBookmark, Name:="AcceptedByPerson"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"LINK Excel.Sheet.8 " & docname & " ""EstimatingInput!TLFEstimator""" & formatting, _
PreserveFormatting:=False
ActiveDocument.Bookmarks.Add Range:=BMRange, Name:="AcceptedByPerson"
And here's the full SUB from Excel. In the middle there are comments from me saying "this one works" and "this one doesn't work". The "this one works' lines do work, but they only do a straight text paste. I need to paste in a LINK FIELD, which is a little different. I've included the entire SUB because there may be something in there that looks innocent to me that's causing the problem. I basically cobbled together the "Word Automation" stuff from other posts on this forum. If you see something in this code that's opening/closing Word, which may cause other problems for me I'd appreciate a "heads up" on that, too.
Sub ExportToWord()
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 _
("C:\Data\Contracts\TestInsert.doc")
wdApp.Visible = True
Dim SourceRange As Range
Dim BMRange As Word.Range
Dim formatting As String
Dim docname As String
formatting = " \a \r \* MERGEFORMAT"
docname = "C:\\Data\\Contracts\\Test13.xls"
Application.ScreenUpdating = False
Set SourceRange = Range("CustomerName")
SourceRange.Copy
Set BMRange = ActiveDocument.Bookmarks("BM1").Range
' This works ...
'With BMRange
' .Select
' .PasteSpecial Link:=False, DataType:=wdPasteText, _
' Placement:=wdInLine, DisplayAsIcon:=False
' End With
' This does not work ...
BMRange.Select
wdApp.Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"LINK Excel.Sheet.8 " & docname & " ""EstimatingInput!TLFEstimator""" & formatting, _
PreserveFormatting:=False
wdApp.ActiveDocument.Bookmarks.Add Range:=BMRange, Name:="BM1"
MsgBox (" Text inserted")
With wdApp.ActiveDocument
.Save
.Close
End With
wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
MsgBox "The report succesfully transferred to the document", vbInformation
End Sub 'ExportToWord