| Code level: intermediate Code area: Outlook Expert Techniques Printer Friendly Version | ||
| Title: Insert text with WordMail as the editor | ||
| Description: This code sample forwards a message and then inserts text at the top of the item. The formatting in the forwarded message remains intact. Add a reference to the Microsoft Word to your project. | ||
| Date: 16-Jun-2004 00:50 | ||
| Code level: intermediate | ||
| Code area: Outlook Expert Techniques | ||
| Posted by: Sue Mosher | ||
| Body: |
||
| All 13comments |
| Page [ 1 2 Next >> ] | ||
|
|
Sue Mosher
16-Jun-2004 07:54
For an example of using WordMail to format text in a message, see http://www.outlookcode.com/codedetail.aspx?id=433 Another approach if WordMail is not the editor, if you're working with an item other than a message or post, or if you want to avoid security prompts with Outlook 2002 SP3 is to use the Redemption library , which offers a SafeInspector object ( http://www.dimastr.com/redemption/safeinspector.htm ) that you can use to paste and format text. |
|
|
|
Sue Mosher
16-Sep-2004 07:47
Here's a version of the same code, adapted to run from a command button on a custom Outlook form: Sub CommandButton1_Click() Dim strText ' As String Dim objInsp 'As Outlook.Inspector Dim objDoc ' As Word.Document Const olEditorWord = 4 On Error Resume Next strText = "This is my inserted text." & vbCrLf Set objInsp = Item.GetInspector If objInsp.EditorType = olEditorWord Then ' next statement triggers security prompt ' in Outlook 2002 SP3 Set objDoc = objInsp.WordEditor objDoc.Characters(1).InsertBefore strText Else MsgBox "Cannot insert text in a formatted message unless Word is the editor" End If Set objInsp = Nothing Set objDoc = Nothing End Sub |
|
|
|
Kurtis Lininger
02-Oct-2007 12:00
One change I would suggest: If word is not the editor, you CAN insert text in a formatted message. Set email = originalEmail.Reply email.HTMLBody = strText & email.HTMLBody |
|
|
|
Sue Mosher
03-Oct-2007 11:11
Kurtis, you can't simply prefix HTMLBody with new text. You must insert the fully tagged HTML content (strText) inside the <body> and </body> tags in the existing HTMLBody. Easy to do if you want text at the end; more involved if you want it at the beginning. I have examples of both in my Outlook 2007 book. |
|
|
|
Alan
27-Nov-2007 12:08
I'm using Outlook 2003 SP3 on Windows XP. When I step through the ForwardInsertText macro in the VBA debugger in Outlook, it exits from the debugger at the line: Set objInsp = objMsg.GetInspector i.e. it doesn't step to the next line. Any ideas why this is happening? Apart from that, the rest of the code seems to be executed and the forwarded message is displayed with the newly inserted text. thanks, |
|
|
|
Sue Mosher
27-Nov-2007 12:52
Alan, try commenting out the On Error Resume Next statement to get a prompt about the error. |
|
|
|
Alan
27-Nov-2007 15:49
I commented out the On Error line, however no error is displayed. It runs exactly as before. I've discovered that I have to explicitly set a breakpoint on the next line: If objInsp.EditorType = olEditorWord Then and then it breaks on that line. The same thing happens with the line: objMsg.Display i.e. the debugger steps out of the subroutine instead of stepping to the next line. I have to set a breakpoint on the next line: Set objInsp = Nothing and then it breaks on that line. |
|
|
|
Sue Mosher
27-Nov-2007 17:41
I'm not sure I understand what that tells us. If you set a breakpoint, the code always will break on that statement. |
|
|
|
Alan
28-Nov-2007 05:42
The point is, if I am stepping through the code line by line I shouldn't have to set a breakpoint on a specific line to make it break on that line. |
|
|
|
Sue Mosher
11-Dec-2007 18:27
The debugger stepping out of the routine would usually indicate an unhandled error, I think. |
|
| Page [ 1 2 Next >> ] | ||
| Post your comment name email |
