Debug this code for me?

C

Carl J. Hixon

I copied this routine from MS support with minor changes. I ran it in the
immediate window yesterday several times and it worked fine. Today, it
doesn't work. I am getting a message that says, "Compile Error: Variable
not defined" The variable it is referring to is "objOutlook" in the "Set
objOutlook = CreateObject("Outlook.Application")" statement. Any idea what
the problem is?
Thanks!
-----------------------------<Code>--------------------------
Option Compare Database

Option Explicit

Sub SendMessage(MailRecip, Optional AttachmentPath)
'SendMessage subroutine for sending a message using the default user
'in Microsoft Outlook. Use this subroutine by passing 1 or 2 parameters
'MailRecip is a text string containing the Recipients Name which must
'be in the Outlook Contacts Directory. AttachmentPath is optional
'and it is a tring which contains the full path and file name of a
'file to be attached.Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

' Create the Outlook Session.
Set objOutlook = CreateObject("Outlook.Application")

'Create the Message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient (s) to the message.
Set objOutlookRecip = .Recipients.Add(MailRecip)
objOutlookRecip.Type = olTo

'Add the CC recipient(s) to the message.
'Set objOutlookRecip = .Recipients.Add("BJ Hixon")
'objOutlookRecip.Type = olCC

'Set the Subject, Body, and Importance of the message.
.Subject = "OAA Announcement"
.Body = "Last test - I promise." & vbCrLf & vbCrLf
.Importance = olImportanceHigh

'Add attachments to the message.
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If

'Since we are sending from MSAccess Query, no need to
'resolve each name with outlook contacts.
'Resolve each Recipient's name.
'Uncomment next 5 lines if resolve is desired.
'For Each objOutlookRecip In .Recipients
'objOutlookRecip.Resolve
'If Not objOutlookRecip.Resolve Then
'objOutlookMsg.Display
'End If
Next
.Send

End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub
-----------------------------<End Code>---------------------------
 
C

Cheryl Fischer

In the code you posted, the Dim statement for objOutlook is on a comment
line:
'file to be attached.Dim objOutlook As Outlook.Application

should be:

'file to be attached.

Dim objOutlook As Outlook.Application


hth,
 
J

Jonathan Parminter

Hi Carl,

if you review the declarations you don't see...
dim objOutlook as Outlook.Application

perhaps you accidentally deleted this line?

Luck
Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top