问题
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