Help with Code: Object Variable or With Block Variable Not Set

M

michaaal

Upon running the following code I get a message that says "Object Variable
or With Block Variable Not Set". My client computer is Outlook 2003.

Why is this happening?? Please help!!!




Private Sub UserForm_Activate()
Dim objApp As Application
Dim objNS As NameSpace
Dim objContacts As MAPIFolder

Set objNS = Application.GetNamespace("MAPI")
Set objContacts = GetFolder("Public Folders/All Public Folders/Contacts1")

Dim strDelim As String 'delimiter to search for
Dim iStrLen As Integer 'length of string
Dim iDelimLoc As Integer 'location of delimiter character
Dim iFound As Boolean 'success flag
strDelim = "," 'This is the delimiter
For x = 1 To objContacts.Items.Count - 1
strParse = Trim(objContacts.Items.Item(x).Categories)
iStrLen = Len(strParse)
iDelimLoc = InStr(strParse, strDelim) 'search for delimiter
If iDelimLoc Then iFound = True
Exist = "no"
Do While iDelimLoc > 0 'if found
If Trim(Left$(strParse, iDelimLoc - 1)) <> "" Then
If ListBox1.ListCount > 0 Then
For yy = 1 To ListBox1.ListCount
If Trim(Left$(strParse, iDelimLoc - 1)) = Trim(ListBox1.List(yy -
1)) Then Exist = "yes"
Next
End If
If Exist = "no" Then
ListBox1.AddItem (Trim(Left$(strParse, iDelimLoc - 1))) 'add just
the part before the delimiter
End If
End If
strParse = Mid$(strParse, iDelimLoc + 1) 'remove that item and
the first delimiter from the string
iDelimLoc = InStr(strParse, strDelim) 'search for delimiter
Loop
If iFound Then
If Trim(strParse) <> "" Then
Exist = "no"
For yy = 1 To ListBox1.ListCount
If Trim(strParse) = Trim(ListBox1.List(yy - 1)) Then Exist = "yes"
Next
If Exist = "no" Then
ListBox1.AddItem (Trim(strParse)) 'add the rest of the string
End If
End If
End If

If objContacts.Items.Item(x).Email1Address <> "" Then
ListBox2.AddItem (objContacts.Items.Item(x).Email1Address)
End If
If objContacts.Items.Item(x).Email2Address <> "" Then
ListBox2.AddItem (objContacts.Items.Item(x).Email2Address)
End If
If objContacts.Items.Item(x).Email3Address <> "" Then
ListBox2.AddItem (objContacts.Items.Item(x).Email3Address)
End If
Next
End Sub

Public Function GetFolder(strFolderPath As String) As MAPIFolder
Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim I As Long
On Error Resume Next

strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objFolder = objNS.Folders.Item(arrFolders(0))
If Not objFolder Is Nothing Then
For I = 1 To UBound(arrFolders)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arrFolders(I))
If objFolder Is Nothing Then
Exit For
End If
Next
End If

Set GetFolder = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function
 
S

Sue Mosher [MVP-Outlook]

Which statement produces that error?

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
M

Michael Bauer

Hi Michaaal,

it would be a good idea using an errorhandler in each procedure.

The error 91 is very simple to find. Just place a Breakpoint in your
code an step through it, watching line for line what is goíng on. In
this case: looking for a variable that is (unexpected) set to nothing.

I just guess, this folder doesn´t exist:
Set objContacts = GetFolder("Public Folders/All Public
Folders/Contacts1")

So trying to use a property (.items) of objContacts (which than is
nothing) would cause the error:
For x = 1 To objContacts.Items.Count - 1

BTW: The above line would skip the last contact. Do you know that? The
collection is 1-based, so the first item is item(1) and the last is
item(count).

On the other hand the ListBox is 0-based.
For yy = 1 To ListBox1.ListCount
If Trim(strParse) = Trim(ListBox1.List(yy - 1))

This will skip the first item (index=0), carries out an unnecessary
calculation in each loop (yy-1) and causes an error if yy=ListCount. So,
in this case you need to write:

For yy = 0 To ListBox1.ListCount-1
If Trim(strParse) = Trim(ListBox1.List(yy))
 
M

michaaal

Michael Bauer said:
Hi Michaaal,

it would be a good idea using an errorhandler in each procedure.

The error 91 is very simple to find. Just place a Breakpoint in your
code an step through it, watching line for line what is goíng on. In
this case: looking for a variable that is (unexpected) set to nothing.

I just guess, this folder doesn´t exist:

Folders/Contacts1")

So trying to use a property (.items) of objContacts (which than is
nothing) would cause the error:


BTW: The above line would skip the last contact. Do you know that? The
collection is 1-based, so the first item is item(1) and the last is
item(count).

On the other hand the ListBox is 0-based.


This will skip the first item (index=0), carries out an unnecessary
calculation in each loop (yy-1) and causes an error if yy=ListCount. So,
in this case you need to write:

For yy = 0 To ListBox1.ListCount-1
If Trim(strParse) = Trim(ListBox1.List(yy))

Michael,

You have given me some very good information. Thank you. Yes, actually,

The following folder actually does exist and I am certain the name is
correct...
Set objContacts = GetFolder("Public Folders/All Public Folders/Contacts1")

I was able to to get my code to work on a different computer, so that tells
me that the code is probably ok. But I really really really need for it to
work on the first computer.
And, Sue, the error is caused here...
For x = 1 To objContacts.Items.Count - 1

Michael, whenever I don't use a "- 1" on the above code I get errors. I
don't recall which ones, but I will look into it since it makes me skip the
last contact. Thanks for pointing that out.

Also, Michael, thanks for pointing out my "0" on the ListBox. I am going to
be doing away with the ListBox so it doesn't matter for now.

Could my entire problem be related to my GetFolder function? Is there
better code I should use to get the contacts from a Public Folder?

Paul
 
M

Michael Bauer

Hi Paul,
And, Sue, the error is caused here...
For x = 1 To objContacts.Items.Count - 1

that definitively means that objContacts is nothing. That is that the
function GetFolder has returned nothing. So you have to move your
breakpoint into this function and repeat your step by step watching :)
 

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