循环遍历Outlook VBA代码的条件时出错 [英] Error while Looping Through conditions for Outlook VBA Code

查看:114
本文介绍了循环遍历Outlook VBA代码的条件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的原始问题在这里

但是我认为我创建的代码太复杂了.我将其简化如下,现在它只有一个数组.

but i figured that the code i created was too complex. I simplified it as below, now it only has one array.

使用VBA在数组中循环,将Outlook电子邮件从一个文件夹移动到另一个文件夹? -原始问题

Looping through Arrays with VBA, to Move outlook emails from one folder to another? - original question


Sub MovingEmails_Invoices()

  'Declare your Variables
    Dim NS As Outlook.Namespace
    Dim rootfol As Outlook.Folder
    Dim Fol As Outlook.Folder
    Dim subfolder As Outlook.Folder


    'Set Outlook Inbox Reference
    Set OP = New Outlook.Application
    Set NS = OP.GetNamespace("MAPI")
    Set rootfol = NS.Folders(7)

    ' loop through subfolder and its folder
    Set Fol = rootfol.Folders("Austria")
    Set subfolder = rootfol.Folders("Austria").Folders("MOVE")

'The list for invoice numbers and folders is dynamic
'Each subject being searched is different

Dim Listmails() As Variant
Dim Rowcount As Long
Dim Mailsubject As Variant
Dim MS As String
Dim myrestrictitem As Outlook.items
Dim myItem As Outlook.Mailitem

'Establish the array based on the mailbox extract
  Sheets("files").Activate
  Listmails = Range("A2").CurrentRegion

'Ititerate through the array which is dynamic (One-dimensional)
For Rowcount = LBound(Listmails) To UBound(Listmails)

   '3rd row for email subject 'used DASL Filter
    Mailsubject = Application.WorksheetFunction.Index(Listmails, Rowcount, 3)
    MS = "@SQL=""urn:schemas:mailheader:subject"" LIKE \'%" & Mailsubject &"%\'"

    'Find the email based on the array for email subject
    Set myitems = Fol.items
    Set myrestrictitem = myitems.Restrict(MS)

    For i = myrestrictitem.Count To 1 Step -1
    Set item = myrestrictitem.item(i)
    myrestrictitem(i).Move subfolder

Next Rowcount

End Sub

该错误是由以下原因引起的:并且错误是它是无效的Next控件变量引用"

The error is caused by; and the error is 'it is an invalid Next control Variable reference'

Next Rowcount

EDIT

EDIT

我根据以下建议重新编辑了代码,谢谢,但是语法'Rowcount'仍然有错误

I re-edited my code according recommendations below, thank you however i still have an error for the syntax 'Rowcount'

错误消息是它是无效的Next控件变量引用"

The error message, is that 'it is an invalid Next control Variable reference'

The list for invoice numbers and folders is dynamic
'Each subject being searched is different

Dim Listmails() As Variant
Dim Rowcount As Long
Dim Mailsubject As Variant
Dim MS As String
Dim myrestrictitem As Outlook.items
Dim myItem As Outlook.Mailitem

'Establish the array based on the mailbox extract
  Sheets("files").Activate
  Listmails = Range("A2").CurrentRegion

'Ititerate through the array which is dynamic (One-dimensional)
For Rowcount = LBound(Listmails) To UBound(Listmails)

   '3rd row for email subject 'used DASL Filter
    Mailsubject = Application.WorksheetFunction.Index(Listmails, Rowcount, 3)
    MS = "@SQL=""urn:schemas:mailheader:subject"" LIKE \'%" & Mailsubject & "%\'"

    'Find the email based on the array for email subject
    Set myitems = Fol.items
    Set myrestrictitem = myitems.Restrict(MS)

    For i = myrestrictitem.Count To 1 Step -1
    myrestrictitem(i).Move subfolder

    Next i
Next Rowcount

End Sub

推荐答案

您的"for"循环是错误的:

Your "for" loop is wrong:

Dim i As Integer
For i = myrestrictitem.Count To 1 Step -1
  Set item = myrestrictitem.item(i)
  myrestrictitem(i).Move subfolder
next

这篇关于循环遍历Outlook VBA代码的条件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