Solved

Cannot move Outlook emails with VBA

Posted on 2014-11-19
6
213 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 68

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 68

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
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…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now