Hi
I have vba to perform a mailmerge:
I get a runtime error 4198 command failed at the line in red.
I have checked the word 14 reference library is checked, and the code is compiling ok.
I would be grateful for any suggestions as to what may be wrong.
Thanks
Kev
I have vba to perform a mailmerge:
Code:
Private Sub Command41_Click()
DoCmd.SetWarnings False
DoCmd.Close acForm, "frmWriteToClient", acSaveYes
Dim mypath As String
Dim mypath3 As String
Dim Wordpath As String
Dim sDBPath As String
Dim oApp As Word.Application
Dim ThisDB As String
Dim oWord As Word.Document
Dim oMainDoc As Word.Document
If (Me.LeadAuthority = "A1") Then
Wordpath = Environ("office") & "\winword.exe"
mypath = Left$(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir$(CurrentDb.Name)))
[COLOR=red]mypath3 = mypath & "LetterTemplates\ClientLetterADC.docx"[/COLOR]
ThisDB = CurrentDb.Name
Set oApp = CreateObject("Word.Application")
Set oWord = oApp.Documents.Open(FileName:=mypath3)
oApp.Visible = True
With oWord.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = ThisDB
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [tblWriteToClient]"
End With
With oWord
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
End With
oApp.Activate
oApp.Documents.Parent.Visible = True
oApp.Application.WindowState = 1
oApp.ActiveWindow.WindowState = 1
oWord.Close savechanges:=False
DoCmd.SetWarnings True
DoCmd.Close acForm, "frmWriteToClientCheck", acSaveNo
Else
Wordpath = Environ("office") & "\winword.exe"
mypath = Left$(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir$(CurrentDb.Name)))
mypath3 = mypath & "LetterTemplates\ClientLetterWBC.docx"
ThisDB = CurrentDb.Name
Set oApp = CreateObject("Word.Application")
Set oWord = oApp.Documents.Open(FileName:=mypath3)
oApp.Visible = True
With oWord.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = ThisDB
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [tblWriteToClient]"
End With
With oWord
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
End With
oApp.Activate
oApp.Documents.Parent.Visible = True
oApp.Application.WindowState = 1
oApp.ActiveWindow.WindowState = 1
oWord.Close savechanges:=False
DoCmd.SetWarnings True
DoCmd.Close acForm, "frmWriteToClientCheck", acSaveNo
End If
I get a runtime error 4198 command failed at the line in red.
I have checked the word 14 reference library is checked, and the code is compiling ok.
I would be grateful for any suggestions as to what may be wrong.
Thanks
Kev