Log in

View Full Version : [SOLVED:] How to keep the HTML-formatting when adding a message to a forwarded message



vegard_fv
03-03-2016, 03:04 AM
Hi guys,

When using the beneath code to forward emails in Outlook VBA, everything works perfectly, and the HTML-formatting is kept. But what I would like to do, is to write a text to the reciever on top of the email. That's when my worries begin.

I've tried adding the following lines to the code:

fwdbody = fwd.Body
With fwd
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>Hi there, please see the beneath forwarded email that I would like you to respond to"</BODY></HTML> _
& fwdbody

This does work technically - BUT it changes the sweet HTML-formatting, and turning the forwarded message into a big soup of black, ugly text :banghead:
What I would LOVE, is to KEEP the formatting as it looks when using the beneath code, but be able to ADD a message on top KEEPING the pretty HTML-look :guitar2:
Any hints?


Sub emailforwarding()
Dim olApp As New Outlook.Application
Dim olExp As Outlook.Explorer
Dim olSel As Outlook.Selection
Dim olNameSpace As Outlook.NameSpace
Dim olArchive As Outlook.Folder
Dim intItem As Integer
Dim fwdbody As String
Set olExp = olApp.ActiveExplorer
Set olSel = olExp.Selection
Set olNameSpace = olApp.GetNamespace("MAPI")
'Starts a loop for each selected email as item
For intItem = 1 To olSel.Count
Set fwd = olSel.Item(intItem).Forward
'Add email adress - I do it with adding a variable
fwd.Recipients.Add 'write your email-adress here as text string
fwd.Send
Next intItem
End Sub

gmayor
03-04-2016, 04:15 AM
If you want to forward a message and add text to the start, whilst retaining the format, you must use the document inspector e.g. as follows. This will crate a forward message for the currently selected message with the named text string at the top and (if you have a signature associated with the account for forwards) it will include the signature after the text string.


Option Explicit

Sub ForwardEmailWithText()
Dim olOutMail As Outlook.MailItem
Dim olItem As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object

Const strText As String = "This is the added text for the start of the message: " & vbCr & vbCr

On Error Resume Next
Set olItem = ActiveExplorer.Selection.Item(1)
Set olOutMail = olItem.Forward
With olOutMail
.BodyFormat = olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
oRng.Text = strText
oRng.collapse 0
.Display
End With
lbl_Exit:
Set olOutMail = Nothing
Set olItem = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
Exit Sub
End Sub

vegard_fv
03-04-2016, 04:18 AM
Ahh... great. Thank you so much!I'm grateful! :friends:

I've tested it now, and it works perfectly! Will also read more about, and understand, the document inspector object. Thanx!