Link to home
Start Free TrialLog in
Avatar of J P
J P

asked on

Outlook VBA, open selected mail item and click hyperlink contained within

I wish to open selected mail item in Outlook (using VBA within Outlook) and then click hyperlink labelled "Test".
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try
Sub macro()

    Set objOL = CreateObject("Outlook.Application")
    Set NewMail = objOL.ActiveInspector.currentItem
    res = NewMail.body
    
    Set regex = CreateObject("VBScript.RegExp")

    With regex
        .Global = False
        .MultiLine = False
        .IgnoreCase = True
        .Pattern = "HYPERLINK ""([^""]+?)""test"
    End With
    Set mtches = regex.Execute(res)
        If mtches.Count > 0 Then
            strHL = mtches(0).submatches(0)
            Set oIE = CreateObject("InternetExplorer.Application")
            oIE.navigate strHL, CLng(2048)
            oIE.Visible = True
            Do While oIE.Busy
                DoEvents
            Loop
        End If
End Sub

Open in new window

Regards
Avatar of J P

ASKER

Line 4: has error "Run Time error '91': Object variable or with block variable not set"
then try
Sub macro()

  '  Set objOL = CreateObject("Outlook.Application")
    Set NewMail = Application.ActiveExplorer.Selection.Item(1)
    NewMail.display
    res = NewMail.body
    
    Set regex = CreateObject("VBScript.RegExp")

    With regex
        .Global = False
        .MultiLine = False
        .IgnoreCase = True
        .Pattern = "HYPERLINK ""([^""]+?)""test"
    End With
    Set mtches = regex.Execute(res)
        If mtches.Count > 0 Then
            strHL = mtches(0).submatches(0)
            Set oIE = CreateObject("InternetExplorer.Application")
            oIE.navigate strHL, CLng(2048)
            oIE.Visible = True
            Do While oIE.Busy
                DoEvents
            Loop
        End If
End Sub

Open in new window

Avatar of J P

ASKER

line 20: error "Run time error '462': the remote server machine does not exist or is unavailable"
the link is directed to what?
Avatar of J P

ASKER

The first code worked fine when the email was already open if that helps?

It did not work correctly when I was viewing it in the preview pane.
then try
Sub macro()

  '  Set objOL = CreateObject("Outlook.Application")
    Set NewMail = Application.ActiveExplorer.Selection.Item(1)
    NewMail.display
    res = NewMail.body
    
    Set regex = CreateObject("VBScript.RegExp")

    With regex
        .Global = False
        .MultiLine = False
        .IgnoreCase = True
        .Pattern = "HYPERLINK ""([^""]+?)""test"
    End With
    Set mtches = regex.Execute(res)
        If mtches.Count > 0 Then
            strHL = mtches(0).submatches(0)
            Set oIE = CreateObject("InternetExplorer.Application")
            oIE.navigate strHL
            oIE.Visible = True
            Do While oIE.Busy
                DoEvents
            Loop
        End If
End Sub

Open in new window

Avatar of J P

ASKER

Line 21: oIE.Visible = True

Get error "Run time error '-2147417848 (80010108)': Automation Error The object invoked has disconnected from its clients."
then try
Sub macro()

  '  Set objOL = CreateObject("Outlook.Application")
    Set NewMail = Application.ActiveExplorer.Selection.Item(1)
    NewMail.display
    res = NewMail.body
    
    Set regex = CreateObject("VBScript.RegExp")

    With regex
        .Global = False
        .MultiLine = False
        .IgnoreCase = True
        .Pattern = "HYPERLINK ""([^""]+?)""test"
    End With
    Set mtches = regex.Execute(res)
        If mtches.Count > 0 Then
            strHL = mtches(0).submatches(0)
            Set oIE = CreateObject("InternetExplorer.Application")
            oIE.navigate strHL, CLng(2048)

            Do While oIE.Busy
                DoEvents
            Loop
            oIE.Visible = True
        End If
End Sub

Open in new window

Avatar of J P

ASKER

Line 22: Do While oIE.busy

has the same error

 "Run time error '-2147417848 (80010108)': Automation Error The object invoked has disconnected from its clients"
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of J P

ASKER

Thanks it worked!
Avatar of J P

ASKER

Rgonzo1971 - following on from the correct code, how would I use GetElementByID for the loaded webpage ?
Pls open a new question