Copy two fields from a form to a word template

N

naigy

Hi I have been trying for a couple of days now and researching on various
forums and tutorials and I just can't seem to get this to work. I have two
fields (only trying one at the moment) that I want to copy from a form to a
word template when a button is pushed on a form. I have included two examples
which I have tried with the following errors

Example 1 Gives the following error. Run-time error '5941': The requested
member of the collection does not exist.

Sub Command39_Click()

Dim MyWord As Word.Application
Dim PathDocu As String

Set MyWord = New Word.Application
PathDocu = "C:\"

With MyWord
.Documents.Open (PathDocu & "QCATS.dot")
.ActiveDocument.Bookmarks("Serial").Select ' If I remove the ""
from around Serial then in the debug window when I mouse over that the
current value pops up in a textbox. It also pops up when mouse over the Me.
Serial on the following line. However everyone seems to have the "" in there
examples everywhere. Either way I still get the above error message.
.ActiveDocument.Selection.Text = Me.Serial

..Visible = True
End With

End Sub



Example 2 Gives the following error. Run-time error '5101': This bookmark
does not exist. The weird thing is though if I mouse over the Name =
Bookmark1 I get bookmark1=empty pop up in a text box if I am in a debug
window. If I change this to Bookmark2 (which doesn't exist in the word doc)
then it doesn't pop up.


Private Sub Command42_Click()
Dim wd As Word.Application
Dim myDoc As Word.Document

Set wd = New Word.Application
wd.Documents.Add "c:\Delete.Doc"
Set myDoc = wd.ActiveDocument
With wd.Selection
.GoTo wdGoToBookmark, Name:=Bookmark1
.Font.Bold = True ' You can choose if bold
.Font.Underline = False ' or underline
.TypeText Text:=Me.Serial
End With
wd.Visible = True

End Sub







Can someone please help to clear up the error in one of the above two
examples. Once I solve this I need to work out how to get it to open a
different word template depending on another field in the form.

Thanks for any assistance you may be able to provide.
 
J

John Nurick

It's always best to avoid using the Selection object when automating
Word or Excel. Instead, work directly with the range you're interested
in.

The following should work. It assumes that the names of the bookmarks
are Bookmark_A and Bookmark_B, and that the data you want to put in them
is in the controls txt_A and txt_B on your form:

Dim oWord As Word.Application
Dim oDoc As Word.Document

Set oWord = CreateObject("Word.Application")

'Use .Add to create a new document from a template,
'and .Open to open an existing document
Set oDoc = oWord.Documents.Add("TestAutomation.dot")

'Range.Text expects a string, so we use Nz to convert
'any Null values into empty strings.
oDoc.Bookmarks("Bookmark_A").Range.Text = Nz(Me.txt_A.Value, "")
oDoc.Bookmarks("Bookmark_B").Range.Text = Nz(Me.txt_B.Value, "")

'Save and close the document
...

If you want the bookmarks to contain the text you insert, you have to
jump through various hoops. See http://word.mvps.org for more.
 
N

naigy via AccessMonster.com

Finally got it sorted. One of the tutorials I read elsewhere said Cntr + F9
to insert a bookmark in a word file. Redid it by the insert menu -> bookmark
and all is now working fine. Thanks for your help.

John said:
It's always best to avoid using the Selection object when automating
Word or Excel. Instead, work directly with the range you're interested
in.

The following should work. It assumes that the names of the bookmarks
are Bookmark_A and Bookmark_B, and that the data you want to put in them
is in the controls txt_A and txt_B on your form:

Dim oWord As Word.Application
Dim oDoc As Word.Document

Set oWord = CreateObject("Word.Application")

'Use .Add to create a new document from a template,
'and .Open to open an existing document
Set oDoc = oWord.Documents.Add("TestAutomation.dot")

'Range.Text expects a string, so we use Nz to convert
'any Null values into empty strings.
oDoc.Bookmarks("Bookmark_A").Range.Text = Nz(Me.txt_A.Value, "")
oDoc.Bookmarks("Bookmark_B").Range.Text = Nz(Me.txt_B.Value, "")

'Save and close the document
...

If you want the bookmarks to contain the text you insert, you have to
jump through various hoops. See http://word.mvps.org for more.
Hi I have been trying for a couple of days now and researching on various
forums and tutorials and I just can't seem to get this to work. I have two
[quoted text clipped - 55 lines]
Thanks for any assistance you may be able to provide.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top