locked
Outlook VBA syntax to get email body as string RRS feed

  • Question

  • I simply wish to extract the body of an email as a string using VBA.  With a single test email selected (just 24 characters in the body), the following code shows the result correctly for sText in the watch window:

    Sub MailItemContent()

        Dim olItem As Outlook.MailItem
        Dim sText As String
        
        For Each olItem In Application.ActiveExplorer.Selection
            sText = olItem.Body
        Next olItem
       
    End Sub

    So what error have I made in the syntax (or is it grammar?) in the following code?

    Sub MailItemContent2()

        Dim olItem As Outlook.MailItem
        Dim sText As String
        
        olItem = Application.ActiveExplorer.Selection
        sText = olItem.Body
     
    End Sub

    This returns an error "Object variable or With block variable not set".  If I change the olItem line to "Set olItem = Application.ActiveExplorer.Selection" I get an error "Type mismatch".

    I have looked through Sue Mosher's excellent book "Microsoft Outlook 2007 Programming", but can't find the right guidance therein.   (If only Outlook had a macro recorder as Excel / Word do, it would be much easier to cheat and copy its code!)

    Any help much appreciated.


    Friday, October 12, 2012 10:00 AM

Answers

  • The olItem variable is an object, so you have to use the keyword Set to assign it a value.

    The selection might contain multiple items, so you have to refer to a specific item. I assume you'll want to look at the first selected item.

    Sub MailItemContent2()
        Dim olItem As Outlook.MailItem
        Dim sText As String
    
        Set olItem = ActiveExplorer.Selection.Item(1)
        sText = olItem.Body
        ...
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by AndyColRomsey Friday, October 12, 2012 11:02 AM
    Friday, October 12, 2012 10:17 AM

All replies

  • The olItem variable is an object, so you have to use the keyword Set to assign it a value.

    The selection might contain multiple items, so you have to refer to a specific item. I assume you'll want to look at the first selected item.

    Sub MailItemContent2()
        Dim olItem As Outlook.MailItem
        Dim sText As String
    
        Set olItem = ActiveExplorer.Selection.Item(1)
        sText = olItem.Body
        ...
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by AndyColRomsey Friday, October 12, 2012 11:02 AM
    Friday, October 12, 2012 10:17 AM
  • The selection can be any thing from MailItem,contactitem etc.Is the selection is a mailitem ?

    Best Regards,
    Asadulla Javed, Kolkata
    ---------------------------------------------------------------------------------------------
    Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer”if it solves the issue.

    Friday, October 12, 2012 10:20 AM
    Answerer
  • Hans

    Many thanks.  That sorted the problem out.

    Andy C

    Friday, October 12, 2012 11:05 AM