Outlook Code Problem On Email Moves - Error -2147221233

Status
Not open for further replies.
E

ejek6337

Please review the following code that moves emails between different outlook

box folders and subsequently creates an excel spreadsheet. This code has

always worked, but for some reason it started not working on a few people's

computers. It still works on mine however. They have the same references as

me loaded, namely the VBA, Outlook 11, Office 11, and OLE Automation

references. Here is the code:

' Create session so that security prompt is not displayed in outlook

Set olapp = Application

Set Session = olapp.Session

Set AL = olapp.Session.AddressLists("Global Address List")

Set fld =

olapp.GetNamespace("Mapi").GetFolderFromID("000000000F7EA95D623B0B4191BB263A85023FAC010079A7F373FA835448AE050AECC8235EE1000000E0C1070000")

Set fldSB =

olapp.GetNamespace("Mapi").GetFolderFromID("000000001A447390AA6611CD9BC800AA002FC45A0300B72F7F36FCD61A408C03CA765846D34D0000006EADB40000")

Set fldMoveTemp =

olapp.GetNamespace("Mapi").GetFolderFromID("000000000F7EA95D623B0B4191BB263A85023FAC01007953D6860919C04DB07FDB35A24059AA0000059D53060000")

Set fldMoveFinal =

olapp.GetNamespace("Mapi").GetFolderFromID("000000000F7EA95D623B0B4191BB263A85023FAC01007953D6860919C04DB07FDB35A24059AA0000059D53070000")

If Not fld Is Nothing Then

'a) count the mail items in the folder

intTotalItems = fld.Items.Count

ErrorCount = 0

'3) set the location of the storage file and

' create the Excel worksheet

Dim objWkb As Object 'Excel.Workbook

Dim objWks As Object 'Excel.Worksheet

Dim objExcel As Object 'Excel.Application

Dim i As Integer, j As Integer

'Set objExcel = New Excel.Application

Set objExcel = CreateObject("Excel.Application")

Set objWkb = objExcel.Workbooks.Add

Set objWks = objExcel.ActiveSheet

objWks.Cells(1, 1).Value = "Subject"

objWks.Cells(1, 2).Value = "Received"

objWks.Cells(1, 3).Value = "Sender Name"

objWks.Cells(1, 4).Value = "EMAIL"

objWks.Cells(1, 5).Value = "Body"

objWks.Cells(1, 6).Value = "Notes"

'4) Loop through all emails in the Rome CSBASES Outlook folder and move

them into the Archive Temp Folder

SubRoutine = "CSBASES"

i = fld.Items.Count

Do While (i - ErrorCount) > 0

For Each itm In fld.Items

DoEvents

If itm.Class = olMail Then

itm.Move (fldMoveTemp) ' Problem occurring here in

some cases with error -2147221233 Automation Error

End If

Next_CSBASES:

Next itm

i = fld.Items.Count

Loop
 
The error is MAPI_E_NOT_FOUND

You are modifying the collection in the "for each" loop.

Use

for i = Items.Coiunt to 1 Step -1

loop instead

Dmitry Streblechenko (MVP)

-

"ejek6337" <ejek6337@yahoo.com(donotspam)> wrote in message

news:57771D77-D794-406A-9A8C-A8369C2B1693@microsoft.com...
> Please review the following code that moves emails between different
> outlook
> box folders and subsequently creates an excel spreadsheet. This code has
> always worked, but for some reason it started not working on a few
> people's
> computers. It still works on mine however. They have the same references
> as
> me loaded, namely the VBA, Outlook 11, Office 11, and OLE Automation
> references. Here is the code:

> ' Create session so that security prompt is not displayed in outlook
> Set olapp = Application
> Set Session = olapp.Session
> Set AL = olapp.Session.AddressLists("Global Address List")
> Set fld =
> olapp.GetNamespace("Mapi").GetFolderFromID("000000000F7EA95D623B0B4191BB263A85023FAC010079A7F373FA835448AE050AECC8235EE1000000E0C1070000")
> Set fldSB =
> olapp.GetNamespace("Mapi").GetFolderFromID("000000001A447390AA6611CD9BC800AA002FC45A0300B72F7F36FCD61A408C03CA765846D34D0000006EADB40000")
> Set fldMoveTemp =
> olapp.GetNamespace("Mapi").GetFolderFromID("000000000F7EA95D623B0B4191BB263A85023FAC01007953D6860919C04DB07FDB35A24059AA0000059D53060000")
> Set fldMoveFinal =
> olapp.GetNamespace("Mapi").GetFolderFromID("000000000F7EA95D623B0B4191BB263A85023FAC01007953D6860919C04DB07FDB35A24059AA0000059D53070000")

