just a little VBA Automation to Word

mcvayman

Registered User.
Local time
Today, 05:12
Joined
Mar 26, 2004
Messages
11
Hello there anyone who can help...

I'm trying to open a query in ACCESS and replace bookmarks in a WORD.DOT (template). Please see the code below:


DoCmd.OpenQuery "qry1370frm4"

'Declare an instance of MS Word.
Dim wrd As New Word.Application

Set wrd = CreateObject("Word.application")

'Open the document template, make it visible
wrd.Documents.Add "h:\My Documents\ADatabase\RptTemplate.dot"
wrd.Visible = True

'Replace each bookmark with current data.

With wrd.ActiveDocument.Bookmarks

Dim BMRange As Range
'Identify current Bookmark range and insert text
Set BMRange = ActiveDocument.Bookmarks("dos").Range
BMRange.Text = [dos]
'Re-insert the bookmark
ActiveDocument.Bookmarks.Add "dos", BMRange

End With

DoCmd.Close , "qry1370frm4", acSaveNo

- My problem comes when trying to set BMRange.Text as the field name in the query. it doesn't want to transfer and gives me a "can't find project or library" error message.

Any ideas why? When i place it is quotes teh automations works wonderfully (it doesn't fill in the field but the code opens word and does what it should up to that point)


Any help would be WONDERFUL

Thanks


mcvayman
 
at first guess you need to add wrd. before active document so access knows what you are talking about.

Set BMRange = wrd.ActiveDocument.Bookmarks("dos").Range
BMRange.Text = [dos]
'Re-insert the bookmark
wrd.ActiveDocument.Bookmarks.Add "dos", BMRange

Peter
 
Even with With??

Hey and thanks for helping..

Do i need to add wrd. to identify even if i have sandwhiched prefaced the entire series of replacements with

With wrd.activedocument.bookmarks etc.



End With

??

Thanks

mcvayman
 
strictly speaking
wrd.ActiveDocument.Bookmarks.Add "dos", BMRange
din not need 'wrd' but it does need a '.' prefixing it to tell access it belongs to the 'with'
.ActiveDocument.Bookmarks.Add "dos", BMRange

HTH

Peter
 
hmmmm

Hello and thanks SO much again for the help...

I was playing around with this and discovered that when i formatted this button (the one that automates to the WORD template) onto a form with all the fields in question listed, it worked fine.

WHy?!

I feel like i hit a problem and then "solve" it but don't know why.... or worse yet, i feel all the time like i'm trying to solve problems i don't need to solve or that i'm doing something i don't need to do. I have real DESIGN issues....

woah... a little psychotherapy might assist...

can you hold me?

mcvayman
 

Users who are viewing this thread

Back
Top Bottom