Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can I send an HTML file as body text using Outlook.MailItem?

Posted on 2008-10-24
3
Medium Priority
?
797 Views
Last Modified: 2013-11-27
I'm trying to send an HTML file as 'body text' via MS Access vba using the Outlook.MailItem feature.
I'm able to do it fine with the CDO message object as follows:
objMessage.CreateMHTMLBody "file://c|/temp/test.htm"

But when I try to do this with the Outlook message object, I get an error.
I couldn't find a 'CreateMHTMLBody' option using Outlook objects, so I tried:
objMailMessage.HTMLBody "file://c|/temp/test.htm"
But received an error :(

The reason why I'm trying to use Outlook objects instead of CDO is that I want to save to my Outlook drafts folder.

0
Comment
Question by:Bill543
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 22799725
I think you need to create a message template in Outlook using the html page.  Then you can create a new message using that template.  I have some code around here somewhere that does exactly that.  I'll see if I can dig it up.
OM Gang
0
 
LVL 28

Accepted Solution

by:
omgang earned 1000 total points
ID: 22799786
Found it - it's older code but shows how to create an Outlook message in Access and specify a template to use
OM Gang
Sub SendMessage(Reg As String)
On Error Resume Next
 
        'declare and open instance of MS Outlook
    Dim olOutlook As New Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim olFolders As Outlook.MAPIFolder
    Dim olContacts As Outlook.MAPIFolder
    Dim olItems As Outlook.Items
    Dim olConItem As Outlook.ContactItem
    Dim olOutlookMsg As Outlook.MailItem
    Dim olOutlookRecip As Outlook.Recipient
    Dim olOutlookAttach As Outlook.Attachment
    Dim strMsgBody As String, strPST As String
    Dim strCriteria As String
    Dim intCounter As Integer
    
        'build message body for this region
        'function call - this module
        'returns "Failed" on error
    strMsgBody = BuildMsg(Reg)
    If strMsgBody = "Failed" Then
            'call error handler with specifics - this module
        Call ErrorHandler("BuildMsg", 1, False)
        GoTo Exit_SendMessage
    End If
    
        'assign name of outlook PST folder we want to use to string
        'variable
    strPST = "Broadcast"
        'set object Outlook NameSpace
    Set olNS = olOutlook.GetNamespace("MAPI")
        'set object NameSpace Folders for PST file
    Set olFolders = olNS.Folders(strPST)
        'set object Contacts folder for PST file
    Set olContacts = olFolders.Folders("Contacts")
        'set object Contacts in folder
    Set olItems = olContacts.Items
    
        'assign item category for this region to string variable so
        'we can search for contacts
    strCriteria = Reg & "_Broadcast"
        
        'loop through list of contacts
    For intCounter = 1 To olItems.Count
        Set olConItem = olItems(intCounter)
            'find contacts for the current region
        If olConItem.Categories = strCriteria Then
    
                'select message template for this region
            Set olOutlookMsg = olOutlook.CreateItemFromTemplate _
                    ("i:\outlook\templates\" & Reg & "_Broadcast.oft")
 
            With olOutlookMsg
                    'recipient
                Set olOutlookRecip = .Recipients.Add(olConItem.Email1Address)
                olOutlookRecip.Type = olTo
 
                    'subject, body, importance
                .Subject = "Inventories Available for " _
                        & "Cherry Pick"
                .Body = strMsgBody
                .Importance = olImportanceNormal
 
                    'attachment(s)
                Set olOutlookAttach = .Attachments.Add _
                        ("p:\output\inventory1.pdf", , , "Inventory 1")
                Set olOutlookAttach = .Attachments.Add _
                        ("p:\output\inventory2.pdf", , , "Inventory 2")
 
                    'resolve recipient name
                For Each olOutlookRecip In .Recipients
                    olOutlookRecip.Resolve
                Next
 
                    'display before sending?  add (DisplayMsg As Boolean)
                    'to sub/func declare
                'If DisplayMsg Then
                '    .Display
                'Else
                '    .Save
                '    .Send
                'End If
 
                .Save
                .Send
 
            End With
        End If
    
    Next intCounter
 
Exit_SendMessage:
        'clear object variables
    Set olOutlookAttach = Nothing
    Set olOutlookRecip = Nothing
    Set olOutlookMsg = Nothing
    Set olConItem = Nothing
    Set olItems = Nothing
    Set olContacts = Nothing
    Set olFolders = Nothing
    Set olNS = Nothing
    Set olOutlook = Nothing
    Exit Sub
 
End Sub

Open in new window

0
 

Author Closing Comment

by:Bill543
ID: 31509811
Thanks! As usual you pulled through. Your solution worked for me.
I needed to add/edit the below line from your code:
'select message template for this region
Set olOutlookMsg = olOutlook.CreateItemFromTemplate _
("i:\outlook\templates\" & Reg & "_Broadcast.oft")
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question