Consulting

Results 1 to 10 of 10

Thread: Outlook 2010 - macro to modify all Hyperlinks in an email body before sending

  1. #1
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    5
    Location

    Outlook 2010 - macro to modify all Hyperlinks in an email body before sending

    I'm hopeful that someone here will know a good way to accomplish this. What I'm trying to do is create a macro that will modify all hyperlinks within an email (newly created message, ready to send). I know I can modify the .Hyperlinks field's Address attribute for a single selection, but would like to run it on an entire message.

    Thanks in advance for any advice!

  2. #2
    are you using word as your default email editor?

  3. #3
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    5
    Location
    Yes, thanks!

  4. #4
    you can try like

    Set msg = ActiveExplorer.Selection(1)
    If msg.GetInspector.EditorType = olEditorWord Then
        Set odoc = msg.GetInspector.WordEditor
        For Each h In odoc.Hyperlinks
            h.Address = "whatever"
        Next
    End If
    msg.Save
    i did not test this

  5. #5
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    5
    Location
    Thanks very much for your help. I tested it and when debugging it reports "Method 'Address' of object 'Hyperlink' failed" - any ideas about that error?

  6. #6
    did you test with a valid address (url)?

  7. #7
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    5
    Location
    I copied your code exactly, so the test was with "whatever" - but the hyperlinks in the message I was working from were correct. When I run the macro, it doesn't error but also doesn't change the hyperlinks. When I step through the macro using debugging, it gives me the error I put above.

    In case it helps any, before posting I had found someone's example code that would modify a hyperlink that you highlight to select... below is my slightly-modified version:

    Public Sub ModifyLink()
    If ActiveInspector.EditorType = olEditorText Then
    MsgBox "Can't add links to textual mail"
    Exit Sub
    End If


    Dim objHTM As Object
    Dim doc As Object
    Dim sel As Object
    Set objHTM = CreateObject("htmlfile")
    Set doc = ActiveInspector.WordEditor
    Set sel = doc.Application.Selection
    doc.Hyperlinks.Add Anchor:=sel.Range, _
    Address:="this%20has%20been%20disabled%20-%20please%20copy%20it%20to%20your%20browser", _
    SubAddress:="", _
    ScreenTip:="this%20has%20been%20disabled%20-%20please%20copy%20it%20to%20your%20browser", _
    TextToDisplay:=sel.Text
    End Sub

    And it works - if I highlight a hyperlink and then run the macro, it replaces the address with the invalid text. I'm looking to expand this to modify all hyperlinks in a message.

    Thanks again for your help.

  8. #8
    doc.Hyperlinks.Add Anc......
    without testing the code, i would say that the added hyperlink is overwriting the existing selection, not editing an existing hyperlink

    how are the hyperlinks inserted into the email body?

    the word editor window is not an inspector window, so can not the properties and methods of an inspector object
    i have tested this code on a new message using word editor
    Set odoc = GetObject(, "word.application").documents("untitled message")
        For Each h In odoc.Hyperlinks
            h.TextToDisplay = "xxx"
        Next
    note untitled message is only the name of the word document, while there is no subject, after the subject is assigned then the document name will change to the subject, there fore it maybe better to just use Set odoc = GetObject(, "word.application").activedocument, though i would normally recommend against doing so

  9. #9
    VBAX Newbie
    Joined
    Aug 2014
    Posts
    5
    Location
    Thanks for all of your help - I had a developer friend look at what you'd posted, and he tweaked it to work for me - in case anyone else looks for this, what works in Outlook 2010 is:Sub HyperLinkChange() Dim objOL As Outlook.Application Dim objNS As Outlook.NameSpace Dim objDoc As Word.Document Dim objSel As Word.Selection On Error Resume Next Set objOL = Application If objOL.ActiveInspector.EditorType = olEditorWord Then ' use WordEditor Set objDoc = objOL.ActiveInspector.WordEditor Set objNS = objOL.Session ' set current selection Set objSel = objDoc.Windows(1).Selection Dim tmpLink As Hyperlink For Each tmpLink In objDoc.Hyperlinks tmpLink.Address = "Hyper Link Removed. Please copy and paste into your browser to view" Next tmpLink End If Set objOL = Nothing Set objNS = NothingEnd SubThis macro will replace all existing hyperlinks in an email with links that say: "Hyper Link Removed. Please copy and paste into your browser to view"

  10. #10
    Quote Originally Posted by westconn1 View Post
    the word editor window is not an inspector window, so can not(sic) the properties and methods of an inspector object
    Just to muddy the waters a bit. Outlook 2010 does not use Word as e-mail editor The last version to do that was Word 2003. Outlook has its own Word-like editor (which uses a subset of Word) and this can be accessed as an Inspector Window, in fact it is the only way to edit the message body. The code that 'nohra's' pet developer produced (listed and corrected below) uses the inspector to edit the hyperlinks. The following will work with a message currently being created. If you want to edit a message that has already been created, it will need some changes.

    Sub HyperLinkChange()
    Dim objDoc As Object
    Dim tmpLink As Object
        On Error Resume Next
        If ActiveInspector.EditorType = olEditorWord Then
            ' use WordEditor Inspector
            Set objDoc = ActiveInspector.WordEditor
            For Each tmpLink In objDoc.Hyperlinks
                tmpLink.Address = "Hyper Link Removed. Please copy and paste into your browser to view"
            Next tmpLink
        End If
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •