Link to home
Start Free TrialLog in
Avatar of Engtech05
Engtech05Flag for United States of America

asked on

HTML Email inside Access

I am try to send an access report via email but as html in the body of the email.

I am using the code below with reference to MS Outlook 14.0 Object library.
First error comes at dim olmail as outlook.mailitem. (Error - User-defined type not defined)
So i comment it out
code works until the 'With olMail', I then get an error code "Runtime error "5", Invalid Procedure call or argument.

I have gone round and round with this code and its driving me insane. Please help.

Public Sub CreateHTMLMail(strRptName As String)

Dim olApp, olMail As Object
'Dim olMail As Outlook.MailItem
Dim oFilesys, oTxtString As Object
Dim txtHTML As String

DoCmd.OpenReport "SQR", acViewReport, , "SQRUID = " & AAA
DoCmd.OutputTo acOutputReport, strReportName, acFormatHTML, "C:\temp\" & strReportName & ".html", False

Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(olmailitem)

Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oTxtStream = oFilesys.OpenTextFile("C:\temp\" & strReportName & ".HTML", 1)

txtHTML = oTxtStream.ReadAll


Set oTxtStream = Nothing
Set oFilesys = Nothing

With olMail
.BodyFormat = olFormatHTML
.HTMLBody = txtHTML
.Recipients.Add ""
.Subject = "Safety Quick React"
End With

Set olApp = Nothing
Set olMail = Nothing

End Sub
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

I think you must double check your references again. those errors suggest issues with your references.User generated imageand the code which produced that
Sub test()

    Dim objApp As Outlook.Application
    Dim objMail As Outlook.MailItem
    Set objApp = CreateObject("Outlook.Application")
    Set objMail = objApp.CreateItem(olMailItem)
    With objMail
        .BodyFormat = olFormatHTML
        .HTMLBody = txtHTML
        .Recipients.Add ""
        .Subject = "Safety Quick React"
    End With
    Set objApp = Nothing
    Set objMail = Nothing
End Sub

Open in new window

i know I used Excel. But same thing
Avatar of Engtech05


The MS Outlook 15.0 works for Office 2013, not for 2010. therein lies my problem. I have to use MS Outlook reference 14.0
If you have version binding issues, then perhaps, instead of using project references (early binding), rather use Late Binding. Of course you give up intellisense etc, but you gain some version independence (so long as using basic, common features).

But that seems to be quite complex for Outlook:
(I dont know how true it is that CreateObject for Outlook can be a problem)... but I think this article seems to takl about all the relevant bits you need.
Avatar of Norie

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yippie!  Thank you so much that works great!!

One question though, and pardon my ignorance, what does the,

Const olMailItem = 0
Const olFormatHTML = 2

do to make such a difference?
Avatar of Norie

olMailItem and olFormatHTML are Outlook VBA constants and are part of the Outlook Object Library.

Since we are using late binding we don't have access to that library, so we need to define them in the code.

We could just replace olMailItem and olFormatHTML in the original code with there values, 0 and 2 respectively, but I prefer to do it this way, makes things clearer.