H
hlock
I really need some help! I am trying to send a record to Word through a
table in Access. The user enters information on a form and clicks a button
to run a make-table query and export the information to Word. I've been at
this for what seems like forever and it's just not working. My knowledge on
coding is very limited so I try to see what others have done. I'm afraid now
that I am just so confused that I don't know where to go. And - I still
haven't gotten to automating the make-table query. If anyone could take the
time to help, I would really appreciate it.
I'm getting a Object Required (424) error message at the InsertABookmark
lines. I am using Access 2003 and have referenced the Word 11.0 Object
Library, the Office 11.0 Object Library, and the DAO 3.6 Object Library
This is what I have so far:
Behind the button that the user clicks on:
Option Compare Database
Private Sub cmdBookMark_Click()
On Error GoTo Err_Handler
Dim strTemplateDoc As String
strTemplateDoc = ("P:\hlock\winword\New Assignment Letter.dot")
If Not IsNull(Me!ClaimNo) Then
CreateLetter strTemplateDoc
Else
MsgBox "No current record.", vbInformation, "Create Letter"
End If
Exit_here:
Exit Sub
Err_Handler:
MsgBox Err.Description & " (" & Err.Number & ")", vbExclamation, "Error"
Resume Exit_here
End Sub
The coding to send the data to the bookmarked Word document:
Private Sub InsertAtBookmarks(objW As Object, _
objD As Object, _
strBookmark As String, _
varText As Variant)
' select bookmark
objD.Bookmarks(strBookmark).Select
' insert text at bookmark
objW.Selection.Text = Nz(varText, "")
End Sub
Sub CreateLetter(strTemplate As String)
' Opens a document in Word and inserts values from
' current record at bookmarks in Word document.
' Accepts: path to Word template file - String
On Error GoTo Err_Handler
Dim objWord As Object
Dim objDoc As Object
Dim tbl As Table
' return reference to form
Set tbl = Table![All Information for Letter]
' if Word open return reference to it
' else establish reference to it
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set objWord = CreateObject("Word.Application")
End If
AppActivate "Microsoft Word"
On Error GoTo Err_Handler
' open Word document in maximised window
objWord.Visible = True
Set objDoc = objWord.Documents.Add(strTemplate)
objWord.WindowState = wdWindowStateMaximize
' insert text at bookmarks, getting values from form
InsertAtBookmarks objWord, objDoc, "Clmnum", Table![All Information for
Letter]!CLAIM
InsertAtBookmarks objWord, objDoc, "Clmoffce", Table![All Information
for Letter]!Loction
InsertAtBookmarks objWord, objDoc, "Csecat", Table![All Information for
Letter]!CsCt
InsertAtBookmarks objWord, objDoc, "Csetyp", Table![All Information for
Letter]!CsTp
InsertAtBookmarks objWord, objDoc, "DOL", Table![All Information for
Letter]!ACCDTE
InsertAtBookmarks objWord, objDoc, "Examnr", Table![All Information for
Letter]!Name
InsertAtBookmarks objWord, objDoc, "InsNam", Table![All Information for
Letter]!INSNAM
InsertAtBookmarks objWord, objDoc, "Party", Table![All Information for
Letter]!Party
InsertAtBookmarks objWord, objDoc, "Polnum", Table![All Information for
Letter]!POLICY
Set objDoc = Nothing
Set objWord = Nothing
Exit_here:
On Error GoTo 0
Exit Sub
Err_Handler:
MsgBox Err.Description & " (" & Err.Number & ")"
Resume Exit_here
End Sub
Private Function GetWordApp() As Object
' if Word open return reference to it
' else establish reference to it
On Error Resume Next
Set GetWordApp = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set GetWordApp = CreateObject("Word.Application")
End If
End Function
Private Sub ShowWord(wApp As Object, wDoc As Word.Document, wActiveDoc As
Word.Document)
wApp.Visible = True
wApp.WindowState = wdWindowStateMaximize
wDoc.Close (wdDoNotSaveChanges)
wActiveDoc.Activate
wApp.Activate
End Sub
table in Access. The user enters information on a form and clicks a button
to run a make-table query and export the information to Word. I've been at
this for what seems like forever and it's just not working. My knowledge on
coding is very limited so I try to see what others have done. I'm afraid now
that I am just so confused that I don't know where to go. And - I still
haven't gotten to automating the make-table query. If anyone could take the
time to help, I would really appreciate it.
I'm getting a Object Required (424) error message at the InsertABookmark
lines. I am using Access 2003 and have referenced the Word 11.0 Object
Library, the Office 11.0 Object Library, and the DAO 3.6 Object Library
This is what I have so far:
Behind the button that the user clicks on:
Option Compare Database
Private Sub cmdBookMark_Click()
On Error GoTo Err_Handler
Dim strTemplateDoc As String
strTemplateDoc = ("P:\hlock\winword\New Assignment Letter.dot")
If Not IsNull(Me!ClaimNo) Then
CreateLetter strTemplateDoc
Else
MsgBox "No current record.", vbInformation, "Create Letter"
End If
Exit_here:
Exit Sub
Err_Handler:
MsgBox Err.Description & " (" & Err.Number & ")", vbExclamation, "Error"
Resume Exit_here
End Sub
The coding to send the data to the bookmarked Word document:
Private Sub InsertAtBookmarks(objW As Object, _
objD As Object, _
strBookmark As String, _
varText As Variant)
' select bookmark
objD.Bookmarks(strBookmark).Select
' insert text at bookmark
objW.Selection.Text = Nz(varText, "")
End Sub
Sub CreateLetter(strTemplate As String)
' Opens a document in Word and inserts values from
' current record at bookmarks in Word document.
' Accepts: path to Word template file - String
On Error GoTo Err_Handler
Dim objWord As Object
Dim objDoc As Object
Dim tbl As Table
' return reference to form
Set tbl = Table![All Information for Letter]
' if Word open return reference to it
' else establish reference to it
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set objWord = CreateObject("Word.Application")
End If
AppActivate "Microsoft Word"
On Error GoTo Err_Handler
' open Word document in maximised window
objWord.Visible = True
Set objDoc = objWord.Documents.Add(strTemplate)
objWord.WindowState = wdWindowStateMaximize
' insert text at bookmarks, getting values from form
InsertAtBookmarks objWord, objDoc, "Clmnum", Table![All Information for
Letter]!CLAIM
InsertAtBookmarks objWord, objDoc, "Clmoffce", Table![All Information
for Letter]!Loction
InsertAtBookmarks objWord, objDoc, "Csecat", Table![All Information for
Letter]!CsCt
InsertAtBookmarks objWord, objDoc, "Csetyp", Table![All Information for
Letter]!CsTp
InsertAtBookmarks objWord, objDoc, "DOL", Table![All Information for
Letter]!ACCDTE
InsertAtBookmarks objWord, objDoc, "Examnr", Table![All Information for
Letter]!Name
InsertAtBookmarks objWord, objDoc, "InsNam", Table![All Information for
Letter]!INSNAM
InsertAtBookmarks objWord, objDoc, "Party", Table![All Information for
Letter]!Party
InsertAtBookmarks objWord, objDoc, "Polnum", Table![All Information for
Letter]!POLICY
Set objDoc = Nothing
Set objWord = Nothing
Exit_here:
On Error GoTo 0
Exit Sub
Err_Handler:
MsgBox Err.Description & " (" & Err.Number & ")"
Resume Exit_here
End Sub
Private Function GetWordApp() As Object
' if Word open return reference to it
' else establish reference to it
On Error Resume Next
Set GetWordApp = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set GetWordApp = CreateObject("Word.Application")
End If
End Function
Private Sub ShowWord(wApp As Object, wDoc As Word.Document, wActiveDoc As
Word.Document)
wApp.Visible = True
wApp.WindowState = wdWindowStateMaximize
wDoc.Close (wdDoNotSaveChanges)
wActiveDoc.Activate
wApp.Activate
End Sub