sherlocked
Registered User.
- Local time
- Today, 13:27
- 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.
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