I don't know VBA, but in PowerShell the script would look like this (notice it is using VBA classes):
#Create Outlook Application object
$ol = New-Object -comObject Outlook.Application
# Create the new email
$mail = $ol.CreateItem(0) # 0 is the value of OlItemType.olMailItem
# Set the subject
$mail.Subject = "Formatting test"
# Set body format to HTML
$mail.BodyFormat = 2 # 2 is the value of OlBodyFormat.olFormatHTML
# Set the body
$mail.HTMLBody = "<html><body><p>Test</p></body></html>"
# Bring the message window to the front
$mail.Display()
I hope this helps.
Edit: I enabled Developer Tools in my Outlook and using examples from documentation I converted my PowerShell code to VBA subroutine:
Sub MakeMessage()
Dim OutlookMessage As Outlook.MailItem
Set OutlookMessage = Application.CreateItem(olMailItem)
OutlookMessage.Subject = "Hello World!"
OutlookMessage.BodyFormat = olFormatHTML
OutlookMessage.HTMLBody = "<html><body><p>Test</p></body></html>"
OutlookMessage.Display
End Sub
although I still do not know how to run this as a script.
Edit: Ok, so this is how to use Events to edit email on opening:
Dim WithEvents m_objMail As Outlook.mailItem
Private Sub Application_ItemLoad(ByVal Item As Object)
Select Case Item.Class
Case olMail
Set m_objMail = Item
End Select
End Sub
Private Sub m_objMail_Open(Cancel As Boolean)
If m_objMail.Subject = "Hello World!" Then
m_objMail.BodyFormat = olFormatPlain
m_objMail.HTMLBody = "<html><body><p>Body: " + m_objMail.body + " </p></body></html>"
End If
End Sub
You need to remember to change if condition in subroutine m_objMail_Open
to whatever fits the email generated by your hotel software and the generated HTMLBody to fit your desired output.
Also, please note that MailItem.Open event is raised whenever any email is opened in a new window, so you need to check if you're dealing with new email rather than existing one (so I guess Not m_objMail.Sent And Not m_objMail.Saved
to eliminate sent and received emails, but you need to experiment with that).
What's the default mail format set in your Outlook. By default, it should be HTML. If it is set to HTML however the email still open in Plain Text format, you may try to contact the developer of the management software to see whether they have any control on this. – Steve Fan – 2018-07-25T06:32:06.723
yes, this is the thing ... default in outlook is set to HTML, however the hotel sw opens a new email in plain text format ... i cant change this ... talkt to the devs in the first place ... however, will remain for now. So I need a workaround. – haemse – 2018-07-25T10:07:57.833