Click here to Skip to main content
15,896,063 members

formatting lost for new replaced hyperlinks(word->outlook)

Member 7746250 asked:

Open original thread
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
   .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 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.
Tags: Visual Basic, VBScript, VBA, Microsoft Word, Outlook

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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