> If Not fld Is Nothing Then

> 'a) count the mail items in the folder
> intTotalItems = fld.Items.Count
> ErrorCount = 0

> '3) set the location of the storage file and
> ' create the Excel worksheet
> Dim objWkb As Object 'Excel.Workbook
> Dim objWks As Object 'Excel.Worksheet
> Dim objExcel As Object 'Excel.Application
> Dim i As Integer, j As Integer

> 'Set objExcel = New Excel.Application
> Set objExcel = CreateObject("Excel.Application")
> Set objWkb = objExcel.Workbooks.Add
> Set objWks = objExcel.ActiveSheet

> objWks.Cells(1, 1).Value = "Subject"
> objWks.Cells(1, 2).Value = "Received"
> objWks.Cells(1, 3).Value = "Sender Name"
> objWks.Cells(1, 4).Value = "EMAIL"
> objWks.Cells(1, 5).Value = "Body"
> objWks.Cells(1, 6).Value = "Notes"

> '4) Loop through all emails in the Rome CSBASES Outlook folder and move
> them into the Archive Temp Folder
> SubRoutine = "CSBASES"
> i = fld.Items.Count
> Do While (i - ErrorCount) > 0
> For Each itm In fld.Items
> DoEvents
> If itm.Class = olMail Then
> itm.Move (fldMoveTemp) ' Problem occurring here in
> some cases with error -2147221233 Automation Error
> End If
> Next_CSBASES:
> Next itm
> i = fld.Items.Count
> Loop
 
I gave it a try on mine and it worked. I just need to try it on the other

personnels' computers and will let you know. Thanks.

Ed

"Dmitry Streblechenko" wrote:


> The error is MAPI_E_NOT_FOUND
> You are modifying the collection in the "for each" loop.
> Use

> for i = Items.Coiunt to 1 Step -1

> loop instead

> > Dmitry Streblechenko (MVP)
>

>

>

> -
> "ejek6337" <ejek6337@yahoo.com(donotspam)> wrote in message
> news:57771D77-D794-406A-9A8C-A8369C2B1693@microsoft.com...
> > Please review the following code that moves emails between different
> > outlook
> > box folders and subsequently creates an excel spreadsheet. This code has
> > always worked, but for some reason it started not working on a few
> > people's
> > computers. It still works on mine however. They have the same references
> > as
> > me loaded, namely the VBA, Outlook 11, Office 11, and OLE Automation
> > references. Here is the code:
> > ' Create session so that security prompt is not displayed in outlook
> > Set olapp = Application
> > Set Session = olapp.Session
> > Set AL = olapp.Session.AddressLists("Global Address List")
> > Set fld =
> > olapp.GetNamespace("Mapi").GetFolderFromID("000000000F7EA95D623B0B4191BB263A85023FAC010079A7F373FA835448AE050AECC8235EE1000000E0C1070000")
> > Set fldSB =
> > olapp.GetNamespace("Mapi").GetFolderFromID("000000001A447390AA6611CD9BC800AA002FC45A0300B72F7F36FCD61A408C03CA765846D34D0000006EADB40000")
> > Set fldMoveTemp =
> > olapp.GetNamespace("Mapi").GetFolderFromID("000000000F7EA95D623B0B4191BB263A85023FAC01007953D6860919C04DB07FDB35A24059AA0000059D53060000")
> > Set fldMoveFinal =
> > olapp.GetNamespace("Mapi").GetFolderFromID("000000000F7EA95D623B0B4191BB263A85023FAC01007953D6860919C04DB07FDB35A24059AA0000059D53070000")
> > If Not fld Is Nothing Then
> > 'a) count the mail items in the folder
> > intTotalItems = fld.Items.Count
> > ErrorCount = 0
> > '3) set the location of the storage file and
> > ' create the Excel worksheet
> > Dim objWkb As Object 'Excel.Workbook
> > Dim objWks As Object 'Excel.Worksheet
> > Dim objExcel As Object 'Excel.Application
> > Dim i As Integer, j As Integer
> > 'Set objExcel = New Excel.Application
> > Set objExcel = CreateObject("Excel.Application")
> > Set objWkb = objExcel.Workbooks.Add
> > Set objWks = objExcel.ActiveSheet
> > objWks.Cells(1, 1).Value = "Subject"
> > objWks.Cells(1, 2).Value = "Received"
> > objWks.Cells(1, 3).Value = "Sender Name"
> > objWks.Cells(1, 4).Value = "EMAIL"
> > objWks.Cells(1, 5).Value = "Body"
> > objWks.Cells(1, 6).Value = "Notes"
> > '4) Loop through all emails in the Rome CSBASES Outlook folder and move
> > them into the Archive Temp Folder
> > SubRoutine = "CSBASES"
> > i = fld.Items.Count
> > Do While (i - ErrorCount) > 0
> > For Each itm In fld.Items
> > DoEvents
> > If itm.Class = olMail Then
> > itm.Move (fldMoveTemp) ' Problem occurring here in
> > some cases with error -2147221233 Automation Error
> > End If
> > Next_CSBASES:
> > Next itm
> > i = fld.Items.Count
> > Loop


