Solved

Cannot move Outlook emails with VBA

Posted on 2014-11-19
6
219 Views
Last Modified: 2014-12-06
I keep getting this error: Object variable or with block variable not set
The error is on line:    Set oItem = oItems.FindNext

Sub MoveToTrackingFolder()

 Dim oNS As Outlook.NameSpace
 Dim oFolder As Outlook.MAPIFolder
 Dim oItem As Object 'mailItem
 Dim cntShipDetails, i As Integer
 Dim oDestFolder As Outlook.MAPIFolder
 Dim oItems As Outlook.Items
  
 Set oNS = GetNamespace("MAPI")
 Set oFolder = oNS.Folders(EmailAccount)
 Set oFolder = oFolder.Folders("Inbox")

 Set oDestFolder = oNS.Folders(EmailAccount)
 Set oDestFolder = oDestFolder.Folders("Inbox")
 Set oDestFolder = oDestFolder.Folders("Tracking")

 For Each oItem In oFolder.Items
    If TypeName(oItem) = "MailItem" Then
    If Left(oItem.Subject, 31) = "Information about your order (#" And oItem.ReceivedTime > #11/19/2014# Then
    oItem.Move oDestFolder
    Set oItem = oItems.FindNext
    End If
    End If
 Next

End Sub

Open in new window

0
Comment
Question by:rrhandle8
  • 2
  • 2
6 Comments
 

Author Comment

by:rrhandle8
ID: 40454115
Additional note:  It seems to move 1 email, then I get the error.
0
 

Author Comment

by:rrhandle8
ID: 40454188
I removed the line Set oItem = oItems.FindNext and it works.
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40455205
Correct. You only call FindNext after having used Find(). But you can't use it here because a Find does not perform partitial matches, and it looks like this command was left over from a trial to use Find.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 40455266
You don't need the FindNext when iterating through messages the way you are.  FindNext is used after a Find command.  You are using Find, so FindNext won't work.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40460078
http:#a40455266 was just repeating what I said (plus a typo - you are not using Find), with a time difference of 18 minutes. That does not count as "simultanous" (the agreed time difference for "same time answers" is ~5 minutes), so that comment should not have accepted.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Outlook Free & Paid Tools
In this step by step procedure, you will come to know the details of creating an Outlook meeting in 2007, 2010, 2013 & 2016.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question