View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jon Peltier Jon Peltier is offline
external usenet poster
 
Posts: 6,582
Default Populate Bookmarkes in a Word Doc. with Excel

Keep the reference to the active workbook, the worksheet and the range. To
let the user select a Word document:

sWdFileName = Application.GetOpenFilename("Word Documents (*.doc), *.doc), ,
"Select a Word document", , False)
Set doc = objWord.Documents.Open(sWdFileName)

You could name the cells with the data using names that match the bookmark
names. Name a cell by selecting it and typing the name in the name box (just
above cell A1) and pressing Enter. When I do this I usually have a unique
prefix to the bookmark and cell names. The bookmark names might be like
"xlexportBrokerFirstName", "xlexportBrokerLastName", etc. Then I do
something like

For Each bkmk In doc.Bookmarks
If Left$(bkmk.Name, 8) = "xlexport" then
bkmk.Range.Text =
ActiveWorkbook.Worksheets("LOOKUP").Range(bkmk.Nam e).Value
End If
Next

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"ryguy7272" wrote in message
...
I'm trying to push several values from an Excel sheet into a Word
document,
using VBA in Excel and bookmarks in Word. Below is what I have so far:

Sub PushToWord()

Dim objWord As New Word.Application
Dim doc As Word.Document
Dim bmk As Word.Bookmark
Set doc = objWord.Documents.Open("C:\Test\Disclosure.doc")
For Each bmk In doc.Bookmarks
If bmk.Name = "BrokerFirstName" Then bmk.Range.Text =
ActiveWorkbook.Sheet("LOOKUP").Range("B1").Value
If bmk.Name = "BrokerLastName" Then bmk.Range.Text =
ActiveWorkbook.Sheet("LOOKUP").Range("B2").Value
Next
'doc.Bookmarks("First_Name").Range.Text = Range("A1").Value
objWord.Visible = True

End Sub

The sheet with all the values is called LOOKUP and the ranges are pretty
obvious. What I can't figure out, is the following:

The macro will run from an Excel file, which will essentially be the
ActiveWorkbook, but I'm not sure how to tell Word that the data is coming
first from the ActiveWorkbook, and then from the sheet named LOOKUP, and
finally from specific cells. Do I even need ActiveWorkbook in there?

As it is written now, I can open a Word template from a specific location,
but I want to be able to open any one of a couple dozen Word files (not a
loop; just open it and let Excel know that this is the active document,
with
bookmarks, that need to be updated) from many locations. Thus, I am
trying
to incorporate the following line of code into the macro:

file = Application.GetOpenFilename


Does anyone have any ideas about the best way to set this up? I am
thinking
it is not difficult at all.just can't get my mind around it right now.

Regards,
Ryan--



--
RyGuy