VBA script for outlook to automatically open URLs from message body in a web browser, for all incoming mails

风格不统一 提交于 2019-12-19 10:17:26

问题


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.


回答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



回答2:


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 '...


来源:https://stackoverflow.com/questions/13011593/vba-script-for-outlook-to-automatically-open-urls-from-message-body-in-a-web-bro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!