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".
J PAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
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
0
J PAuthor Commented:
Line 4: has error "Run Time error '91': Object variable or with block variable not set"
0
Rgonzo1971Commented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

J PAuthor Commented:
line 20: error "Run time error '462': the remote server machine does not exist or is unavailable"
0
Rgonzo1971Commented:
the link is directed to what?
0
J PAuthor Commented:
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.
0
Rgonzo1971Commented:
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

0
J PAuthor Commented:
Line 21: oIE.Visible = True

Get error "Run time error '-2147417848 (80010108)': Automation Error The object invoked has disconnected from its clients."
0
Rgonzo1971Commented:
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

0
J PAuthor Commented:
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"
0
Rgonzo1971Commented:
then try (with reference " microsoft Internet Controls"  ticked)
Sub macro()
Dim regex
    Set objOL = CreateObject("Outlook.Application")
    Set NewMail = objOL.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 = New InternetExplorerMedium
            oIE.navigate strHL, CLng(2048)
            oIE.Visible = True

        End If
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
J PAuthor Commented:
Thanks it worked!
0
J PAuthor Commented:
Rgonzo1971 - following on from the correct code, how would I use GetElementByID for the loaded webpage ?
0
Rgonzo1971Commented:
Pls open a new question
0
J PAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.