PDA

View Full Version : Push Excel Named Range to Word Bookmark



icedkube
01-21-2010, 02:55 AM
Hi,

I have used code from this forum (courtesy Ken Puls & Suat Ozgur [kb_id=381]) which works well with transferring named range data with only 1 cell reference to a bookmark in a word document that is created from a word template.

For a named range with more than 1 cell ($D$1:$E$2) I get 'error' 13 (see code below).

Please Help.



Sub BCMerge()
Dim pappWord As Object
Dim docWord As Object
Dim wb As Excel.Workbook
Dim xlName As Excel.Name
Dim TodayDate As String
Dim Path As String
Set wb = ActiveWorkbook
TodayDate = Format(Date, "mmmm d, yyyy")
Path = wb.Path & "\pushmerge.dot"

On Error GoTo ErrorHandler
'Create a new Word Session
Set pappWord = CreateObject("Word.Application")

On Error GoTo ErrorHandler
'Open document in word
Set docWord = pappWord.Documents.Add(Path)
'Loop through names in the activeworkbook
For Each xlName In wb.Names
'if xlName's name is existing in document then put the value in place of the bookmark
If docWord.Bookmarks.Exists(xlName.Name) Then
docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
End If
Next xlName
'Activate word and display document
With pappWord
.Visible = True
.ActiveWindow.WindowState = 0
.Activate
End With
'Release the Word object to save memory and exit macro
ErrorExit:
Set pappWord = Nothing
Exit Sub
'Error Handling routine
ErrorHandler:
If Err Then
MsgBox "Error No: " & Err.Number & "; There is a problem"
If Not pappWord Is Nothing Then
pappWord.Quit False
End If
Resume ErrorExit
End If
End Sub

macropod
01-22-2010, 02:48 AM
Hi icedkube,

The use of multi-cell ranges suggests a tabular output. In that case, you could perhaps use something like:

If docWord.Bookmarks.Exists(xlName.Name) Then
Range(xlName).Copy
docWord.Bookmarks(xlName.Name).Range.PasteSpecial Link:=False, _
DataType:=wdPasteRTF, Placement:=wdInLine, DisplayAsIcon:=False
End If

icedkube
01-22-2010, 03:30 AM
Hi,

I have used a similar solution, I was trying to escape using the copy method as I have various bookmarks to populate. I know mail merge is an option but I will be changing the excel document name by revision and then mail merge will not work.

Thank you for the speedy response