>
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
P How to get a QR code for automatic signin with Outlook for iOS Using Outlook 5
S HTML Code Embedded in String Within Open Outlook Email Preventing Replace(Application.ActiveInspector.CurrentItem.HTMLBody From Working Outlook VBA and Custom Forms 4
P Color Code or highlight folders in Outlook 2016 Using Outlook 2
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0
S Excel vba code to manage outlook web app Using Outlook 10
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
I Outlook 2003 shows html code when To: field is empty Using Outlook 7
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
Z Outlook Custom Form: Adding Dropdown(Project Code) at the end of subject Outlook VBA and Custom Forms 0
dyny723 Outlook 2016: Code to link a contact to emails received from that contact Outlook VBA and Custom Forms 1
M code to move selected Outlook contacts to another folder Using Outlook 3
E Outlook Form - Voting Responses Not Auto Processing If Form Contains Any Code Outlook VBA and Custom Forms 0
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
J Outlook 2010 VBScript editor does not run code at all Outlook VBA and Custom Forms 0
L Outlook 2007 Contact Form Code Not Working Using Outlook 20
M VBA Code to Restart Outlook on error Outlook VBA and Custom Forms 3
Z Default VBA code for extracting data from email (Outlook) to Excel? Outlook VBA and Custom Forms 1
M VBA Auto-Reply code for Two Mailboxes on one Outlook Session. Outlook VBA and Custom Forms 4
S Outlook 2010 I am getting error code 0x8DE00006 'the operation failed'. outlook 2010 send/receive progress Using Outlook.com accounts in Outlook 2
H Where to post code in Outlook Outlook VBA and Custom Forms 1
G Custom Form code not working - Outlook 2010 Outlook VBA and Custom Forms 2
Kelli VBA code for Outlook Using Outlook 1
Wotme code to run outlook add-in Using Outlook 1
S Outlook code to validate textbox value Using Outlook 1
M VBA Code to extract data from an Outlook Form Using Outlook 0
M VBA code needed to move from Outlook 2010 subfolder to Symantec Vault subfolde Using Outlook 0
C In need of VBA code to read / parse HTML - Outlook emails Using Outlook 0
L Outlook 2007 Code for Note Fields of Appointment Using Outlook 102
R Private Sub Application_ItemSend() -Cannot make this code work in Outlook 2013 Using Outlook 0
R [VBA] complicated(?) outlook events - need help with code Using Outlook 15
C Error Code -3253 Outlook for MAC Using Outlook 0
D Outlook Add-In That Calculates Time Based on Area Code Using Outlook 8
R how to get Outlook VBA code to work on the current folder Using Outlook 3
J VBA code can't be completely executed in outlook 2013 Using Outlook 0
P VBA Code being completely ignored by Outlook Using Outlook 7
M Updating VBA code from outlook 2007 to outlook 2010 Using Outlook 1
A Outlook VB Code Using Outlook 1
B How do I create and send mail in Outlook 2003 from code? Using Outlook 5
J Create or Import a Outlook Rule through C# code. Outlook VBA and Custom Forms 2
C compile error on previously functioning code outlook 2003 windows Outlook VBA and Custom Forms 1
J Executing Ribbon Command from Code Outlook 2010 Outlook VBA and Custom Forms 3
N why does vb not recognise outlook code? Outlook VBA and Custom Forms 1
D Code to clear certain fields in Outlook Form Outlook VBA and Custom Forms 7
H want to launch outlook with attachemnet for new mail with c# code Outlook VBA and Custom Forms 3
T forcing a rule when opening outlook via code Outlook VBA and Custom Forms 1
O Scheduled Task Fails running Code to send Outlook Mail from Excel. Outlook VBA and Custom Forms 9
A Accessing .pst folder in outlook using VBA Code Outlook VBA and Custom Forms 4
T Outlook setting through code(in >tools >options) Outlook VBA and Custom Forms 1
P 'Trusted' VBA code in Outlook 2003? Outlook VBA and Custom Forms 2

Similar threads

Back
Top