How can I declare Outlook.MailItem (1 Viewer)

DNewman

Registered User.
Local time
Today, 20:35
Joined
Oct 12, 2012
Messages
60
End User: Access 2010 Runtime on Windows XP
Automated emails using an Outlook Template

I am trying to avoid referencing the Outlook Object Library as the PC's don't all have the same version of Outlook and I need my my runtime package to be installed at will on new/renovated PC's ... - so I am "Late Binding" :

Dim objOutlook as Object
Set objOutlook =GetObject("Outlook.Application")
seems to be OK if Outlook is open (I have put in a warning box to ensue users open Outlook)

My problem is now how to make the MailItem object

I (think I) need: Set objMailItem=objOutlook.CreateMailItem(objMailItem)
but have to I declare objMailItem??

Dim objMailItem as Outlook.MailItem obviously doesn't work
Dim objMailItem as MailItem doesn't work (though I found on the net!)
dim objMailItem as Object doesn't work either.

Is there another way of creating a MailItem object?
Is there a way of avoiding having to declare it?

Any suggestions would be most welcome.
 

spikepl

Eledittingent Beliped
Local time
Today, 21:35
Joined
Nov 3, 2010
Messages
6,142
You really should hone your google skills - that is worth a lot of code and time, instead off running to AWF with a question you could google yourself in 60 seconds. Use AWF for less obvious things.

Where did you get the idea of using objMailItem in objOutlook.CreateMailItem(objMailItem) ? This idea is completely wrong.

It is - and should be - olMailItem and that is an outlook parameter (i.e. a number) and it has a value of 0! With late binding, with Outlook or any other code, all the application-specific parameters are not known to the code and need to be assigned values explicitely.
 

James Deckert

Continuing to Learn
Local time
Today, 14:35
Joined
Oct 6, 2005
Messages
189
Set objOutlook = CreateObject("Outlook.Application")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const olMailItem = 0
Set objMsg = objOutlook.CreateItem(olMailItem)
Const olFormatHTML = 2
With objMsg
.To = strRecipient
.Subject = strSubject
.BodyFormat = olFormatHTML
.HTMLBody = strBody
.Send
End With

Set objOutlook = Nothing
Set objMsg = Nothing
 

DNewman

Registered User.
Local time
Today, 20:35
Joined
Oct 12, 2012
Messages
60
Thank you for your helpful reply James.
One question: Can I use:

Set objMesg = objOutlook.CreateItemFromTemplate(DLookup("[TemplatesDirectory]", "MSystMySettings") & "ConfirmationLetterTemplate.oft")

in place of your:

Set objMsg = objOutlook.CreateItem(olMailItem)

Since I don't use Outlook I am paddling in uncharted seas - according to Spike, what I have found surfing the net is TOTALLY WRONG so I am hesitant to presume too much!!
 

Users who are viewing this thread

Top Bottom