Hyperlink formatting lost after replacement in outlook

Status
Not open for further replies.

outlookmacro

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
I have written a code which replaces the text of certain format into a hyperlink. This code is invoked by a rule during an Incoming email.

Incoming email -> copy the email to word editor -> make necessary changes -> copy from word editor to outlook mail item(replaced hyperlinks gets lost in mail item, while newly added text reamins intact)

My code is below for your refernce..

this code replaces only the first occurrence of the needed text and does not replace others.

Please help solve these problems. Thank you...

I have tried out different options and still not able to get it work.
 

outlookmacro

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Sub IncomingHyperlink(MyMail As MailItem)

Dim strID As String

Dim Body As String

Dim objMail As Outlook.MailItem

Dim myObject As Object

Dim myDoc As Word.Document

Dim mySelection As Word.Selection

strID = MyMail.EntryID

Set objMail = Application.Session.GetItemFromID(strID)

'Creates word application

Set objWord = CreateObject("Word.Application")

objWord.Visible = True

Set objDoc = objWord.Documents.Add()

Set objSelection = objWord.Selection

'Copies contents of email into word document

objSelection.TypeText "GOOD" & objMail.HTMLBody

With objSelection.Find

> ClearFormatting

> Text = "ASA[0-9][0-9][0-9][0-9][a-z][a-z]"

> Forward = True

> Wrap = wdFindAsk

> MatchWildcards = True

End With

objSelection.Find.Execute

objSelection.Hyperlinks.Add Anchor:=objSelection.Range, _

Address:="http://www.code.com/" & objSelection.Text, _

TextToDisplay:=objSelection.Text

'Copies contents to email item from word document

objMail.HTMLBody = objDoc.Range(0, objDoc.Range.End)

objMail.Save

Set objMail = Nothing

End Su
I have written a code which replaces the text of certain format into a hyperlink. This code is invoked by a rule during an Incoming email.

Incoming email -> copy the email to word editor -> make necessary changes -> copy from word editor to outlook mail item(replaced hyperlinks gets lost in mail item, while newly added text reamins intact).
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I don't see a loop for the selection.find. Its stopping after the first find.




I use Selection.WholeStory in one word macro to solve the problem. On another i use a do while selection.find.execute and then loop. The do while requires me to to make sure I'm at the top of the page or it will skip the things above it.
 

outlookmacro

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
I don't see a loop for the selection.find. Its stopping after the first find.

Thanks for your reply.

I use Selection.WholeStory in one word macro to solve the problem. On another i use a do while selection.find.execute and then loop. The do while requires me to to make sure I'm at the top of the page or it will skip the things above it.
I tried using Selection.WholeStory but it does not work. Can you provide me a sample code for the second option.

Also, do you have any idea, why the text when copied to outlook object loses its replaced hyperlink format while other changes remain intact in outlook object

Help on this is greatly appreciated.

Thanks
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
My selection.wholestory code is






Sub name()
Selection.WholeStory

With Selection.Find
.Text = "words"
.Replacement.Text = "different word"
End With
Selection.Find.Execute Replace:=wdReplaceAll




End sub






And a different macro with a loop - i'm deleting an entire paragragh here. the first part finds the para that begind with a certain word, selects it then backspaces.


Private Sub NAME()




Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"




Selection.Find.ClearFormatting
With Selection.Find
.Text = "words"
.Forward = True
End With




Do While Selection.Find.Execute
Selection.Paragraphs(1).Range.Select
Selection.TypeBackspace


Loop




End Sub
 

outlookmacro

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Thanks for the helper code. In the first code, how to add the code with just hyperlink.

For Eg: If ASA1234yy is the found pattern, it should be replaced with just ASA1234yy, but clicking on it should take me to www.code.com/ASA1234yy/xyz.

Also, am I copying the contents of the word document to the outlook item in a right fashion?(because I am not able to get the replaced hyperlink in the word document)

Thanks a lot!
 
Status
Not open for further replies.
Top