-1

I am very new to this so please be gentle. I am using outlook 2010 and I need a vba script to automatically locate and action the first hyperlink in an email. I can use the outlook mailbox rules to select this script but I cannot write it. Any help will be appreciated

I was given this but it only opens URL's in the email but not the first hyperlink

Sub LaunchURL(itm As MailItem)

    Dim bodyString As String
    Dim bodyStringSplitLine
    Dim bodyStringSplitWord
    Dim splitLine
    Dim splitWord

    bodyString = itm.Body
    bodyStringSplitLine = Split(bodyString, vbCrLf)

    For Each splitLine In bodyStringSplitLine
        bodyStringSplitWord = Split(splitLine, " ")

        For Each splitWord In bodyStringSplitWord
            If Left(splitWord, 7) = "http://" Then
                Shell ("C:\Program Files\Internet Explorer\IEXPLORE.EXE" & " " & splitWord)
            End If
        Next

    Next

    Set itm = Nothing
End Sub

Private Sub test()
    Dim currItem As MailItem
    Set currItem = ActiveInspector.currentItem
    LaunchURL currItem
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Could you explain what you've tried so far and the specific problem(s) you've encountered? This is a question-and-answer site for professional and enthusiast programmers and you'll most likely find the folks here unwilling to just write code to solve your problem as that would be equivalent to doing a skilled job for free. – Aiken Jul 30 '14 at 07:46
  • I was given this but it opens up existing URL's in the email, not the first hyperlink – user1734455 Jul 30 '14 at 11:12
  • Right, to solve your particular problem you need to add `Exit Sub` underneath `Shell`, that will cause the subroutine to exit immediately after the first URL is opened. However this problem (which is essentially "How do I end a subroutine/function early?") is incredibly trivial and unlikely to be of any use to future users of StackOverflow so I wouldn't count on your question sticking around, which is why I've just left this comment instead of a full answer. – Aiken Jul 30 '14 at 12:55

1 Answers1

1

The LaunchURL code is specifically for use where the URL is visible, like this VBA script for outlook to automatically open URLs from message body in a web browser, for all incoming mails, such as it in in plain text mail.

Where the link is hidden, you could select the text, take it to Word where there is the Hyperlink.Follow Method


Edit: Found it simpler not to go to Word.

Public Sub FollowLinkAddress(itm As Outlook.MailItem)

Dim oDoc As Object
Dim h

Set itm = ActiveInspector.CurrentItem

If itm.GetInspector.EditorType = olEditorWord Then

    Set oDoc = itm.GetInspector.WordEditor

    For Each h In oDoc.Hyperlinks
        h.Follow
    Next

End If

End Sub
Community
  • 1
  • 1
niton
  • 8,771
  • 21
  • 32
  • 52
  • Thankyou for the explanation and direction but I have no idea how to do that. Is it possible to alter the existing script to reflect these instructions? – user1734455 Jul 30 '14 at 22:44