levila Posted July 29, 2022 Posted July 29, 2022 (edited) 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 July 29, 2022 by levila
Subz Posted July 29, 2022 Posted July 29, 2022 (edited) Use error checking for example: _FileReadToArray($vFile, $aFile) If @Error Then Exit MsgBox(4096, "Error", "Error reading file to Array") Edited July 29, 2022 by Subz levila 1
Musashi Posted July 29, 2022 Posted July 29, 2022 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. expandcollapse popup#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 levila 1 "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."
levila Posted July 29, 2022 Author Posted July 29, 2022 Yah i think i found what was the main issue right now, thank you @Subz, @Musashi
Zedna Posted July 30, 2022 Posted July 30, 2022 or add test by FileExists() before _FileReadToArray() ... Resources UDF ResourcesEx UDF AutoIt Forum Search
levila Posted August 1, 2022 Author Posted August 1, 2022 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now