Alternatives for replacing text selection under Outlook with custom HTML code
I need to replace an user selection while editing an Outlook e-mail with my custom HTML code.
I tried two approaches:
Using the Clipboard
- In HTML mode.
- Getting the
Word.Range
from theWord.Selection
. - Doing a
range.Copy()
to put it in the clipboard - Convert
inputString
tooutputString
- Replacing the selection with
outputString
withrange.PasteSpecial(...)
Using the Open XML Format
- In HTML mode.
- Getting the
Word.Range
from theWord.Selection
. - Doing a
range.Copy()
to put it in the clipboard. - Convert
inputString
tooutputString
(in Office Open XML format, knowing how to craft it to produce a specific HTML). - Replacing the selection with
outputString
withrange.InsertXML(...)
The issues are:
- When using the clipboard it crashes sometimes. There are references about checking the clipboard state but I think I've tried the known alternatives.
- To use the
insertXML
you need to have Microsoft Word installed. You can't do aninsertXML
with Outlook instal开发者_运维问答led alone. Look at: Impact of deploying Outlook 2007 without Word 2007 for further information.
Do you know more alternatives? I can think of adding the elements myself like using InsertParagraph
, but I am not sure if I can add hyperlinks or will experience similar issues.
According to msdn, the HTMLEditor has been deprecated in Outlook 2007 - see the fourth section down, "HTML Editor".
Technically, you can still access the HTML through MailItem.HTMLBody
, but it's just a read/write string value so you wouldn't be able to find out what portion of the text is highlighted directly.
Using MailItem.getInspector.WordEditor
, you could find out what specific text is highlighted and then find and replace it in the HTMLBody via vba's Replace
, surrounded by your html.
Considering how convoluted that is, you may want to use WordEditor
to make your formatting changes directly. I didn't see anything in the link you provided that would indicate the WordEditor
object isn't available in a standalone Outlook 2007 installation, but I have the full office suite so I can't test it myself. this site describes how to do it with the WordEditor
.
精彩评论