Jump to content

[SOLVED] A way to not end a script when "Subscript used on non-accessible variable" occurs?


Recommended Posts

Morning, I am wondering is there is a way to prevent a script from ending/crashing when a "Subscript used on non-accessible variable" error occurs? 

Here is some of my code:

$aItems = _OL_ItemFind($oOutlook, $mFolder[1], $olMail, "[UnRead]=True", "Subject", "Maintenance Request", "EntryID,Subject", "", 0)
    If @error Then
        Sleep(2000)
        LoopUnreadEmails()
        ;Exit MsgBox(48, "", "@error = " & @error & ", @extended: " & @extended & " at line: " & @ScriptLineNumber)
    EndIf

Error Msg:

(371) : ==> Subscript used on non-accessible variable.:
$aItems = _OL_ItemFind($oOutlook, $mFolder[1], $olMail, "[UnRead]=True", "Subject", "Maintenance Request", "EntryID,Subject", "", 0)
$aItems = _OL_ItemFind($oOutlook, $mFolder^ ERROR

I know that error occurs very rarely and at random. But like with most errors "in this case" I can use recursion to fix it. 

I just recall the function and it will and has fixed itself. But this "Subscript used on non-accessible variable" just ends the script.

Like I said before, is there a way to allow the script to keep running or call a function when this error occurs instead of just ending?   

Edited by nooneclose
problem solved to the best of our ability
Link to comment
Share on other sites

Do proper error checking before sending the array array to the function. Make sure that first it's an array, and second has the correct number of subscripts, then use the function with the array. Otherwise you're hiding an error that you won't know about and are just obfuscating improper error checking.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewManNH Something like this?

Func ScanInvaildEmails()

    Local $mFolder = _OL_FolderAccess($oOutlook, "Inbox")
    If IsArray($mFolder) Then
        Local $iRows = UBound($mFolder, $UBOUND_ROWS)
        If $iRows >= 2 Then
            ; It worked, do nothing
        Else
            Sleep(2000)
            ScanInvaildEmails()
        EndIf
    Else
        Sleep(2000)
        ScanInvaildEmails()
    EndIf
    
EndFunc

 

Edited by nooneclose
Spacing in code bugged me
Link to comment
Share on other sites

No, you're using recursion where you shouldn't be. You can put the whole thing inside a loop and never call the same function over and over. You have no way to leave this either so it will continue to recurse until it errors out, if left running long enough. Here's the proper way to go about it without the recursion.

Func ScanInvaildEmails()
     While 1
          Local $mFolder = _OL_FolderAccess($oOutlook, "Inbox")
          If IsArray($mFolder) Then
               Local $iRows = UBound($mFolder, $UBOUND_ROWS)
               If $iRows >= 2 Then
                    ; It worked, do nothing
               Else
                    Sleep(2000)
;~                ScanInvaildEmails()
               EndIf
          Else
               Sleep(2000)
;~           ScanInvaildEmails()
          EndIf
     WEnd
EndFunc   ;==>ScanInvaildEmails

You'll just need someway to exit this loop, the same as with your script, it's an endless loop with no way to exit it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I want it to exit if it is an array and if it does contain the correct number of subscripts. If it does not then I want it to try again because it should be.  

Func ScanInvaildEmails()
     While 1
          Local $mFolder = _OL_FolderAccess($oOutlook, "Inbox")
          If IsArray($mFolder) Then
               Local $iRows = UBound($mFolder, $UBOUND_ROWS)
               If $iRows >= 2 Then
                    ; It worked, Go to next function
                    ExitLoop 
               Else
                    Sleep(2000)
;~                ScanInvaildEmails()
               EndIf
          Else
               Sleep(2000)
;~           ScanInvaildEmails()
          EndIf
     WEnd
EndFunc   ;==>ScanInvaildEmails

This will try until its a proper Array with the correct subscripts and move on if I add Exitloop correct? 

Link to comment
Share on other sites

@BrewManNH Some of my emails were processed more than once. (the same email 2-3 times)

could this code be the cause? (is it somehow adding the same email multiple times to the array?)

; Find all vaild E-mails in the inbox
    While 1
        $aItems = _OL_ItemFind($oOutlook, $mFolder[1], $olMail, "[UnRead]=True", "Subject", "Maintenance Request", "EntryID,Subject", "", 0)
        If IsArray($aItems) Then
            ; It worked, Exit loop
            ConsoleWrite(@CRLF & "Array created" & " at line: " & @ScriptLineNumber & @CRLF)
            Exitloop
        Else
            Sleep(2000)
            ConsoleWrite("Error: No Array created" & " at line: " & @ScriptLineNumber & @CRLF)
        EndIf
    WEnd

 

Link to comment
Share on other sites

I assume you are trying to process new mails. In this case I suggest to use events. Means: A function in your script gets triggered as soon as a new mail arrives.
Please see the example scripts section in my signature.
Edit: Added an updated example fot the NewMailEx event today.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

@nooneclose

Execute sets the @error flag to non zero on failure.

so:

While 1
    $value = Execute("$mFolder[1]")
    If @error <> 0 Then
        Sleep(1000)
        ContinueLoop
    EndIf
    $aItems = _OL_ItemFind($oOutlook, $mFolder[1], $olMail, "[UnRead]=True", "Subject", "Maintenance Request", "EntryID,Subject", "", 0)
WEnd

I assume you'll use @water's solution, but it's always good with options

Link to comment
Share on other sites

Of course fixing the root cause of a potential error is much, really much better than trying to circumvent the consequences of an error.

The former is correct code, the latter is duck tape on a crack in a concrete wall.

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

12 hours ago, jchd said:

Of course fixing the root cause of a potential error is much, really much better than trying to circumvent the consequences of an error.

The former is correct code, the latter is duck tape on a crack in a concrete wall.

@jchd Which former code? Also, my script needs to runs 24/7. Hence the original reason why I created this thread. I needed a way to stop the script when stops on Autoit Errors. The only way I have found so far is by creating a disgusting amount of Error checking and if that does not work calling the function again (this eventually forces it to correct itself). I know that it is not a good practice to force code to work if it has problems. However, when you have to make it run 24/7 it's a bit different.      

Link to comment
Share on other sites

"The former" refers to "fixing the root cause".

A software having to run reliably 24/7 is an imperative reason to code it robustly in the first place and catch all the potential errors and handle them gracefully.

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

6 minutes ago, jchd said:

"The former" refers to "fixing the root cause".

A software having to run reliably 24/7 is an imperative reason to code it robustly in the first place and catch all the potential errors and handle them gracefully.

Yes, I would love to program it that way. However, since I do not know all of Autoit syntax and functions it is harder for me to do so. Also with people who just comment without giving any real help just keep making it difficult to refine not just my code but for anyone who seeks help. I greatly appreciate the people who take time out of their day to help me. It is annoying though when some people comment for comment's sake.  Whether it was to argue or to actually help. If one wants to help but is not clear and or does not contribute to solving the problem then what's the point? I understand where you are coming from but unless you can show me examples or teach me how to do better how can I improve? 

Link to comment
Share on other sites

34 minutes ago, nooneclose said:

how can I improve?

First by studying AutoIt syntax and functions you don't already know and master, at least the parts that relate to your goal (processing mails).
Then by following advices that were given by @BrewManNH and @water.

I was just stressing that the more important a software is in practice the more care must be devoted to its conception.

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

I guess you need to learn Autoit And I suggest reading the users manual for anything you “don’t know”

 

We’re not here for you to order up code from we are here to assist you with the code you’ve written

 

Giving you code isn’t helping you it’s holding you back

Edited by Earthshine

My resources are limited. You must ask the right questions

 

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

×
×
  • Create New...