Click here to Skip to main content
14,869,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 here for your refernce..

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
   .Text = "ASA[0-9][0-9][0-9][0-9][a-z][a-z]"
   .Forward = True
   .Wrap = wdFindAsk
   .MatchWildcards = True
  End With
  objSelection.Hyperlinks.Add Anchor:=objSelection.Range, _
  Address:="" & objSelection.Text, _
  'Copies contents to email item from word document
  objMail.HTMLBody = objDoc.Range(0, objDoc.Range.End)
  Set objMail = Nothing
 End Sub
Also, 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.
Updated 20-Jun-11 10:38am

1 solution

You need to loop until Execute() is true.
Do while objSelection.Find.Execute()
    'code to make changes in text

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900