Can anyone tell me why I'm getting the "Run-time error '91'" message in my function below? It's happening on this line:
Set olMailItem = olApp.CreateItem(olMailItem)
Also, whenever I'm in debug and I place my cursor over this line, Access gives me this message: "Outlook.Application = < Object variable or With block variable not set >":
Dim olApp As New Outlook.Application
I'm trying to create a button that will open an outlook email message and allow the data entry clerk to edit the message before sending it. I've checked my references and I have Microsoft Outlook 14.0 Object Library checked.
Also, if you have any suggestions on making my code more efficient, please share. I'm fairly new to Access programming.
Private Sub EmailButton_Click()
Dim EmailThis As String
EmailThis = CreateEmailWithOutlook("[email protected]", "Testing e-mail Access database", "This is a test")
DoCmd.SendObject acSendForm, "SubmitNewIdeaForm", , "My Name", , "Test", , True
On Error GoTo CreateEmail_Exit
CreateEmail_Exit:
Exit Sub
End Sub
Public Function CreateEmailWithOutlook(MessageTo As String, Subject As String, MessageBody As String)
' Define app variable and get Outlook using the "New" keyword
Dim olApp As New Outlook.Application
Dim olMailItem As Outlook.MailItem ' An Outlook Mail item
Set olApp = CreateObject("Outlook.Application")
' Create a new email object
Set olMailItem = olApp.CreateItem(olMailItem)
' Add the To/Subject/Body to the message and display the message
With olMailItem
.To = MessageTo
.Subject = Subject
.Body = MessageBody
.Display ' To show the email message to the user
End With
' Release all object variables
Set olMailItem = Nothing
Set olApp = Nothing
End Function
To correct this errorMake sure you aren't referring to an object variable that has been set to Nothing . Search your code for the keyword Nothing , and revise your code so that the object isn't set to Nothing until after you have referenced it. Make sure that any array variables are dimensioned before you access them.
Runtime errors occur as your macro runs, and typically result from specific conditions present at that time. For example, if you prompt the user for a host name, and attempt to connect to that host, but the host is not available, the Connect method fails and Visual Basic generates a runtime error.
Step 1: Write the subprocedure for VBA Type Mismatch. Step 2: Again assign a new variable, let's say “A” as Byte data type. Let's understand the Byte Data type here. Byte can only store the numerical value from 0 to 255.
The problem is that, with the Outlook library reference enabled, olMailItem
is a reserved constant, and I think when you are Dim olMailItem as Outlook.MailItem
that is not a problem, but trying to set the variable is causing a problem.
Here is the full explanation:
You have declared olMailItem
as an object variable.
Object
prior to setting it's value to an instance of an object. This is basically a recursive error, since you have the object trying to assign itself itself. olMailItem
had previously been assigned, this statement would raise another error (probably a Mismatch
error, since the constant olMailItem
is an Integer but by using this name inappropriately, you may introduced the mismatch error by passing an Object
where an Integer
is expected.Try changing the name of this variable olMailItem
to something else, like mItem
. This code is tested in Excel 2010, Windows 7, I think it should work for Access, too:
Dim olApp As New Outlook.Application
Dim mItem As Outlook.MailItem ' An Outlook Mail item
Set olApp = CreateObject("Outlook.Application")
Set mItem = olApp.CreateItem(olMailItem)
' Add the To/Subject/Body to the message and display the message
With mItem
.To = MessageTo
.Subject = Subject
.Body = MessageBody
.Display ' To show the email message to the user
End With
' Release all object variables
Set mItem = Nothing
Set olApp = Nothing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With