Using bookmarks to insert data

gwalker

New member
Local time
Today, 13:29
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
 
The following code shows you how to populate three different types of fields. When the ControlType is "Memo" the object in the word document is a plain bookmark. When the ControlType is "Checkbox" then the object is a checkbox. The default is a FormField.

Code:
        Do While rsDAO2.EOF = False
            If rsDAO2!ControlType = "Memo" Then
                If IsNull(rsDAO2!MemoValue) Then
                Else
                     WordDoc.BookMarks(rsDAO2!BookMarkName).Range.Fields(1).result.Text = rsDAO2!MemoValue
                End If
            Else
                If IsNull(rsDAO2!TextValue) Then
                Else
                    If rsDAO2!ControlType = "Checkbox" Then
                        If rsDAO2!TextValue = "Yes" Then
                            WordDoc.FormFields(rsDAO2!BookMarkName).CheckBox.value = True
                        Else
                            WordDoc.FormFields(rsDAO2!BookMarkName).CheckBox.value = False
                        End If
                    Else
                        WordDoc.FormFields(rsDAO2!BookMarkName).result = rsDAO2!TextValue
                    End If
                End If
            End If
            rsDAO2.MoveNext
        Loop
 

Users who are viewing this thread

Back
Top Bottom