Trying to add subject and name to an email using VB Code

pjeanphilippe

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Hi:

I am fairely new to coding, so pardon the ignorance. I am trying to use Visual Basic to create a couple of macros for me in Outlook 2010. It works fine until I try to insert a code for Subject, then everything goes awry. I haven't even tried to insert text into the coding because I have been spending most of my time debugging the issue. I've looked into so many different websites and no information has worked thus far. Please help.

Thank you so much.

Sub Try()
Dim olApp As Outlook.Application
Dim olMailItem As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim strHTMLBody As String
Dim olMsg As Outlook.MailItem

Set olApp = New Outlook.Application
Set olInsp = olApp.ActiveInspector
Set olMsg = Application.CreateItem(olMailItem)

olMsg.Subject = "My Subject"
olMsg.Display

strHTMLBody = olMailItem.HTMLBody
olMailItem.BodyFormat = olFormatHTML
olMailItem.HTMLBody = "<HTML><BODY><FONT face=""Calibri"" size=3 color=black> Hi [name of person]:" & _
"<BR><BR> Can you blah blah blah <b>[Name of Course]</b> in <b>[enter Month]</b>?" & _
"<BR><BR><BR> When done, can you blah blah blah?" & _
"<BR><BR> Thank you so much. </FONT></BODY></HTML>" & vbCrLf & vbCrLf & strHTMLBody

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
What exactly are you trying to do? Your code mixes olmsg and olmailitem.

This code creates a new message without error:
Sub Try()
Dim strHTMLBody As String
Dim olMsg As Outlook.MailItem
Set olMsg = Application.CreateItem(olMailItem)
olMsg.Subject = "My Subject"
olMsg.Display
strHTMLBody = olMsg.HTMLBody
olMsg.HTMLBody = "<HTML><BODY><FONT face=""Calibri"" size=3 color=black> Hi [name of person]:" & _
"<BR><BR> Can you blah blah blah <b>[Name of Course]</b> in <b>[enter Month]</b>?" & _
"<BR><BR><BR> When done, can you blah blah blah?" & _
"<BR><BR> Thank you so much. </FONT></BODY></HTML>" & vbCrLf & vbCrLf & strHTMLBody
olMsg.BodyFormat = olFormatHTML
End Sub

if you are trying to pick up a value from a contact, see http://www.slipstick.com/developer/create-deferred-birthday-message-contact/ for code samples.
 

pjeanphilippe

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Diane:

Thank you so much! This was exactly what I was trying to do! I tried it and it worked perfectly.

I do have one more question though: what is the code for adding a person's email address to the To: and cc: field and how can it be added to this coding?

Again, you are a lifesaver!

Much Appreciated,

PJ

What exactly are you trying to do? Your code mixes olmsg and olmailitem.

This code creates a new message without error:

Sub Try()

Dim strHTMLBody As String

Dim olMsg As Outlook.MailItem

Set olMsg = Application.CreateItem(olMailItem)

olMsg.Subject = "My Subject"

olMsg.Display

strHTMLBody = olMsg.HTMLBody

olMsg.HTMLBody = "<HTML><BODY><FONT face=""Calibri"" size=3 color=black> Hi [name of person]:" & _

"<BR><BR> Can you blah blah blah <b>[Name of Course]</b> in <b>[enter Month]</b>?" & _

"<BR><BR><BR> When done, can you blah blah blah?" & _

"<BR><BR> Thank you so much. </FONT></BODY></HTML>" & vbCrLf & vbCrLf & strHTMLBody

olMsg.BodyFormat = olFormatHTML

End Sub

if you are trying to pick up a value from a contact, see http://www.slipstick.com/developer/create-deferred-birthday-message-contact/ for code samples.
 
Top