Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Time Error 91

Status
Not open for further replies.

tractorvix

Technical User
Jun 29, 2004
122
GB
I have a database set up to automatically mail out tests and then process the results. It pulls the data from Outlook and pops it into a table.

I can upload the information from Outlook without any problems on my PC, but if my colleague tries to, she gets a run time error 91 Object variable or With Block Variable not set.

I've checked that the references are set up correctly and it was working fine yesterday (so frustrating!)

Code is below:

Public Sub MAPIImportMessages()
Dim db As Database, rs As Recordset
Dim objRecipient As Recipient
Dim objAttachment As Attachment
Dim stOut As String

On Error GoTo MAPIImportMessages_Error

If Not mboolErr Then
Set db = CurrentDb
Set rs = db.OpenRecordset(mstTable, dbOpenDynaset)
'***Must change this to QUERIES somehow

Set mobjMsgColl = mobjFolder.Messages
If Not 0 = mobjMsgColl.Count Then
Set mobjMessage = mobjMsgColl.GetFirst()
Do While Not mobjMessage Is Nothing
If UCase(Left(mobjMessage.Subject, 12)) = "PROCESSED - " Then
'do nowt already processed
'mlngCount = mlngCount + 1
mstStatus = SysCmd(acSysCmdSetStatus, "Imported " & mlngCount & " message(s)....")
Set mobjMessage = mobjMsgColl.GetNext
Else
With rs
.AddNew
!Class = mobjMessage.Class
!FolderID = mobjMessage.FolderID
!ID = mobjMessage.ID

stOut = vbNullString
For Each objRecipient In mobjMessage.Recipients
stOut = stOut & objRecipient.Name & " (" _
& objRecipient.Address & ") ;"
Next
'some emails don't have your name in the To: field
If mobjMessage.Recipients.Count > 0 Then
stOut = Left$(stOut, Len(stOut) - 2)
!Recipients = stOut
End If

stOut = vbNullString
'Attachments at the moment are generating
'E_OutofMemory error code.
'
'For Each objAttachment In mobjMessage.Attachments
' stOut = stOut & objAttachment.Name & ";"
' Next
'If mobjMessage.Attachments.Count > 0 Then
' stOut = Left$(stOut, Len(stOut) - 1)
' !Attachments = stOut
' End If

!SenderEmailAddress = mobjMessage.Sender.Address
!Sender = mobjMessage.Sender.Name

'!Sensitivity = mobjMessage.Sensitivity
'!MsgSize = mobjMessage.Size
'!StoreID = mobjMessage.StoreID
!Subject = mobjMessage.Subject

!MessageBody = mobjMessage.Text
'!TimeCreated = mobjMessage.TimeCreated
'!TimeLastModified = mobjMessage.TimeLastModified
!TimeReceived = mobjMessage.TimeReceived
!TimeSent = mobjMessage.TimeSent
mobjMessage.Subject = "Processed - " & mobjMessage.Subject
mobjMessage.Update True
.Update
mlngCount = mlngCount + 1
mstStatus = SysCmd(acSysCmdSetStatus, "Imported " & mlngCount & " message(s)....")
Set mobjMessage = mobjMsgColl.GetNext
End With
End If
Loop
End If
End If
Set rs = Nothing
Set db = Nothing
stOut = "Imported " & mlngCount & " messages from the folder '" & mobjFolder.Name & "'."
MsgBox stOut, vbOKOnly, "Success!!"

MAPIImportMessages_Exit:
Exit Sub

MAPIImportMessages_Error:
stOut = "Finished importing " & mlngCount & " Messages." & vbCrLf
stOut = stOut & "Couldn't import the message titled " & vbCrLf
stOut = stOut & "'" & mobjMessage.Subject & "'." & vbCrLf & "Aborting!" & vbCrLf
stOut = stOut & "Error returned was:" & vbCrLf
stOut = stOut & Err & ": " & Err.Description

MsgBox stOut, vbCritical + vbOKOnly, "Critical error encountered!"
Set mobjMessage = Nothing
Set mobjMsgColl = Nothing
Set mobjFolder = Nothing
mobjSession.Logoff
Set mobjSession = Nothing
Resume MAPIImportMessages_Exit
Resume

End Sub

This is not something I've written, so bear with me if I'm a bit thick about the responses.

Any help / Advice would be greatly appreciated

Thanks

Vix
 
Do you know which line? You have four objects that may not be set, it would be a lot easier if we know which one is causing the problem.
One thing, VB has a lot of objects with the same name. I always uniquely identify them.
Example
Dim rs as DAO.recordset
and
Dim rs as ADODB.recordset

another.
dim myControl as access.control
dim myControl as MSforms.control

You did not do this so what can happen is vb looks for a reference and come to the first one that matches. Even though you both have DAO and ADODB referenced, she might have ADODB higher in the priority. You meant a DAO recordset, and it finds ADODB listed first. It therefore assumes you wanted an ADODB recordset not a DAO recordset. This is not likely your problem though, because usually the error would be something like "object does not support this property or method", but try it anyways, and get in this habit to save a lot of frustration down the road.


 
Thanks for the reply MajP. Have checked the references and they are in the right order.

Will have to wait until tomorrow to find the line of code that's causing the problem.

So frustrating!

Vix
 
If I was guessing, I would say the most likely place this error could happen would be here:

Set mobjMsgColl = mobjFolder.Messages

I assume that these are global, or module level variables. If mobjFolder is not set or out of scope for some reason, you would get that error. Since mobjFolder is set somewhere outside this procedure, the problem of why it is not being set could actually be in another procedure.
 
Morning MajP

The only reference I can find to mobjfolder is in the declarations at the top

Option Compare Database
Option Explicit

Private Const mcERR_DOH = vbObjectError + 10000
Private Const mcERR_DECIMAL = 261144 'low word order +1000
Private Const mcMAXFLD = 16

Private mobjSession As MAPI.Session
Private mobjFolder As Folder
Private mobjMessage As Message
Private mobjMsgColl As Messages
Private mlngFolderType As Long
Private mstStatus As String
Private mstTable As String
Private mstFolderName As String
Private mastFld(0 To mcMAXFLD, 1) As String
Private mboolErr As Boolean
Private mlngCount As Long

is this sufficient?

Vix
 
That is where the variable is declared, but somewhere (probably in another procedure) it has to be set to something. I am not familiar with Outlook objects, but somewhere in your code there should be something like.

Set mobjFolder = someobjectmodelthatreturns.folder

If this does not take place somewhere then, mobjFolder is "nothing", and this would give you an error.
Set mobjMsgColl = mobjFolder.Messages
Because you are asking "nothing" to return its "Messages" collection.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top