Problem with Object Reference for MailItem

A

alexcraig

I've solved this problem in the past, but I just cannot remember how.

The below code runs fine until it hits objMsg.Subject = "Test Subject" where
I get an "Object Required" error message.

I guess I am somehow not properly qualifying objMsg.

Please help.

Code:

Dim Source As Variant, Rs1 As ADODB.Recordset
Dim JobOrderNo As String, Srcresult As String, Connect As String, CoID As
String, CoName As String
Dim CoAddress1 As String, CoAddress2 As String, CoCity As String, CoState As
String, CoZip As String
Dim objMsg As MailItem
Dim sInspector As Object
Sub SENDOUTINFO()
'
' Insert Sendout Info
' Macro created 1/5/2008 by Alex Craig
'
' Display Input Box and Get Job Order #
Message = "Enter Job Order #"
Title = "Job Order # Input Box"
JobOrderNo = InputBox(Message, Title)
' Get Job Order Data from Database
Set Rs1 = New ADODB.Recordset
Source = Array("SELECT * FROM tblJobOrders WHERE JobOrderNum = ", "
")
Connect = "Provider=MSDASQL; Driver={SQL Server}; Server=XXXXXXX;
Database=XXX; UID=XX; PWD=XXXXX;"
Source(1) = JobOrderNo
Srcresult = Source(0) & Source(1)
Rs1.Open Srcresult, Connect
Rs1.MoveFirst
CoID = Rs1!CompanyID
Rs1.Close
Set Rs1 = Nothing
' Get Company Data from Database
Set Rs1 = New ADODB.Recordset
Source = Array("SELECT * FROM tblCompanyData WHERE CompanyID = ", "
")
Connect = "Provider=MSDASQL; Driver={SQL Server}; Server=XXXXXXX;
Database=XXX; UID=XX; PWD=XXXXXXXs;"
Source(1) = CoID
Srcresult = Source(0) & Source(1)
Rs1.Open Srcresult, Connect
Rs1.MoveFirst
CoName = Rs1!Name
CoAddress1 = Rs1!Address1
CoAddress2 = Rs1!Address2
CoCity = Rs1!City
CoState = Rs1!StateOrProv
CoZip = Rs1!PostalCode
Rs1.Close
Set Rs1 = Nothing
' Create and populate the email item.
Set objMsg = Application.CreateItem(olMailItem)
obMsg.Subject = "Test Subject"
obMsg.Body = "Test Body"
obMsg.Display
End Sub
 
A

alexcraig

Please ignore this request.

5 hours later, I finally noticed I had the MailItem delcared as "objMsg" and
was referring to it in the code as "obMsg"

AAAAAhhhhhhhhhhhh!!!!!
 
J

JP

If you used "Option Explicit" at the top of all your code modules,
this wouldn't have happened.

Or in the VBE, click Tools>Options>Editor tab, check "Require Variable
Declaration" box to add "Option Explicit" automatically to the
beginning of every new module.

HTH,
JP
 
A

alexcraig

JP,

Sounds like a pretty darn good tip!

I'll take your advice and maybe save myself a chunk of time in the future.

Thanks.
 

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