Jump to content
Sign in to follow this  
Terenz

Action with object has failed, how to avoid?

Recommended Posts

Hi guys

This is a little snip of the code:

Global $oShell = ObjCreate("Shell.Application")
If Not IsObj($oShell) Then Exit

Run("explorer /e, C:\")

While 1
    $hWnd = WinActive("[REGEXPCLASS:(Explore|Cabinet)WClass]")
    If $hWnd <> 0 Then
        For $oWindow In $oShell.Windows()
            If $oWindow.HWND() = $hWnd Then ExitLoop
        Next
        $oTest = $oWindow.Document.SelectedItems()
        If IsObj($oTest) Then
            For $oItem In $oTest
                ConsoleWrite($oItem.Name() & @CR)
            Next
        EndIf
    EndIf
    Sleep(100)
WEnd

Pratically it open a folder ( C: ) and wait for the click on a item, it return the filename ( i need to add a flag for avoid the multiple ConsoleWrite but for now i don't care )

The problem is if i close the folder, i have:

: ==> The requested action with this object has failed.:
$oTest = $oWindow.Document.SelectedItems()
$oTest = $oWindow.Document.SelectedItems()^ ERROR

And i really don't know how to avoid it. I don't want to close the script, i need it to work "globally" with any folder but if no folder has found...i have the COM error.

Solution?

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites

Usually this is being avoided by waiting for events to occur (menas: wait until the suers selects a file and then process this selection).

I don't know which events (if any) the Explorer COM provides.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Remove the MsgBox with the error information. So you simply get an empty function.

This function is being called when an COM error occurrs, does nothing and returns to execute the script after the line causing the COM error.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

$oWindow is not an object when there are no open windows. Thats why $oWindow.Document.SelectedItems() can't process

Solution:

Global $oShell = ObjCreate("Shell.Application")
If Not IsObj($oShell) Then Exit

Run("explorer /e, C:\")
While 1
    $hWnd = WinActive("[REGEXPCLASS:(Explore|Cabinet)WClass]")
    If $hWnd <> 0 Then
        For $oWindow In $oShell.Windows()
            If $oWindow.HWND() = $hWnd Then ExitLoop
        Next

        If IsObj($oWindow) And IsObj($oWindow.Document.SelectedItems()) Then
            For $oItem In $oWindow.Document.SelectedItems()
                ConsoleWrite($oItem.Name() & @CR)
            Next
        EndIf

    EndIf
    Sleep(100)
WEnd

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  

×
×
  • Create New...