Solved

Cannot move Outlook emails with VBA

Posted on 2014-11-19
6
210 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
Comment Utility
Additional note:  It seems to move 1 email, then I get the error.
0
 

Author Comment

by:rrhandle8
Comment Utility
I removed the line Set oItem = oItems.FindNext and it works.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Find out how to use dynamic social media in email signatures with this top 10 DOs & DON’Ts.
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now