2

I'm pretty new to writing VBA scripts. Actually, I'm trying to write a small VBA script which will go through the newly received email, scan the body of the message and open all the urls(I'm targetting this script for a message which always contains a single URL) in a web browser like IE or FF or Chrome. I tried looking for similar Qs in SO and on Google. I found one here: How to launch a URL when an email arrives which is about how to open a URL when an email arrives. But it is always opening a fixed URL and not by going through the message body and picking the URL from there. Also, I found this: Using VB/VBA to search Outlook messages and extract specific data into Excel worksheet which go through the message body to pick/search something. In my scenario, I want to consolidate both but I'm not able to come up with a basic script. Could someone give me pointers in consolidating the two and help me achieve my target of opening URLs from message body whenever a new mail arrives in Outlook mailbox. Thanks in advance.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
alphaGeek
  • 442
  • 1
  • 7
  • 19

2 Answers2

1
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
niton
  • 8,771
  • 21
  • 32
  • 52
0

Alpha, I thing that is little unsafe, even if your rule if only people from a particular domain for. That is a good idea, when you check urlstring with variable you create (like link only from local intranet).

if splitWord like "my_company_internat_url" then '...
Oskar Shon
  • 32
  • 7