Manipulating a Word document from VBA

  • Thread starter Thread starter Joop
  • Start date Start date
J

Joop

Hi all,

In an Access VBA app that I developed I start Word with a template and fill
the bookmarks in the template with data from Access. When the document is
filled I want to place the cursor on the right spot so the user can start
typing immidiately. The code looks as follows:

With WApp.ActiveDocument
.Bookmarks("BedrNaam").Range.Text = Me!FldBedrNaam
.Bookmarks("ContPers").Range.Text = Me!TxtAHKort
.Bookmarks("Onderw").Range.Text = Me!TxtOnderwerp
.Bookmarks("FaxNummer").Range.Text = Me!FldFax
End With
WApp.ActiveDocument.SaveAs FileName:=RootString & DocPadString &
Trim(Str(Year(Date))) & "\" & TypeString & CorNumString
Debug.Print WApp.ActiveDocument.Name
If OptBrFEM = 1 Then Selection.GoTo what:=wdGoToLine,
which:=wdGoToAbsolute, Count:=17
If OptBrFEM = 2 Then Selection.GoTo what:=wdGoToLine,
which:=wdGoToAbsolute, Count:=15
If OptBrFEM = 3 Then Selection.GoTo what:=wdGoToLine,
which:=wdGoToAbsolute, Count:=5
WApp.Visible = True
WApp.Activate

Up to the positioning of the cursor everything works well, but the
"Selection.GoTo" often (not always) goes wrong.
When it goes wrong I get a error 462 in Access (The external Servercomputer
does not exist or is not available, I don't know if this is the right text
as it is translated fron dutch)

When I activate Word verything looks normal, all data is there.

MOst of the times it works ok the first time Word is started. It looks as if
there is a difference between closing Word or closing just the document
after the document is finished.

Is it a bug???

regards Joop
 
You might try prefixing Selection with a . (dot), so that it refers
explicitly to WApp.ActiveDocument.

I recently had a client upgrade to Office 2003, and found I needed to make
this change in several places in my code to avoid the kind of errors you're
getting.

Also, yes, you'll need to Set WApp=Nothing to release that reference, even
after you've closed the document itself.

HTH
- Turtle
 
Try this to get a cursor in a bookmark:

objWord.ActiveDocument.Bookmarks("CompanyName").Select
objWord.Selection.TypeText Item.CompanyName

The above was use in an Outlook\Word automation to put the CompanyName field
from Outlook into the CompanyName bookmark in Word. Leaving out the second
line should leave the cursor in the bookmark if you display the document at
that point (or just before), as in:

objWord.Activate

--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 

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

Back
Top