Outlook VBA script "object variable or with block not set" error

SK 0 Reputation points
2023-02-10T15:00:48.7133333+00:00

Hello. Total beginner at VBA and I've been using an Outlook reply for a while with a script similar to one archived here that creates a standard reply to an email: https://social.technet.microsoft.com/forums/office/en-US/04a4a2e9-4124-4d01-a950-dca8a9746a96/vba-script-for-autotext-in-ms-outlook

The standard reply used to look like:

Hi Sender, This is done

But now an error message "object variable or with block not set" pops up and the script doesn't run. The debug points to Set objDoc and Set objSel lines. I am running Windows 10, Outlook for MS 365 MSO Version 2211. Do you know how this can be fixed?

Thank you!

Sub standardResponse()
    Set objDoc = Application.ActiveInspector.WordEditor
    Set objSel = objDoc.Windows(1).Selection

    Dim Msg As Outlook.MailItem
    Set Msg = ActiveExplorer.Selection.Item(1)

    Dim ReplyName As String

    If InStr(1, Msg.SenderName, ",") Then
        ReplyName = Right$(Msg.SenderName, Len(Msg.SenderName) - InStr(1, Msg.SenderName, ","))
    Else
        ReplyName = Left$(Msg.SenderName, InStr(1, Msg.SenderName, " ") - 1)
    End If

    objSel.TypeText Text:="Hi " & ReplyName & "," & vbCrLf & "This is done"

End Sub
Outlook
Outlook
A family of Microsoft email and calendar products.
4,005 questions
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
5,279 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 54,811 Reputation points
    2023-02-10T16:06:58.07+00:00

    I'm not heavy into Outlook addins but it seems to me that you don't really need to use the code that is blowing up. Starting on the line Set Msg = you're getting the active mail item. Note that this could be nothing potentially so you should handle that case. But given that mail item you already have access to the body of the message so you can set it.

    Just guessing at what the code might look like here.

    Sub standardResponse()
        Dim Msg As Outlook.MailItem
        If (Not ActiveExplorer Is Nothing) AndAlso (Not ActiveExplorer.Selection Is Nothing) AndAlso (ActiveExplorer.Selection.Count > 0) Then 
           Set Msg = ActiveExplorer.Selection.Item(1)
        End If
        If (Msg Is Nothing) Then
           Return 
        End If
        Dim ReplyName As String
    
        If InStr(1, Msg.SenderName, ",") Then
            ReplyName = Right$(Msg.SenderName, Len(Msg.SenderName) - InStr(1, Msg.SenderName, ","))
        Else
            ReplyName = Left$(Msg.SenderName, InStr(1, Msg.SenderName, " ") - 1)
        End If
    
        Msg.Body = "Hi " & ReplyName & "," & vbCrLf & "This is done"
    
    End Sub
    

  2. SK 0 Reputation points
    2023-02-17T02:29:03.95+00:00

    Found a solution and combined it with the earlier script to get the sender name. Appears to be working now. Thank you!

    Sub standardResponse()
        Dim olItem As Outlook.MailItem
        Dim olReply As MailItem
    
    Dim Msg As Outlook.MailItem
    Set Msg = ActiveExplorer.Selection.Item(1)
    
    Dim ReplyName As String
    
    If InStr(1, Msg.SenderName, ",") Then
         ReplyName = Right$(Msg.SenderName, Len(Msg.SenderName) - InStr(1, Msg.SenderName, ","))
    Else
         ReplyName = Left$(Msg.SenderName, InStr(1, Msg.SenderName, " ") - 1)
    End If
    
        For Each olItem In Application.ActiveExplorer.Selection
            Set olReply = olItem.ReplyAll
            olReply.HTMLBody = "Hi " & ReplyName & ",<br><br>" & "It is done<br><br>" & "Thanks," & olReply.HTMLBody
            olReply.Display
            
        Next olItem
    End Sub
    
    0 comments No comments

  3. SK 0 Reputation points
    2023-02-17T02:32:39.27+00:00
    Sub standardResponse()
        Dim olItem As Outlook.MailItem
        Dim olReply As MailItem
    
    Dim Msg As Outlook.MailItem
    Set Msg = ActiveExplorer.Selection.Item(1)
    
    Dim ReplyName As String
    
    If InStr(1, Msg.SenderName, ",") Then
         ReplyName = Right$(Msg.SenderName, Len(Msg.SenderName) - InStr(1, Msg.SenderName, ","))
    Else
         ReplyName = Left$(Msg.SenderName, InStr(1, Msg.SenderName, " ") - 1)
    End If
    
        For Each olItem In Application.ActiveExplorer.Selection
            Set olReply = olItem.ReplyAll
            olReply.HTMLBody = "Hi " & ReplyName & ",<br><br>" & "It is done<br><br>" & "Thanks," & olReply.HTMLBody
            olReply.Display
            
        Next olItem
    End Sub
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.