Jump to content

Array: Subscript used on non-accessible variable


Recommended Posts

Hi i would like to do automation but always receiving this error, but weird thing it never happen with another pc.

My error: Subscript used on non-accessible variable.:

For $i = 1 to $aFile[0]
For $i = 1 to $aFile^ ERROR

here is my code;

 

Func CheckTemp()

    $vfile = "C:\Users\Username\Desktop\Sample.txt"
    Local $aFile
    Local $aFinal[0]

    _FileReadToArray($vFile, $aFile)

    For $i = 1 to $aFile[0]
        $sTemp = _StringBetween($aFile[$i], "Temperature", "Celcius")
        If $sTemp <> 0 Then _ArrayAdd($aFinal, $sTemp)
    Next

    If $aFinal[0] > 40 Then
        $oOApp = ObjCreate("Outlook.Application")
        $oOMail = $oOApp.CreateItem ($olMailItem)
        $oOMail.Save

        ;With $oOMail
            ;.To = ($to)
            ;.;Subject = $subj
            ;.Importance= $olImportanceHigh
            ;.HTMLBody = &body
            ;.attachments.add ("")
            ;.Send
        ;EndWith

        MsgBox($MB_SYSTEMMODAL, "Current Temperature", $aFinal[0], 2)

    EndIf

    $oOApp=0
    FileClose($vfile)

EndFunc

 

 

 

Edited by levila
Link to comment
Share on other sites

3 hours ago, levila said:

but weird thing it never happen with another pc.

@Subz is certainly right. The Sample.txt is missing or empty. On the other PC it is present, therefore no error message appears.

Here is a runable test version with extended error messages. Since you did not include an example of a Sample.txt, I created one myself for testing purposes.

#include <File.au3>
#include <String.au3>
#include <Array.au3>

Global $vfile
CheckTemp()

Func CheckTemp()
    ;$vfile = "C:\Users\Username\Desktop\Sample.txt"
    $vfile = @ScriptDir & "\Sample.txt" ;  ; *** only for test

    Local $aFile, $aFinal[0], $iError, $sErrorMessage

    _FileReadToArray($vFile, $aFile)
    $iError = @error
    If $iError <> 0 Then
        Switch $iError
            Case $iError = 1
                $sErrorMessage = "Error : Opening specified file"
            Case $iError = 2
                $sErrorMessage = "Error : Unable to split the file"
            Case $iError = 3
                $sErrorMessage = "Error : File lines have different numbers of fields"
            Case $iError = 4
                $sErrorMessage = "Error : No delimiters found"
            Case Else
                $sErrorMessage = "undefined error : "
        EndSwitch

        Exit MsgBox(BitOR(4096, 16), "Error", $sErrorMessage & @CRLF)
    EndIf

    _ArrayDisplay($aFile, "aFile :") ; *** only for test


    For $i = 1 to $aFile[0]
        $sTemp = _StringBetween($aFile[$i], "Temperature", "Celcius")
        If $sTemp <> 0 Then _ArrayAdd($aFinal, $sTemp)
    Next

    _ArrayDisplay($aFinal, "aFinal :") ; *** only for test

;~     If $aFinal[0] > 40 Then
;~         $oOApp = ObjCreate("Outlook.Application")
;~         $oOMail = $oOApp.CreateItem ($olMailItem)
;~         $oOMail.Save

;~         ;With $oOMail
;~             ;.To = ($to)
;~             ;.;Subject = $subj
;~             ;.Importance= $olImportanceHigh
;~             ;.HTMLBody = &body
;~             ;.attachments.add ("")
;~             ;.Send
;~         ;EndWith

;~         MsgBox($MB_SYSTEMMODAL, "Current Temperature", $aFinal[0], 2)
;~      EndIf

;~     $oOApp=0
;~     FileClose($vfile)

EndFunc

 

sample.txt

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

On 7/30/2022 at 11:04 PM, Zedna said:

or add test by FileExists() before _FileReadToArray() ...

Yes, actually i was looping the coding to save the file and then read the file again. the problem is the only PC seem taken abit longer than usual. so i just adding sleep and fileExist and if exist process to Check Temp() function, if not continue loop until fileexist.  thank you for your help.

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...