PDA

View Full Version : Outlook 2010 - macro to modify all Hyperlinks in an email body before sending



nohra
08-05-2014, 12:48 PM
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!

westconn1
08-05-2014, 02:18 PM
are you using word as your default email editor?

nohra
08-05-2014, 02:29 PM
Yes, thanks!

westconn1
08-06-2014, 04:50 AM
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.Savei did not test this

nohra
08-06-2014, 11:11 AM
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?

westconn1
08-06-2014, 02:15 PM
did you test with a valid address (url)?

nohra
08-06-2014, 09:48 PM
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.

westconn1
08-07-2014, 03:50 AM
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"
Nextnote 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

nohra
09-16-2014, 01:19 PM
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"

gmayor
09-16-2014, 11:13 PM
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