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

Problem with Access to Word in Windows XP

Status
Not open for further replies.

Zepher2

Technical User
Dec 29, 2001
25
US
Using a lot of help from others, I developed my own database which when I click a button opens a Word file from a template. Access then sends field values from a form to the newly created word document using custom field variables in Word. This worked fine for several years under Windows 98. I just purchased a new computer with Windows XP. I installed my Access 2000 and Word 2000 on the new computer. The file location for Microsoft word templates is different for XP so I copied my new files to the new location.
Now, when I try to run the VBA routine to do the above, I get an error message which reads Error 2147417851 Description: Method "Add" of object documents failed - which points me to the following line from the code below:

docs.Add strLetter


The code or the entire routine is:

Private Sub Cmd_Proof_of_Mailing_Click()
On Error GoTo ErrorHandler

Dim appWord As Word.Application
Dim docs As Word.Documents
Dim strLetter As String
Dim strTemplateDir As String
Dim prps As Object
Dim strDate As String

Set appWord = GetObject(, "Word.Application")
strDate = CStr(Date)

strTemplateDir = appWord.Options.DefaultFilePath(wdUserTemplatesPath)
strTemplateDir = strTemplateDir & "\Divorce\"
Debug.Print "Office templates directory: " & strTemplateDir
strLetter = strTemplateDir & "Proof_Of_Mailing.dot"
Debug.Print "Letter: " & strLetter

Set docs = appWord.Documents
docs.Add strLetter

Set prps = appWord.ActiveDocument.CustomDocumentProperties

With prps
.Item("Judge").Value = Nz(Me![Judge])
.Item("CaseNo").Value = Nz(Me![CaseNo])
.Item("CourtName").Value = Nz(Me![CourtName])
.Item("Plaintiff").Value = Nz(Me![Plaintiff])
.Item("Defendant").Value = Nz(Me![Defendant])

End With

With appWord
.Visible = True
.Activate
.Selection.WholeStory
.Selection.Fields.Update
.Selection.MoveDown Unit:=wdLine, Count:=1
End With

ErrorHandlerExit:
Exit Sub

ErrorHandler:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If

End Sub
Can anyone help me. I am not a programmer but got this code from differrent sites offering samples of vba code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top