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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word/Access automation question 1

Status
Not open for further replies.

eagleclaw

Programmer
Sep 20, 2002
19
US
What I am trying to accomplish is to open a word document from access and publish the results of a query into a table in word. I have 2 problems


When I try to instantiate word with

Private Sub cmdOutputCorel_Click()

Dim wordapp As Application
Dim rst As ADODB.Recordset

Set wordapp = word.Application
DoCmd.SetWarnings True

I get an error at set wordapp = word.Application

Second I think I need to stay with ADO for my access recordset. How do I declare a recordset based on a stored query using ADO.

The way that I normally open a recordset is
dim db as database
dim rs as recordset
set db = Currentdb
set rs = db.OpenRecordset("Stored Query Name")

If I try to use that I get a user defined type not defined at dim db as database.

 
Check to see if the Microsoft DAO library you have referenced is near the top of the reference list. If it isn't (ADO libs first) then your code won't work. Or try this:
For the following to work, do an ALT+F11 to go into VBA. Click Tools, then References and check the latest version you have of the Microsoft DAO library. MAKE SURE YOU MOVE IT NEAR THE TOP OF THE LIBRARY LIST.

Public Function CreateWordLetter(strDocPath As String)

'if no path is passed to function, exit
If IsNull(strDocPath) Or strDocPath = "" Then
Exit Function
End If

Dim dbs As Database
Dim objWord As Object
Dim PrintResponse
Set dbs = CurrentDb

'create reference to Word Object

Set objWord = CreateObject("Word.Application")

'Word Object is created

With objWord
.Visible = True
.Documents.Open(strDocPath)
'move to each bookmark, and insert correct text.
.ActiveDocument.Bookmarks("bookmark name").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

'release all objects

Set dbs = nothing

End Function

Other things that have to be done.

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 "First
Name", etc etc

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

4) Back to Access. Create a button on a form.

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

CreateWordLetter "path to your document"

Neil



 
I have see this Public Function CreateWordLetter a number of time and it seem to be really simple to use. But I can not get it to work maybe someone can help me I have the function name createWordletter I the form I have a command button on click I have:

Private Sub Command45_Click()
CreateWordLetter "C:\Documents and Settings\Irene\Desktop\dear"

End Sub

The problem is that when I click the button I get a compile error message: expected variable or procedure, not module.

Can someone tell me want I am doing wrong. Thank you Irene
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top