Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening MS Word 2

Status
Not open for further replies.

Basia

Programmer
Apr 27, 2001
64
ML
Greetings.

I am trying to open up MS Word from an Access Form using a merge. I have a field in an Access Table that strings together several fields.

This field needs to be displayed on a pre-printed flyer and editable to users before printing. I want to:

* Click on a button and open word to a .doc with correct margins
* Move the current record's data into the merge document
* Allow users to edit the data in Word
* Print the flyer and return back to Access.

Any help????

Thanks in advance...Basia
 
Basia:

I hope the following function helps you out. You will need a reference to the Microsoft Word 8.0 or 9.0 Library, depending on your version of Word. Also, your Word document will have to have bookmarks at the insertion points - more on bookmarks to follow. :)

Function CreateWordLetter(strDocPath As String)

Dim dbs As Database, rst As Recordset, objWord As Object
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset (<enter table name here>)
If rst.Recordcount = 0 Then
'no records, no need to continue
Exit Function
End if

'create reference to Word Object
Set objWord = CreateObject(&quot;Word.Application&quot;)
'Word Object is created - now let's fill it with data.

While Not rst.EOF
With objWord
.Visible = True
.Documents.Open(strDocPath)
'move to each bookmark, and insert correct text.
.ActiveDocument.Bookmarks(&quot;<bookmark name>&quot;.Select
.Selection.Text=(Cstr(Forms!<form name>!<field
name>))
.ActiveDocument.Bookmarks.Add Name:=<bookmark name>,
Range = Selection.Range
**continue the ActiveDocument and Selection statements
for each bookmark that you have on the Word Document **
End With
'Print the document in the foreground, and then close
the document without saving changes.
objWord.ActiveDocument.PrintOut Background:=False
rst.MoveNext
Wend
'release all objects

set objWord = nothing
set dbs = nothing
set rst = nothing

End Function

Ok, now the function is written - however, this is only half the battle. Now we have to create/edit the document you are using so the above code actually works! :)

1) Open your document. If you do not have a current
document that you are not using, create a new one.

2) In the places where the values will change based on
information in the record, create a bookmark and name
it appropriately. i.e. If you have a field in Access
called FirstName, then make the bookmark name &quot;First
Name&quot;, etc etc

3) After creating / editing your document, save it.
Remember the file name.

4) Back to Access! Create a button on a form somewhere,
and name it appropriately (ie cmdMerge, cmdMakeLetter,
etc)

5) In the On_Click event of your button, use an event
procedure and add the following line of code:

CreateWordLetter &quot;<path to your document>&quot;

6) Watch everything kick into action! :)

HTH

Greg Tammi, ATS Alarm Supervisor
 
Thanks so much for the quick feedback.

I am getting one error. At this line:

.ActiveDocument.Bookmarks(&quot;<bookmark name>&quot;.Select

it says &quot;Object Required&quot;. Is there a something I am missing?

Thanks,
basia
 
Okay, ignore my previous message! I got it to work!!! Still a couple of small questions.

On this line:

objWord.ActiveDocument.PrintOut Background:=False

I want to have the word doc open so that the users can format the text before printing. What is the syntax?

Thanks again - you made my week!

Basia
 

hmm...Make sure you remove the Set objWord = Nothing statement if you want Word to stay open for editing.

By setting the variable objWord to Nothing (aka 'releasing the variable'), you would then have no variable available to contain the Word instance, hence it's closing.

HTH

Greg

 
Got it to work perfectly! Thanks so much again.

Regards,
Basia
 
Hello, I came across the above code and decided to give it a try. I am running into problems. I've followed the directions: creating a module, entering the code with my bookmark names and field names, and entering the book marks into my Word doc, creating a form with the fields, and command button which runs the code for the path of the specific Word doc.

I am getting an error: "Run-time error 5941- the requested member of the collection does not exit". When I click Debug, it takes me to the first line of code where I have started inserting bookmark names and field names (Active Document.Bookmark...). The correct Word doc is opening, but no data is showing up where my bookmarks are saved.

Any ideas what I'm doing wrong?

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top