I have a question for “run-time error ‘4198’: Command failed”.. I’ve been using this code for 2 years with no issue but it now doesn’t work.

Anonymous
2024-04-16T12:53:42+00:00

Sub MACRO_NAME_Email()

Sheets("Email").Select

Dim olApp As Outlook.Application
    Dim olEmail As Outlook.MailItem
    Dim ilInsp As Outlook.Inspector
    Dim wdDoc As Word.Document
    Dim strGreeting As String
    Dim strClosing As String
    
    Set olApp = New Outlook.Application
    Set olEmail = olApp.CreateItem(olMailItem)
    
    With olEmail
    
    .BodyFormat = olFormatHTML
        .Display
        
        .To = ThisWorkbook.Sheets("Email").Range("D2").Value
        .CC = ThisWorkbook.Sheets("Email").Range("D3").Value
        .Subject = ThisWorkbook.Sheets("Email").Range("D4").Value
        
        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor
        wdDoc.Range.InsertAfter strGreeting
        
Sheets("Email").Select
    Range("D5:I75").Copy
    
    wdDoc.Range(Len(strGreeting), Len(strGreeting)).Paste
    
    End With
    
End Sub

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2024-04-16T16:09:31+00:00

    Your code works at my side.

    The "run-time error ‘4198’: Command failed" error message usually occurs when there is an issue with the clipboard.

    You may try this one.

    wdDoc.Range(Len(strGreeting), Len(strGreeting)).PasteSpecial DataType:=wdPasteText

    0 comments No comments
  2. Anonymous
    2024-04-17T12:17:47+00:00

    It worked once, but after another try, it generated the same error.

    0 comments No comments
  3. Anonymous
    2024-04-17T13:08:24+00:00

    You may try adding these lines.

    =========================

    Set olEmail = Nothing

    Set olApp = Nothing

    Set ilInsp = Nothing

    Set wdDoc = Nothing

    This will release all the set objects and free up memory resources.

    0 comments No comments