Sign in to follow this  
Followers 0
pete1234

Arrays and For/Next loops

3 posts in this topic

This is more of a conceptual question. I have the following code:

#Include <Array.au3>

$BackupFiles = _ArrayCreate("C:\1.xls", "C:\2.xls", "C:\3.xls")

$xlApp = ObjCreate("Excel.Application")
If Not IsObj($xlApp) Then Exit

With $xlApp
    .Visible = False
    .Workbooks.Open("C:\BackupMacros.xls")
    For $i = 0 to UBound($BackupFiles) - 1
        .Run("AutoItBackup(" & chr(34) & $BackupFiles[$i] & chr(34) & ")")
    Next
    .Quit
    msgbox(0, "", "done")
EndWith

What I'm basically doing is running a macro on a bunch of different Excel files. This all works just as expected. The issue I've noticed is that if my array has only 2 elements, my code runs the macro on the first element twice. So if I change my array to only include 2 files instead of 3, and make no other changes, the macro I'm executing will run on the first file twice. If the array has 3 or more file this doesn't happen. Does anyone know what's going on?

Thanks.

Share this post


Link to post
Share on other sites



If I remember correctly, you shouldn't be using _ArrayCreate anyways. One of the updates says to stop using it and use the built in array initializers.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I can't reproduce this problem. Also, like Richard said, don't use _ArrayCreate anymore (It's not in the latest SciTE anymore, even.)

I had to comment out the Excel lines, because I don't have the 1.xls, 2.xls and 3.xls files.

#include <Array.au3>

Dim $BackupFiles[2] = ["C:\1.xls", "C:\2.xls"]

;$xlApp = ObjCreate("Excel.Application")
;If Not IsObj($xlApp) Then Exit

;With $xlApp
;    .Visible = False
;    .Workbooks.Open("C:\BackupMacros.xls")
    For $i = 0 to UBound($BackupFiles) - 1
        ConsoleWrite("$BackupFiles[" & $i & "] = " & $BackupFiles[$i] & @CRLF)
;        .Run("AutoItBackup(" & chr(34) & $BackupFiles[$i] & chr(34) & ")")
    Next
;    .Quit
;    msgbox(0, "", "done")
;EndWith

$BackupFiles = _ArrayCreate("C:\1.xls", "C:\2.xls")

;$xlApp = ObjCreate("Excel.Application")
;If Not IsObj($xlApp) Then Exit

;With $xlApp
;    .Visible = False
;    .Workbooks.Open("C:\BackupMacros.xls")
    For $i = 0 to UBound($BackupFiles) - 1
        ConsoleWrite("$BackupFiles[" & $i & "] = " & $BackupFiles[$i] & @CRLF)
;        .Run("AutoItBackup(" & chr(34) & $BackupFiles[$i] & chr(34) & ")")
    Next
;    .Quit
;    msgbox(0, "", "done")
;EndWith
Edited by Manadar

Share this post


Link to post
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
Sign in to follow this  
Followers 0