开发者

Alternatives for replacing text selection under Outlook with custom HTML code

开发者 https://www.devze.com 2023-03-18 12:02 出处:网络
I need to replace an user selection while editing an Outlook e-mail with my custom HTML code. I tried two approaches:

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

  1. In HTML mode.
  2. Getting the Word.Range from the Word.Selection.
  3. Doing a range.Copy() to put it in the clipboard
  4. Convert inputString to outputString
  5. Replacing the selection with outputString with range.PasteSpecial(...)

Using the Open XML Format

  1. In HTML mode.
  2. Getting the Word.Range from the Word.Selection.
  3. Doing a range.Copy() to put it in the clipboard.
  4. Convert inputString to outputString (in Office Open XML format, knowing how to craft it to produce a specific HTML).
  5. Replacing the selection with outputString with range.InsertXML(...)

The issues are:

  1. When using the clipboard it crashes sometimes. There are references about checking the clipboard state but I think I've tried the known alternatives.
  2. To use the insertXML you need to have Microsoft Word installed. You can't do an insertXML 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消