I am using this code in MS Access to to open a saved HTML Outlook Template. The code searches for "SALUTATION" in the body of the email and replaces it with controls data from my form.
It works pretty well but, I lose the email formatting which includes formatted text, multiple links and a few images.
How can my code be changed to keep the original formatting?
Private Sub Command139_Click()
Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem
Dim value As String
value = Me.Salutation & " " & Me.LastName
Set myOlApp = CreateObject("Outlook.Application")
Set MyItem = myOlApp.CreateItemFromTemplate("C:\Users\Meiaer\AppData\Roaming\Microsoft\Templates\ELMOVM.oft")
MyItem.Display
With MyItem
.To = Me.EMAIL_ADDRESS
MyItem.Body = Replace(MyItem.Body, "SALUTATION", value)
End With
Set MyItem = Nothing
Set myOlApp = Nothing
End Sub
Thank you
You need to edit MyItem.HTMLBody, not MyItem.Body (which is the plaintext representation of the mail body).
See https://technet.microsoft.com/en-us/library/ff868941(v=office.14).aspx
Related
I want to automate the summation of a number of cells, in an HTML table on an Outlook email, according to their Red, Amber, Green status.
Scraping the contents is easy with "innerText" but "bgColor" seems to always be blank. I can't find any other attribute where the data might be hiding.
Public Sub TableScrubber()
'This macro interrogates an Outlook email's body to find a table and then show the contents of each cell
Dim outlookHTML As MSHTML.HTMLDocument: Set outlookHTML = New MSHTML.HTMLDocument
Dim elementCollection As MSHTML.IHTMLElementCollection
Dim iItem As Single
Dim iTable As Single
Dim iRow As Long
Dim iColumn As Long
Dim activeSelection As Outlook.Selection
Dim selectedObject As Object
Dim selectedMailItem As mailItem
Dim itemInfo As String
Set activeSelection = Application.ActiveExplorer.Selection
If activeSelection.Count > 0 Then
For iItem = 1 To activeSelection.Count
Set selectedObject = activeSelection.Item(iItem)
If (TypeOf selectedObject Is Outlook.mailItem) Then
Set selectedMailItem = selectedObject
itemInfo = "Message Subject: " & selectedMailItem.Subject
'save Outlook email's html body (tables)
With outlookHTML
.Body.innerHTML = selectedMailItem.HTMLBody
Set elementCollection = .getElementsByTagName("table")
End With
For iTable = 0 To elementCollection.Length - 1
For iRow = 0 To elementCollection(iTable).Rows.Length - 1
For iColumn = 0 To elementCollection(iTable).Rows(iRow).Cells.Length - 1
itemInfo = "The text in this cell is: " & elementCollection(iTable).Rows(iRow).Cells(iColumn).innerText
itemInfo = "The color of this cell is: " & elementCollection(iTable).Rows(iRow).Cells(iColumn).bgColor
Next iColumn
Next iRow
Next iTable
End If
Next iItem
End If
Set outlookHTML = Nothing
Set elementCollection = Nothing
End Sub
The Outlook object model provides three main ways of dealing with message bodies.
The Body property returns or sets a string representing the clear-text body of the Outlook item.
The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create e-mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>Enter the message text here. </BODY></HTML>"
.Display
End With
End Sub
The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.
So, you can use the Word object model for dealing with tables and changing their styles.
If this email does not display properly, please click here.
I would like to be able to have the person in the email click on the word "here" and then it will take them to a website.
This is what I have bee using but it just puts the full link into the email.
mymsg = "<HTML><BODY>"
mymsg = "If this email does not display properly, please click " & "<A href=http://us.localnews.com/ov?mailing=3TVGZMLJ-WDS49&m2u=3TVGZMLK-3TVGZMLJ-12Z630I>URL Text</A>"
mymsg = mymsg & "</BODY></HYML>"
Keep it simple, no need for the HTML/BODY tags
Example
Option Explicit
Public Sub Example()
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
Dim OutMail As Object
Set OutMail = OutApp.CreateItem(0)
With OutMail
.HTMLBody = "If this email does not display properly, please click " & _
"<A href=https://stackoverflow.com/>" & _
"Here</A>"
.Display
End With
End Sub
Make sure you are using HTMLBody Property
MSDN - HTMLBody Property
Returns or sets a String representing the HTML body of the specified item. The HTMLBody property should be an HTML syntax string. Read/write.
I currently have a spreadsheet that I use to generate mass e-mails that I can format using HTML and it works really well. One thing that I have not been able to figure out is whether or not there is a way to also incorporate CSS styling within an HTML e-mail created through excel.
Has anyone tried this/had success with this before?
My current subroutine uses the Outlook Application to construct material into an HTML email. The code for calling/constructing is:
Sub Test_Email(what_address As String, subject_line As String, mail_body_message As String)
Dim olApp As Outlook.Application
Dim oAttatch As Outlook.Attachment
Set olApp = CreateObject("Outlook.Application")
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.To = what_address
.Subject = "My Subject"
.BodyFormat = olFormatHTML
.HTMLBody = mail_body_message
'mail_body_message is all text that resides in Cell A1 and it consists of body text of the email as well as the HTML tags that I use to organize the message display
.Display
End With
End Sub
Within cell A1, I have all of my message information. Starts with and ends with <HTML> </HTML> and then my message body resides within the tags. So far, when I have tried to insert CSS styles within the existing HTML in cell A1, it spits out the HTML just fine but treats the CSS as text.
You'll need to use inline CSS.
For example:
<img src="http://example.com/foo.jpg" style="max-width:100px;">
Or
<a href="http://example.com" style="color:#000;">
I would like to know how can I export data from VB6 textbox to a HTML textbox? It could be a simple html page or an asp page.
for example, on my VB6 form, i have an name field. Upon clicking of a button on the VB6 form, the data in the name field will be exported to a textbox on the html page.
Thank you all for help and time for reading this.
To see this demo in action, and be able to follow it through and learn how to grab from it what you need:
Create a form with a lable over a textbox, and stick 1 command buttons on the form. Don't rename any of them - the program expects the text1, command1
The following CODE is the complete FORM CODE to copy/paste into it.
Add refernce to your project from (Project=>References)Microsoft Internet Controls,Microsoft HTML Object Library,
Option Explicit
Public TargetIE As SHDocVw.InternetExplorer
Private Sub Command1_Click() ' Send text to first IE-document found
GetTheIEObjectFromSystem
SendTextToActiveElementWithSubmitOptionSet (False)
End Sub
Private Sub Form_Load()
Me.Text1 = "This is a sample text message set and submitted programmatically" 'make text1 multiline in design
Me.Command1.Caption = "Text to the first IE browser document found"
End Sub
Public Sub GetTheIEObjectFromSystem(Optional ByVal inurl As String = ".") ' "." will be found in ALL browser URLs
Dim SWs As New SHDocVw.ShellWindows
Dim IE As SHDocVw.InternetExplorer
Dim Doc As Object
For Each IE In SWs
If TypeOf IE.Document Is HTMLDocument Then ' necessary to avoid Windows Explorer
If InStr(IE.LocationURL, inurl) > 0 Then
Set TargetIE = IE
Exit For
End If
End If
Next
Set SWs = Nothing
Set IE = Nothing
End Sub
Private Sub SendTextToActiveElementWithSubmitOptionSet(ByVal bSubmitIt As Boolean)
Dim TheActiveElement As IHTMLElement
Set TheActiveElement = TargetIE.Document.activeElement
If Not TheActiveElement.isTextEdit Then
MsgBox "Active element is not a text-input system"
Else
TheActiveElement.Value = Me.Text1.Text
Dim directParent As IHTMLElement
If bSubmitIt Then
Dim pageForm As IHTMLFormElement
Set directParent = TheActiveElement.parentElement
' find its parent FORM element by checking parent nodes up and up and up until found or BODY
Do While (UCase(directParent.tagName) <> "FORM" And UCase(directParent.tagName <> "BODY"))
Set directParent = directParent.parentElement
Loop
If UCase(directParent.tagName) = "FORM" Then
Set pageForm = directParent
pageForm.submit 'intrinsic Form-element Method
Else
MsgBox ("Error: No form unit for submitting the text on this page!")
End If
End If
Set pageForm = Nothing
Set directParent = Nothing
End If
Set TheActiveElement = Nothing
Set TargetIE = Nothing
End Sub
I have code, similar to the following, that I would like to modify:
Sub SendEmail()
Dim myOlApp As Outlook.Application
Dim myItem As Outlook.MailItem
'Create an Outlook application object
Set myOlApp = New Outlook.Application
'Create a new MailItem form
Set myItem = myOlApp.CreateItem(olMailItem)
'Build and display item
With myItem
.To = “test#test.com”
.Subject = “Test Subject”
.HTMLBody = “Test Body”
.Display
.SaveAs “C:\Test.msg”, olMSG
End With
End Sub
This code is called from various buttons throughout the application. When a button is clicked, a new email is created and saved. Unfortunately, the email is saved as soon as it is created and BEFORE it is sent... so, if any modifications are made to it, they will not be in the saved version.
What can I do to modifiy this code to ONLY save the email once it has been sent?
Feel free to ask any followup questions as necessary and I will respond as best I can.
Thanks!
Robert
You can use Outlook events with Access. For this example you will need a Class Module called clsOlMail with this code:
''Requires reference to the Microsoft Outlook x.x Object Library
Dim WithEvents conItems As Outlook.Items
Private Sub Class_Initialize()
Set oApp = Outlook.Application
Set oNS = oApp.GetNamespace("MAPI")
Set conFolder = oNS.GetDefaultFolder(olFolderSentMail)
Set conItems = conFolder.Items
End Sub
Private Sub Class_Terminate()
Set conItems = Nothing
Set conFolder = Nothing
Set oNS = Nothing
Set oApp = Nothing
End Sub
Sub ConItems_ItemAdd(ByVal Item As Object)
Dim frm As Form
Set frm = Forms!frmEmailDetails
frm.txtSenderName = Item.SenderName
frm.txtSentOn = Item.SentOn
frm.txtTo = Item.To
frm.txtCreationTime = Item.CreationTime
frm.txtBCC = Item.BCC
frm.txtCC = Item.CC
frm.txtSentOnBehalfOfName = Item.SentOnBehalfOfName
frm.txtSubject = Item.Subject
frm.txtBody = Item.Body
End Sub
You will also need a form called frmEmailDetails with these textboxes:
txtSenderName, txtSentOn, txtTo, txtCreationTime, txtBCC, txtCC, txtSentOnBehalfOfName, txtSubject, txtBody
And this code:
Private oEvent As clsOLMail
''Requires reference to Microsoft Outlook x.x Object Library
Public oApp As Outlook.Application
Public oNS As Outlook.NameSpace
Public conFolder As Outlook.MAPIFolder
Private Sub Form_Open(Cancel As Integer)
Set oEvent = New clsOlMail
End Sub
Open the form and send an email through Outlook, you can use one of the examples shown above. The form fields should fill with the relevant details from the sent email. You are likely to get an Outlook security warning.
From: http://wiki.lessthandot.com/index.php/Access_and_Email
The problem is there is no EntryID for the newly created item. Once you save/send this item, the reference is no longer good. Why is probably due to how MAPI works. Remou suggests using the ItemAdd event to handle the item newly added to the special folder "Sent Items". From this event you can save the message. The only issue I see is how would you know passed item is the sent item. You are calling Display, which allows the user to preview, edit, send, or close the message without sending. Therefore, the item may not be the mail item you created. To get around this, add a custom property to your mail item. When the ItemAdd event is fired, you can inspect the passed item for the custom property, and save if needed.