Searching Outlook Inbox sub-folder from VBA (1 Viewer)

sherlocked

Registered User.
Local time
Today, 04:09
Joined
Sep 22, 2014
Messages
125
Experts,

I have Googled this to no avail and come hoping you can point me in the right direction. Below is the code I am using to search for a particular email in an Inbox sub-folder of an email address. This sub-folder is renamed each day with the current date. This process can't be changed, so I am using a variable to pass the name of the subfolder each time the code is run.

I am getting a "With variable or Object variable not set" error message when I try to run this. It throws the debug line at the "Set myitems" part of the code. Any ideas what I may be doing wrong? I have never done anything like this before and found most of the below code by Googling how to perform the function I'm trying to accomplish.

Your help, as always, is greatly appreciated.

Code:
Private Sub btnView_Click()

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myInbox2 As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim varRecipient As Outlook.Recipient
Dim myitem As Object
Dim Found As Boolean
Dim varInqy As String
Dim varDate As Date

varDate = Date
varInqy = Me.NPICInqyNo

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set varRecipient = myNameSpace.CreateRecipient("LIAISON, NPIC")

If varRecipient.Resolve Then
  Set myInbox = myNameSpace.GetSharedDefaultFolder(varRecipient, olFolderInbox)
  Set myInbox2 = myInbox.folders(" & varDate & ")
End If

Set myitems = myInbox2.Items
Found = False
varInqy = Me.NPICInqyNumber

For Each myitem In myitems
    If myitem.Class = olMail Then
        If InStr(1, myitem.Body, " & varInqy & ") > 0 Then
            myitem.Display
            Found = True
        End If
    End If
Next myitem
   
If Not Found Then
    MsgBox "No email with this inquiry number was found.", vbOKOnly + vbInformation
End If
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:09
Joined
Sep 21, 2011
Messages
14,351
I'd walk through the code using F8 step by step.
If varRecipient.Resolve is not true, then myInbox and myInbox2 are not set.
Look to see what that object is, but walking through the code generally gets me there.

Is the code doing what you think it should be doing?
 

sherlocked

Registered User.
Local time
Today, 04:09
Joined
Sep 22, 2014
Messages
125
I have used F8 to walk through the code. The recipient.resolve seems to be working fine; it is at the Set myitems portion that it throws the error.

If I do not add the step where the code searches for the subfolder (just leave it as the default inbox) the code works fine.
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:09
Joined
Sep 21, 2011
Messages
14,351
So what properties do you have for the object myInbox2?, myInBox even?
 

Cronk

Registered User.
Local time
Today, 21:09
Joined
Jul 4, 2013
Messages
2,772
Set myInbox2 = myInbox.folders("ABC")
will set a reference to a sub folder 'ABC'

Set myInbox2 = myInbox.folders(" & varDate & ")
will set a reference to a sub folder ' & varDate & '

If the sub folder is 08/03/2017 then use
Set myInbox2 = myInbox.folders(chr(34) & format(varDate,"mm/dd/yyyy") & chr(34))

to set a reference to a subfolder '08/03/2017'
 

sherlocked

Registered User.
Local time
Today, 04:09
Joined
Sep 22, 2014
Messages
125
Thank you kindly for your response. I am now getting an "object could not be found" error at the "set myInbox" line. I'm thinking it can't find the folder being referred to :(
 

Cronk

Registered User.
Local time
Today, 21:09
Joined
Jul 4, 2013
Messages
2,772
Is the folder shared and do you have permissions to access it?
 

Users who are viewing this thread

Top Bottom