Jump to content

Working with a ShellExecute failure


Recommended Posts

Trying to verify that Excel can open a previously created file

Here is the code that I wrote, and it works exactly the way I wanted when it passes

$OpenExelDoc = ShellExecute (@MyDocumentsDir & "\compatibility.xls")

If $openExelDoc = 0 Then

MsgBox(0, "Error", "Excel was not able to open the file")

FileWriteLine($file, "FAIL - Excel was not able to open the Previously Saved spreadsheet")

elseif $OpenExelDoc = 1 Then

FileWriteLine($file, "PASS - Excel Open Previously Saved Document and Verified it was in the .xls format")

sleep(1500) ;pause for 1 and 1/2 second

Send("!{F4}") ;Sends the ALT+F4 function to close the window

Sleep(1500) ;pause for 1 and 1/2 second

FileDelete(@MyDocumentsDir & "\compatibility.xls") ;tells the script to delete the file it just created from the myDocuments folder

Endif

Now, here is the issue. If it passes, the code is fine. When I intentionally break it to simulate a problem, an error message pops up before it actually "Fails" and returns the failure. Until I click the "Windows cannot find 'C:\xxxxxxxx" error message, the script will not continue.

I thought I could get around it with a winexists prior to the ShellExecute command, but it doesn't seem to be picking up that window at all.

Link to comment
Share on other sites

Try using FileExists to see if it's there instead of trying to open it with ShellExecute, if it's there then you can open it, if it's not, you won't get any pop up.

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 do have a FileExists , and that is working perfectly as well. The test criteria I am trying to simulate is as following...

Open Excel (no issue here)

Enter data into a spreadsheet (again, workes fine)

Print the spreadsheet (fine)

Save the spreasheet and close (no problemo)

Verify the file exists, and is in the correct directory (works perfect)

Verify the file can be opened from Excel without error (ah... busted... so close but so far)

Edited by jerseyzuks
Link to comment
Share on other sites

When it's failing, why is it failing? Because you've moved/renamed the file?

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

Why don't you use the Excel UDF that comes with AutoIt?

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

When it's failing, why is it failing? Because you've moved/renamed the file?

To simulate a failure, I rename the file, and comment out the FileExists line. It doesn't matter what the error message is, if there is an error, it is a failure.

Once this code is hammered out, this will be used as a template for other applications (word, powerpoint, in house apps, etc)

Basically we need to make sure all of our baseline apps still work after we make any changes to policies, software upgrades, hardware changes, etc.

Link to comment
Share on other sites

Honestly, testing it that way is stupid. You're deliberately breaking the script and expecting it to work? The FileExists was there to check to see if the file exists, then you try to open a file that doesn't exist? Can't see how that's any way to idiot proof a script.

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

Honestly, testing it that way is stupid. You're deliberately breaking the script and expecting it to work? The FileExists was there to check to see if the file exists, then you try to open a file that doesn't exist? Can't see how that's any way to idiot proof a script.

I am not "deliberately breaking the script".

I am deliberately generating an error in Excel, to see if I can capture that as a failure. If Excel fails to open a file for any reason, it needs to be noted.

If you have a better suggestion as to how to recreate a failure, I am all ears. That was just the first one that popped into my head.

Link to comment
Share on other sites

But FileExists will tell you why any application cannot open a file if the file does not exist.

Maybe trying to open the non-existing file with an Excel UDF (or a translated VBA function) function will just return an error code instead of an error message window.

Link to comment
Share on other sites

The Excel UDF uses the COM interface and if a COM error handler is set up returns details error information.

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

You are deliberately breaking how the script is working by removing the error checking and then creating an error and trying to find a way to compensate for an error you shouldn't have in the first place. If the file doesn't exist, which you checked for with FileExists, don't try and open it, seems simple enough to me. If you're going to ignore the fact that the file doesn't exist and still try and open it, then you are only making more work for yourself to try and fix later.

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

OK, lets take a couple of steps back... Forget we are talking about Excel specifically, and forget what is causing the error message for a moment...

I have a file I want to open with the ShellExecute command, and then if it succeeds, follow one if/then statement, and if it fails follow another

When it succeeds, it works fine

When it "fails", it does't report a fail until I click the "OK" on the message box (so the script won't continue)

What would be the best way to handle this?

Link to comment
Share on other sites

FileExists, see above.

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

FileExists, see above.

Yes, I fully understand that FileExists will show me if the file exists.

What I am trying to do is see if there are any errors when it attemps to open the file (permissions, corrupt file, anything).

And this code is going to be used for numerous applications, not just excel, so I need it to be as portable as possible

Link to comment
Share on other sites

Extensive error checking is better than dealing with errors. ShellExecute is probably not the best way to open files unless you know you're able to open them and they aren't going to cause an error condition. If you get an error, you deal with it by looking for the error window with WinExists or WinWait with a time out and clicking past it. If you are specifically looking to open files and you know which file types you're looking to open, you can always open them directly using Run most of the time.

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

Extensive error checking is better than dealing with errors. ShellExecute is probably not the best way to open files unless you know you're able to open them and they aren't going to cause an error condition. If you get an error, you deal with it by looking for the error window with WinExists or WinWait with a time out and clicking past it. If you are specifically looking to open files and you know which file types you're looking to open, you can always open them directly using Run most of the time.

So why bother including error handling on ShellExecute?

99.9% of the time, we expect all of these files to open normally. They are .txt files, .xls files, .pdf files, .doc files, etc.

If, for some reason they don't open normally, we have someone investigate manually. I just need to figure out a "catch" for the rare instance something doesn't open the way it is supposed to

Link to comment
Share on other sites

You could use FileOpen() after FileExists() to test for access rights/attributes. If it returns >= 0 then FileClose() and ShellExecute().

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

So why bother including error handling on ShellExecute?

If you don't mind the windows popup if it fails for whatever reason... and you don't mind the user having to deal with that error as opposed to the program dealing with it... then who cares? ;)

ShellExecute away without error handling.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

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