Using bookmarks to insert data

gwalker

New member
Local time
Today, 08:30
Joined
May 10, 2008
Messages
2
I have a database that amongst other things requires a user to enter data, which after saving it to the database, then takes that same information and puts it into a word document ready for the user to complete the word document. I am attempting to insert the data using bookmrks with the following code.

'Gets word running
Set WordObj = CreateObject("Word.Application")


'makes word visible
WordObj.Visible = True

'makes new document based on the template
WordObj.Documents.Add Template:="G:\JLW Office Templates\Letter with logo.dot", NewTemplate:=False

WordObj.Activate

'insert text at bookmark
WordObj.Selection.Goto what:=wdGoToBookmark, Name:="job"
WordObj.Selection.TypeText [Forms]![correspondence entry form]![jobname]

All goes well until i reach the wdGoToBookmark line. I keep getting the message Runtime error 5101 This bookmark does not exist.

The bookmark definitley does exist in the template. And in the Word Session opened by the code, the document also contains the bookmark.

I cant figure out why the code cant find the bookmark.

Can anyone help?

TIA
Geoff
 
Maybe the wrong document is being searched?
Documents.Add returns a document object - you should use this to search the bookmark.
I would try something like the following

with WordObj.Documents.Add Template:="G:\JLW Office Templates\Letter with logo.dot", NewTemplate:=False
.Bookmarks("job").Range.Text = [Forms]![correspondence entry form]![jobname]
End With

If not already,I would also add a reference to the Office library in Tools-References, so you can use Intellisense.
dim WordObj as Word.Application

If the above doesn't work, put a break on the bookmarks line, and query the document in the debug window, to see what bookmarks exist.
 
Thanks Mearle. I ended up using the following

Set WordObj = CreateObject("Word.Application")
WordObj.Visible = True
WordObj.Documents.Add Template:="G:\JLW Office Templates\memo with logo.dot", NewTemplate:=False
Dim corresnoentry
corresnoentry = [Forms]![correspondence entry form]![corresno]
WordObj.ActiveDocument.Bookmarks("corresno").Select
WordObj.Selection.Text = corresnoentry

And this worked fine. I will play with yours and incorporate.

Thanks
 

Users who are viewing this thread

Back
Top Bottom