Unable to extract text from an Outlook email message

saranbl

New Member
Outlook version
Outlook 2019 64-bit
Email Account
Office 365 Exchange
Hi,

I've tried the code from the link Use RegEx to extract text from an Outlook email message in Excel and also added the references as mentioned in the webpage but getting an error message. I am using Excel 365. I've the Outlook mail opened in the background.

1619799830681.png


Error is in the line Set olMail = Application.ActiveExplorer().Selection(1)

1619799873881.png


Full code -

Code:
Sub GetValueUsingRegEx()
 ' Set reference to VB Script library
 ' Microsoft VBScript Regular Expressions 5.5
 
    Dim olMail As Outlook.MailItem
    Dim Reg1 As RegExp
    Dim M1 As MatchCollection
    Dim M As Match
        
    Set olMail = Application.ActiveExplorer().Selection(1)
   ' Debug.Print olMail.Body
    
    Set Reg1 = New RegExp
    
    ' \s* = invisible spaces
    ' \d* = match digits
    ' \w* = match alphanumeric
    
    With Reg1
        .Pattern = "Request to change name on bill."
        .Global = True
    End With
    If Reg1.test(olMail.Body) Then
    
        Set M1 = Reg1.Execute(olMail.Body)
        For Each M In M1
            ' M.SubMatches(1) is the (\w*) in the pattern
            ' use M.SubMatches(2) for the second one if you have two (\w*)
            Debug.Print M.SubMatches(1)
            
        Next
    End If
    
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
You are running it in Excel?

This is the problem -
Set olMail = Application.ActiveExplorer().Selection(1)

you can try using Set olMail = Outlook.Application.ActiveExplorer().Selection(1) - if it doesn't work, try this:

This should work -
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Set olMail = olApp.ActiveExplorer().Selection(1)
 

saranbl

New Member
Outlook version
Outlook 2019 64-bit
Email Account
Office 365 Exchange
You are running it in Excel?

This is the problem -
Set olMail = Application.ActiveExplorer().Selection(1)

you can try using Set olMail = Outlook.Application.ActiveExplorer().Selection(1) - if it doesn't work, try this:

This should work -
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Set olMail = olApp.ActiveExplorer().Selection(1)

Hi Diane,

Tried both but still getting error. While I tried the second I am getting the below error

1619807792923.png


Code:
Sub GetValueUsingRegEx()
 ' Set reference to VB Script library
 ' Microsoft VBScript Regular Expressions 5.5

    Dim olApp As Outlook.Application
    Dim olMail As Outlook.MailItem
    Dim Reg1 As RegExp
    Dim M1 As MatchCollection
    Dim M As Match

    Set olMail = olApp.ActiveExplorer().Selection(1)
   ' Debug.Print olMail.Body

    Set Reg1 = New RegExp

    With Reg1
        .Pattern = "(\d{11}|\d{3}-\d{8})"
        .Global = True
    End With
    If Reg1.test(olMail.Body) Then

        Set M1 = Reg1.Execute(olMail.Body)
        For Each M In M1
            Debug.Print M.SubMatches(1)

        Next
    End If

End Sub
 
Similar threads
Thread starter Title Forum Replies Date
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0
M Unable to share 2019 calendar Using Outlook 0
D Outlook 2016 Unable to load Outlook data pst file Using Outlook 5
L Unable to Sync Web/Android MS To Do with Windows Outlook Tasks Using Outlook 3
A Unable to save recurring Meeting to Documents folder due to error Using Outlook 2
O Outlook 365 - suddenly unable to send using Gmail POP3 Using Outlook 10
S Outlook 2010 unable to change default font Using Outlook 7
Q Unable to Sync Quicken reminder with Outlook 2016 64Bit Using Outlook 1
S Unable to remove rule outlook 2010 Using Outlook 0
L Wierd Office 365 Contact unable to edit body of random contacts Using Outlook 5
X Unable to send an email from one account to another on same PC Using Outlook 2
Mark Foley Unable to subscribe to published calendar in Outlook 2010 Using Outlook 4
S Unable to Edit Contact Information in Certain Contact Folders Using Outlook 3
P Deleted Items - Unable to Iterate All of Items Outlook VBA and Custom Forms 1
G Unable to dismiss reminders from share point list calendar shared in Outlook Using Outlook 2
R Unable to install without email account Using Outlook 4
E Unable to open Outlook 2010 after adding new email account Using Outlook 4
A Outlook 2016- unable to have all subfolders expanded when opening outlook Using Outlook 11
avant-guvnor Unable to view certain emails in Outlook 2016 Using Outlook 16
M Unable to Configure Gmail Account in Outlook 2007 Using Outlook 1
D Unable to Send On Behalf of Another User Exchange Server Administration 0
J Unable to link email messages in BCM using a single microsoft office 365 account in outlook 2013 BCM (Business Contact Manager) 1
Tim King Outlook 365 unable to change accounts Using Outlook 0
R Unable to send mail using Cox.net Using Outlook 1
T Unable to 'Upload a file' using popup 'Browse' button Using Outlook 0
D Unable to add email address to contact Using Outlook 3
G Outlook calendar entry corrupted. Constant pop up when you open outlook. Unable to delete or remove. Using Outlook 2
O OL2000: Unable to create IMAP account Using Outlook 2
wallisellener Unable to reply/post/create new thread using Chrome BCM (Business Contact Manager) 5
Rupert Dragwater unable to change font sizes in some replies Using Outlook 3
C Unable to see meeting attendees Outlook 2010 Using Outlook 5
M Unable to email from Word or Excel Using Outlook 11
O Unable to check name. Using Outlook 3
T Unable to create contacts subfolder in EAS profile Using Outlook.com accounts in Outlook 6
UncleBill Unable to delete items from gmail IMAP Trash using Outlook 2010 Outlook VBA and Custom Forms 5
B Unable to search delegated mailfile Using Outlook 3
K Unable to activate QueryBuilder in Outlook 2010 (32bit) with Windows 7 (64bit) Using Outlook 1
C Unable to forward email with URL Using Outlook 2
R New computer, OL2010 unable to display shared calendar appointments Exchange Server Administration 3
S unable to send email from my second address (outlook 2007) Using Outlook 2
A "Unable to display the folder" error when accessing Group mailbox Using Outlook 0
C Outlook 2010, two exchange mailboxes configured.Unable to see 2ndrymeeting req Using Outlook 2
H Outlook 2010 unable to send iCloud account e-mail after iOS 7 upgrade Using Outlook 12
B Unable to apply rule on Exchange server functional account Exchange Server Administration 1
J Archives: Unable to locate emails Using Outlook 1
S Unable to send or reeceive email in new Exchange 2010 Exchange Server Administration 0
B Unable to Accept meetings that are already Tentatively accepted Using Outlook 2
M Unable to read HTML Using Outlook 2
A Unable to send with attachments in Outlook 2013 (Exchange ActiveSync) Using Outlook 2
F Outlook 2010 unable to delete emails from scanner Using Outlook 1

Similar threads

Top