A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Problem with Object Reference for MailItem



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old January 6th 08, 04:59 PM posted to microsoft.public.outlook.program_vba
alexcraig
external usenet poster
 
Posts: 8
Default Problem with Object Reference for MailItem

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
  #2  
Old January 6th 08, 06:06 PM posted to microsoft.public.outlook.program_vba
alexcraig
external usenet poster
 
Posts: 8
Default Problem with Object Reference for MailItem


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!!!!!

"alexcraig" wrote:

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

  #3  
Old January 6th 08, 07:34 PM posted to microsoft.public.outlook.program_vba
JP[_3_]
external usenet poster
 
Posts: 201
Default Problem with Object Reference for MailItem

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

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

HTH,
JP


On Jan 6, 1:06*pm, alexcraig
wrote:
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!!!!!



  #4  
Old January 6th 08, 10:08 PM posted to microsoft.public.outlook.program_vba
alexcraig
external usenet poster
 
Posts: 8
Default Problem with Object Reference for MailItem

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.

"JP" wrote:

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

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

HTH,
JP


On Jan 6, 1:06 pm, alexcraig
wrote:
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!!!!!




 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CPAO: object reference not set to an instance of an object Caroline Outlook - Calandaring 2 November 29th 08 04:32 AM
using outlook object model to add a MailItem to the Sent Items folder Omatase Outlook - General Queries 1 July 22nd 07 06:29 AM
Object reference not set to an instance of an object [email protected] Outlook - General Queries 0 May 17th 07 07:40 AM
Discarding the changes of mailItem object after saving it to the d AtulSureka Outlook - Using Forms 6 January 17th 07 02:50 PM
Help with 'MailItem' object of WordMail Inspector [email protected] Add-ins for Outlook 2 May 18th 06 07:28 PM


All times are GMT +1. The time now is 12:29 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.