Found this website: http://www.tek-tips.com/faqs.cfm?fid=2379
Has this code:
Code:
Public Function CreateWordLetter(strDocPath As String)
'function returns nothing, but I created this as a
'function so all those macro users out there could
'use it also. :P
'if no path is passed to function, exit - no further
'need to do anything
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 - now let's fill it with data.
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
'find out if the user would like to print the document
'at this time.
PrintResponse = MsgBox("Print this document?", vbyesno)
If PrintResponse = vbYes Then
objWord.ActiveDocument.PrintOut Background:=False
End If
'release all objects
Set objWord = nothing
Set dbs = nothing
End Function
So I modified it to look like this:
Code:
Public Function CreateWordLetter(strDocPath As String)
'function returns nothing, but I created this as a
'function so all those macro users out there could
'use it also. :P
'if no path is passed to function, exit - no further
'need to do anything
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 - now let's fill it with data.
With objWord
.Visible = True
.Documents.Open (strDocPath)
'move to each bookmark, and insert correct text.
.ActiveDocument.Bookmarks (txtFirstName.Select)
.Selection.Text = (CStr(Forms!tblTour!txtFirstName))
.ActiveDocument.Bookmarks.Add Name:=txtFirstName,
Range = Selection.Range
** continue the ActiveDocument and Selection statements for each bookmark that you have on the Word Document **
End With
'find out if the user would like to print the document
'at this time.
PrintResponse = MsgBox("Print this document?", vbYesNo)
If PrintResponse = vbYes Then
objWord.ActiveDocument.PrintOut Background:=False
End If
'release all objects
Set objWord = Nothing
Set dbs = Nothing
End Function
This line has an error:
.ActiveDocument.Bookmarks.Add Name:=txtFirstName,
When run, it gives me this message:
=====================
Compile Error:
Method or data member not found
[OK] [Help]
=====================
I realize this is a function, and in my C++ class we have to put the functions at the beginning of the code, where do we put functions for this type of thing? Button_OnClick?