Solved

Cannot move Outlook emails with VBA

Posted on 2014-11-19
6
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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 70

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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 …

751 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