Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cannot move Outlook emails with VBA

Posted on 2014-11-19
6
Medium Priority
?
229 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 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
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 …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

972 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