mail mergve vba runtime 4198 error

krowe

Registered User.
Local time
Yesterday, 22:48
Joined
Mar 29, 2011
Messages
159
Hi

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
 
If that line in red is the one that was highlighted by debugger, you have a far deeper problem than you think. The highlighted line doesn't do ANYTHING to an external object, but that is what the particular error code suggests.

I could see it failing a few lines down where you have:

Set oWord = oApp.Documents.Open(FileName:=mypath3)

Is that highlighted line REALLY the place that dies? Because if it is, Access itself is hosed in terms of its pseudocode execution library. I tend to doubt that it is because if that level of Access code is hosed, you shouldn't be able to start the database and go far enough to get that error.

A simple test is to put a breakpoint on the "mypath = " line that precedes it, then single-step through and use debug.print from the immediate window to see what is in your string variables.
 
hi

I did that and it works fine when i step through the process.

Could it be that the vba is trying to use the o.word before it has had a chance to open?

if so how can I resolve this??
 
oh, and ive just checked, you are spot on it does fail at the line
Set oWord = oApp.Documents.Open(FileName:=mypath3)

if i just run the code without stepping through.

I have tried putting in a DoEvents command in to allow time for word to open, but it hasnt helped :(
 
Sorry to bump this thread, I know I shouldn't but this error is effecting every mailmerge script I have in the database now. I think it is to do with the antique computers we have here.

Does anyone have any ideas how I might get around the problem.

Thanks

Kev
 
Only a thought.
A loop with a DoEvents checking if the oApp/oWord is set + an error handling.
 
That sounds like an option, but i really wouldnt know where to start with that. How would I check if oApp and oWord are set?
 
Try, (without error handling):
Code:
    Do
      Set oWord = oApp.Documents.Open(FileName:=mypath3)
      DoEvents
    Loop Until Not oWord Is Nothing
 
Sorry, that didn't work, still get the error when i run it, but goes through fine if i step through :banghead:
 
Ok - then try to place an error handling in the code.
Code:
   Private Sub Command41_Click()  
    On Error GoTo ErrorHandling  
    DoCmd.SetWarnings False
     .. ..
     .....
    Exit Sub
   ErrorHandling:
   If Err.Number = 4198 Then
    DoEvents
    Resume  
  Else
    MsgBox (Err.Number) 
 End If
End Sub

Please show us a Print Screen from the error massage (not number, but message).
 
Hi

That seems to be working fine with the error handling in.

Thanks so much for your help!

Kev
 

Users who are viewing this thread

Back
Top Bottom