Hotel software opens E-Mail Window in Outlook with generated text - how to change it automaticly to html or trigger a vba script?

0

My situation: A hotel managment software generates a text and opens a "New E-Mail" Window in Outlook with that text preset. However, I need the E-Mail to be in HTML format, apply some formatting and add the right signature.

The E-Mail Format is plain text. What is a possible way to have that E-Mail automaticly change to html format. I have no option to set this up in the hotel software.

What could be a feasible option? Automaticly trigger a VBA Script, that changes this?

haemse

Posted 2018-07-24T09:05:08.527

Reputation: 123

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

Answers

1

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).

Konrad Botor

Posted 2018-07-24T09:05:08.527

Reputation: 334

hey, thanks a lot! however, my problem is more that the hotel software is already initiating an outlook email (outlook subwindow opens with email text). I would like to find a way to intercept this in order to end up with a formatted email, insted of a text only email. – haemse – 2018-07-25T00:14:59.960

You should check the documentation: https://msdn.microsoft.com/en-us/vba/vba-outlook. I'm guessing you'd have to try something with Application.ItemLoad event, but I couldn't make it work.

– Konrad Botor – 2018-07-25T06:57:27.997

1OK, I figured it out - see my edit – Konrad Botor – 2018-07-25T08:50:21.717

this looks great! I will try it. So the m_objMail_Open might be key to this. thx for now. – haemse – 2018-07-25T10:10:34.290

works so far ... however I am having a hard time adding one of the signatures. – haemse – 2018-07-25T21:53:50.330

This sounds like a separate issue. To avoid long discussion in the comments please open a new question with description of your new problem and the solutions you have already tried. I'd be grateful if you marked my answer to this question as accepted as well. – Konrad Botor – 2018-07-26T06:13:37.137