Error while Looping Through conditions for Outlook VBA Code

亡梦爱人 提交于 2019-12-11 15:54:36

问题


My original Question is here

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

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

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

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

Next Rowcount

EDIT

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

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


回答1:


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


来源:https://stackoverflow.com/questions/57612681/error-while-looping-through-conditions-for-outlook-vba-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!