assistance-needed-on-error-91-object-variable or with block variable not set (1 Viewer)

Voyager

Registered User.
Local time
Today, 10:31
Joined
Sep 7, 2017
Messages
95
I am trying to use the below given procedure to download unique mails but getting error (91 Object variable or with block variable not set) Error in line : .FindFirst "task =""" & Mailobject.UserProperties.Find("taskID") & """"

Private Sub getml()
Dim rst As DAO.Recordset
Dim OlApp As Outlook.Application

Dim inbox As Outlook.MAPIFolder
Dim inboxItems As Outlook.Items
Dim Mailobject As Object
Dim db As DAO.Database
Dim dealer As Integer
Set db = CurrentDb

Set OlApp = CreateObject("Outlook.Application")
Set inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
Set rst= CurrentDb.OpenRecordset("mls")
Set inboxItems = inbox.Items
For Each Mailobject In inboxItems

With rst
.FindFirst "task =""" & Mailobject.UserProperties.Find("taskID") & """"
If .NoMatch
.AddNew
!task= Mailobject.UserProperties.Find("taskID")
!tsktml= Mailobject.UserProperties.Find("timeline")
.Update

Mailobject.UnRead = False
End If
End With
End If
Next
Set OlApp = Nothing
Set inbox = Nothing
Set inboxItems = Nothing
Set Mailobject = Nothing
End sub
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:01
Joined
May 7, 2009
Messages
19,169
put Mailobject.UserProperties.Find("taskID") first in a variable:

Dim var as Variant
var = Mailobject.UserProperties.Find("taskID")
With rs
.FindFirst "Task = " & Chr(34) & var & Chr(34)

...
...


try to debug your code, click on "Private Sub getm()" and press F9.
rerun your form.
when your code stop at the breakpoint, press F8 to step through.
hover to the value of var. Does it has a value?
 

Voyager

Registered User.
Local time
Today, 10:31
Joined
Sep 7, 2017
Messages
95
I have tried what you have suggested. When I hover the cursor over var it shows taskid = empty. But there are mails with taskid
Am I committing any mistakes
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:01
Joined
May 7, 2009
Messages
19,169
put Mailobject.UserProperties.Find("taskID") first in a variable:

Dim var as Variant
var = Mailobject.UserProperties.Find("taskID")
debug.print var
With rs
.FindFirst "Task = " & Chr(34) & var & Chr(34)

...
...


after assigning the value to (var) variable.
check its value on immediate window. if
it is blank then nothing was returned from
MailObject.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:01
Joined
Sep 21, 2011
Messages
14,048
If you indented your code, I believe you would spot the error?

Code:
Private Sub getml()
Dim rst As DAO.Recordset
Dim OlApp As Outlook.Application

Dim inbox As Outlook.MAPIFolder
Dim inboxItems As Outlook.Items
Dim Mailobject As Object
Dim db As DAO.Database
Dim dealer As Integer
Set db = CurrentDb

Set OlApp = CreateObject("Outlook.Application")
Set inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFold erInbox)
Set rst= CurrentDb.OpenRecordset("mls")
Set inboxItems = inbox.Items
For Each Mailobject In inboxItems

	With rst
		.FindFirst "task =""" & Mailobject.UserProperties.Find("taskID") & """"
		If .NoMatch
			.AddNew
			!task= Mailobject.UserProperties.Find("taskID")
			!tsktml= Mailobject.UserProperties.Find("timeline")
			.Update

			Mailobject.UnRead = False
		End If
	End With
[COLOR="Red"]End If[/COLOR]
Next
Set OlApp = Nothing
Set inbox = Nothing
Set inboxItems = Nothing
Set Mailobject = Nothing
End sub

HTH

I am trying to use the below given procedure to download unique mails but getting error (91 Object variable or with block variable not set) Error in line : .FindFirst "task =""" & Mailobject.UserProperties.Find("taskID") & """"

Private Sub getml()
Dim rst As DAO.Recordset
Dim OlApp As Outlook.Application

Dim inbox As Outlook.MAPIFolder
Dim inboxItems As Outlook.Items
Dim Mailobject As Object
Dim db As DAO.Database
Dim dealer As Integer
Set db = CurrentDb

Set OlApp = CreateObject("Outlook.Application")
Set inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
Set rst= CurrentDb.OpenRecordset("mls")
Set inboxItems = inbox.Items
For Each Mailobject In inboxItems

With rst
.FindFirst "task =""" & Mailobject.UserProperties.Find("taskID") & """"
If .NoMatch
.AddNew
!task= Mailobject.UserProperties.Find("taskID")
!tsktml= Mailobject.UserProperties.Find("timeline")
.Update

Mailobject.UnRead = False
End If
End With
End If
Next
Set OlApp = Nothing
Set inbox = Nothing
Set inboxItems = Nothing
Set Mailobject = Nothing
End sub
 

isladogs

MVP / VIP
Local time
Today, 05:01
Joined
Jan 14, 2017
Messages
18,186
If you indented your code, I believe you would spot the error?

Yes I agree but I believe you've now got an orphan End If without a home to go to....
I think it'll give a compile error
Happy to be proved wrong if I'm barking up a gum tree....

Not sure if its already been suggested but try adding the 2 lines in RED as shown below:

Code:
With rst
                [COLOR="Red"][B].MoveLast
                .MoveFirst[/B][/COLOR]
		.FindFirst "task =""" & Mailobject.UserProperties.Find("taskID") & """"

You may also wish to use Chr(34) as Arnel suggested in place of double quotes

Perhaps that'll fix it

EDIT: Just realised Gasman was saying to remove the End If not add it!
So we agree after all ....
 
Last edited:

Voyager

Registered User.
Local time
Today, 10:31
Joined
Sep 7, 2017
Messages
95
Hi Arnelgp / Gasman,
As you said I tried to debug the code. The var is capturing the taskid from the mails and the same is viewable in the immediate window. The error occurs after the .findfirst completes the entry for last mail item. Ideally after looking into the last mail item it has to go toowards end sub but the code is looping back to the var = and after completing the last entry var shows empty and reflects error.

I have removed the orphan if statement it was a mistake while typing the code the if statement is not there in my code.

My new code follows

Private Sub getml()
Dim rst As DAO.Recordset
Dim OlApp As Outlook.Application

Dim inbox As Outlook.MAPIFolder
Dim inboxItems As Outlook.Items
Dim Mailobject As Object
Dim db As DAO.Database
Dim dealer As Integer
Dim var as variant
Set db = CurrentDb

Set OlApp = CreateObject("Outlook.Application")
Set inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFold erInbox)
Set rst= CurrentDb.OpenRecordset("mls")
Set inboxItems = inbox.Items
For Each Mailobject In inboxItems
var = Mailobject.UserProperties.Find("taskID")
With rst
.FindFirst "task =""" & var & """"
If .NoMatch
.AddNew
!task= Mailobject.UserProperties.Find("taskID")
!tsktml= Mailobject.UserProperties.Find("timeline")
.Update

Mailobject.UnRead = False
End If
End With

Next
Set OlApp = Nothing
Set inbox = Nothing
Set inboxItems = Nothing
Set Mailobject = Nothing
End sub
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 05:01
Joined
Jan 14, 2017
Messages
18,186
Did you try adding the 2 lines I marked in RED on my last post?

Code:
With rst [COLOR="Red"][B] 
          .MoveLast
          .MoveFirst[/B][/COLOr]
          .FindFirst "task =....
 

JHB

Have been here a while
Local time
Today, 06:01
Joined
Jun 17, 2012
Messages
7,732
..
I have removed the orphan if statement it was a mistake while typing the code the if statement is not there in my code.
Only an advice/guidance.
Copy the code from the module, don't type it again. And ofcause set the code into code tags (press the "#" sign), when you post code, it is much easier to read.
 

Voyager

Registered User.
Local time
Today, 10:31
Joined
Sep 7, 2017
Messages
95
Hi Ridders, Yes I have tried that too but still getti same error
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:01
Joined
May 7, 2009
Messages
19,169
possible to ignore this error

Code:
Dim var as Variant
dim var2 as Variant

'... preceeding codes
'...
On Error Resume Next
For Each MailObject In InboxItems
	
	set var = MailObject.UserProperties.Find("taskID")
	set var2 = MailObject.UserProperties.Find("timeline")

	IF Not (var Is Nothing) Then
		With rs
			.FindFirst "task=" Chr(34) & var & Chr(34)
			If .NoMatch Then
				.AddNew
				!task = var.Value & ""
				!tskml = var2.Value & ""
				.Update
				
				MailObject.UnRead = False
			End If
		End With
	End If
Next
'... rest of code
'...
 

Cronk

Registered User.
Local time
Today, 16:01
Joined
Jul 4, 2013
Messages
2,770
Not all inbox items are necessarily mail items. It may be instructive to examine the item on which the error is generated.

In any case, you can exclude non mail items with
Code:
if  typename(Mailobject)="mailItem" then
 

Voyager

Registered User.
Local time
Today, 10:31
Joined
Sep 7, 2017
Messages
95
Thank you very much to all who helped me out. Especially Arnelgp your answers are always to the point.
 

Users who are viewing this thread

Top Bottom