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

oTxtStream.Close

Set oTxtStream = Nothing
Set oFilesys = Nothing

With olMail
.BodyFormat = olFormatHTML
.HTMLBody = txtHTML
.Recipients.Add "gdrahos@igloocorp.com"
.Subject = "Safety Quick React"
.Display
End With

Set olApp = Nothing
Set olMail = Nothing


End Sub
Engtech05Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
I think you must double check your references again. those errors suggest issues with your references.Quick Testand 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 "gdrahos@igloocorp.com"
        .Subject = "Safety Quick React"
        .Display
    End With
    
    Set objApp = Nothing
    Set objMail = Nothing
    
End Sub

Open in new window

0
MlandaTCommented:
i know I used Excel. But same thing
0
Engtech05Author Commented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

MlandaTCommented:
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:
http://www.devhut.net/2014/10/31/createobjectoutlook-application-does-not-work-now-what/
(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.
0
NorieVBA ExpertCommented:
Try this, it uses late binding and a reference to any MS Outlook X.0 Object Library isn't needed.
Public Sub CreateHTMLMail(strRptName As String)

Dim olApp As Object, olMail As Object
'Dim olMail As Object
Dim oFilesys, oTxtString As Object
Dim txtHTML As String

Const olMailItem = 0
Const olFormatHTML = 2

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

oTxtStream.Close

Set oTxtStream = Nothing
Set oFilesys = Nothing

With olMail
.BodyFormat = olFormatHTML
.HTMLBody = txtHTML
.Recipients.Add "gdrahos@igloocorp.com"
.Subject = "Safety Quick React"
.Display
End With

Set olApp = Nothing
Set olMail = Nothing


End Sub

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Engtech05Author Commented:
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?
0
NorieVBA ExpertCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.