Adding text to email body

tarun_shetty

New Member
Joined
Apr 18, 2013
Messages
1
Here goes,

All I have to do is to add a formatted text (red color) at the beginning of the email body and forward to a particular email address.

I'm stuck in the first part. What I have achieved so far that I have to keep the email open in edit mode. Only then my code adds the formatted text and save the email. Can I achieve this just by keeping the email in active selection? neither do I wish to open the mail in edit or read mode in a separate window.

I just got into macro programming yesterday, so please be patient. But I do have programming background. Any suggestions how I can achieve the first of my problem.


My current code:
Sub AddText()

Dim objItem As Object
Dim objInsp As Outlook.Inspector

' Add reference to Word library
' in VBA Editor, Tools, References
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
On Error Resume Next

'Reference the current Outlook item
' Set objItem = Application.ActiveExplorer.Selection
Set objItem = Application.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
Set objInsp = objItem.GetInspector
If objInsp.EditorType = olEditorWord Then
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection

With objSel
' Formatting code goes here

Dim Ins As Outlook.Inspector
Dim Doc As Word.Document
Dim Range As Word.Range
Dim Pos As Long

Set Ins = Application.ActiveInspector
Set Doc = Ins.WordEditor
If Not Doc Is Nothing Then
Pos = Doc.Range.End - 1
Set Range = Doc.Range(0, 0)
Range.Select
End If

.Font.Color = wdColorRed
.TypeText Text:="Text Message" & vbNewLine & vbNewLine

End With

End If
End If
End If

' objItem.Save
objItem.Close (olSave)


Set objItem = Nothing
Set objWord = Nothing
Set objSel = Nothing
Set objInsp = Nothing


End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Set a Reference (Tools > References > , from within the VBE window) to the Outlook Object Library

Instantiate a new Outlook App (Actually links to your open one, doesn't open a new one for me).

Then its really just modify something like the below:

Code:
Sub EmailWithOutlook()
    
    Dim oApp, oMail As Object, _
    MailBody As String, MailSubject As String




    Set oApp = CreateObject("Outlook.Application")


    MailBody = "Mail Body Text Here."
    
    MailSubject = "Subject Line Here"




    Set oMail = oApp.CreateItem(0)
    With oMail
        .To = "Email@Address.com"
        .Subject = MailSubject
        .Body = MailBody
        .Send
    End With

    Set oMail = Nothing
    Set oApp = Nothing

End Sub

This does Plain txt emails, but still supports generic windows styley formatting (Bold, Underline, Colours, etc). The code for an HTML email I do have lying around somewhere, but I can't find it.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,496
Members
449,089
Latest member
Raviguru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top