Jump to content

FOR...IN...NEXT skips items in Outlook


Recommended Posts

If I execute these lines on an Excel workbook, all (except last) worksheets are deleted.

For $sh In $oWB.Worksheets
   $sh.Delete
Next

But why, if I execute similar lines in an Outlook folder, about half of the mails remain?

I would expect all mailitems to be deleted.

For $j in $oParentFld.Items
   $j.Delete
Next

Test for yourself with full code (should be safe to run but proceed at own risk)

#include <Debug.au3>

_DebugSetup("Debug Out", False, 1)
_DebugOut("Debug start")

;-----------------------------------------------------------------------
; Run Excel test
;-----------------------------------------------------------------------
$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = True
$oExcel.SheetsInNewWorkbook = 1
$oWB = $oExcel.workbooks.add

for $i = 1 to 10
   _DebugOut("Adding worksheet...")
   $oWB.Sheets.Add
   _DebugOut("Now " & $oWB.Worksheets.Count & " sheets in workbook")
   Sleep(1000)
Next

For $sh In $oWB.Worksheets
   _DebugOut("Deleting sheet " & $sh.Name)
   $sh.Delete
   If @error Then _DebugOut("Error deleting")
   _DebugOut("Now " & $oWB.Worksheets.Count & " sheets in workbook")
   Sleep(1000)
Next

;-----------------------------------------------------------------------
; Run Outlook test
;-----------------------------------------------------------------------
$olApp       = ObjCreate("Outlook.Application")
$olNameSpace = $olApp.GetNameSpace("MAPI")
Dim $archive = $olNameSpace.PickFolder

Dim $Mail
for $i = 1 to 10
   _DebugOut("Adding mail #" & $i)
   $Mail = $olApp.CreateItem(0)
   $Mail.Subject = "Test mail (" & $i & ") " & @HOUR & ":" & @MIN & ":" & @SEC & "," & @MSEC
   $Mail.HTMLBody = "<HTML><p>test</p></HTML>"
   $Mail.save()
   Sleep(1000)
Next

$oParentFld = $Mail.Parent

For $j in $oParentFld.Items
   $j.Delete
   Sleep(1000)
Next

Andre

Link to comment
Share on other sites

Did you have a look at my OutlookEX UDF? Function _OL_FolderDelete with the correct flags set should delete all members.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water, thanks, I know the UDF.

But I am trying to address this strange behaviour in plain Autoscript. Should this not work?

EDIT:

In addition, deleting the folder is not my purpose, just happens to be in the test script above.

My real life code, where I found this issue, iterates through a folder with (e.g.) 100 mails.
While iterating I delete a few (e.g. 5).
Then I noticed the iteration completes after having processes 95 items, so the last 5 items are never processed. This is not what I would expect, and also not consistent with the behaviour when iterating Excel sheets.

Andre

Edited by apoliemans
Link to comment
Share on other sites

A UDF is plain AutoIt. So why not use it?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

A UDF is plain AutoIt. So why not use it?

​@water, my purpose is to learn the plain language myself, which works better by not using UDFs.

I think your UDFs are great (!) and I browse them for reference.

Just like you do in your UDF I started to bypass this problem by coding 'FOR..TO..STEP -1'.

I feel this is a bug (or at least an inconsistency in the language) and have opened a bug report for it.

https://www.autoitscript.com/trac/autoit/ticket/3043

Link to comment
Share on other sites

I do not think it is a bug.
After thinking about it for some minutes I'm sure it is a similar problem like deleting using the 'FOR..TO' (notice the missing STEP -1).

The FOR..TO expression is evaluated before every iteration.
Lets assume we have a collection of 3 mail items.
The first iteration deletes item 1 of 3 items.
The second iteration deletes item 2 of the newly evaluated collection of 2 items (= item 3 of the original collection).
The third iteration does nothing as the newly evaluated collection only consists of 1item.

In this example mail item 2 of the original collection stays undeleted.

If my assumtion is correct, this is no bug :)

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water, you assume that the 2nd iteration deletes item3 of the original set and I understand why you assume that.

My bug tracker testscript Outlook_test.au3 shows that:

- iteration #2 deletes mailitem #2 of the original set (as expected)

- iteration #3 deletes mailitem #3 of the original set (as expected)

- etc

Let's assume you start with 10 mailitems. The loop deletes original items #1 through #5 and then exits and continues right after NEXT.

It will leave original items #6 through #10 unprocessed.

It is as if below the covers a .Count is used that is re-evaluated with each loop.

If this is by design then that is OK by me, but then the Excel FOR..IN..NEXT should be changed to behave like this too.

Feel free to run the bug tracker attachments which are better versions of my code here in the forum.

Thanks for thinking this over.

Link to comment
Share on other sites

Right now I have no Excel/Outlook available so it was all untested.
But I will have a look as soon as I have access to my office computer again.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Tested high and low and get the same results.Don't know what's going on.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Maybe someone well versed into the inner working of COM objects will look/explain the behavior of the iterator associated with delete for mail containers. My uneducated point of view makes me suspect another MS bug or feature, as I believe this is outside AutoIt control. Note that I may be 100% wrong on this.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • Developers

Played a little and it seems to stop when the Items.count is equal to the number of loops ran and it thinks the For loop is done:

For $j in $oParentFld.Items
   $iteration += 1
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oParentFld.Items.count = ' & $oParentFld.Items.count & '   $iteration = ' & $iteration & @CRLF & '   $j.Subject = ' & $j.Subject & @CRLF) ;### Debug Console
   _DebugOut("Deleting mail, loop " & $iteration)
   $j.Delete
   Sleep(500)
Next

Doing it with a for-Next loop like this works fine:

For $j = $oParentFld.Items.count to 1 Step -1
   $iteration += 1
   _DebugOut("Deleting mail, loop " & $iteration)
   $oParentFld.Items.Item($j).Delete
   Sleep(500)
Next

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

As it works with Excel, do you think it is a bug in MS Outlook?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Double post - it happend again, this time without code box :(

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Did anybody try this with VBS to see if the same occurs or is that working?

Jos

​I rewrote my testcase to VBS (FOR EACH..IN..NEXT) and this is what happens:

[in VBS] deletes items #1 through #6, then exits, leaves items #7 through #10 untouched.

Compare to Autoit:

[in AUTOIT] deletes items #1 through #5, then exits, leaves items #6 through #10 untouched.

Link to comment
Share on other sites

So it seems to be a bug in Outlook.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Developers

I think it has to do with the fact that the deletion of the sheets happen in reverse order starting at Sheet 5, where with the emails they start with the first one created.

Jos

PS. The double posting is happening for me too in this thread. :(

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

If you want to delete all items you have to go backward. Like this maybe:

;...
For $i = $oParentFld.Items.Count To 1 Step -1
    ; $oParentFld.Items.Item($i).Delete()
    ; ...or using default Items method
    $oParentFld.Items($i).Delete()
Next

...As said already.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...