Sign in to follow this  
Followers 0
Terenz

Action with object has failed, how to avoid?

7 posts in this topic

#1 ·  Posted (edited)

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



Look at ObjEvent in the help file for how to construct an error handler.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

I'm check it out


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 (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Registering the error give more information but nothing else, i'll try to search if someone has the same problem with that COM function used in a loop


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

 

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 (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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  
Followers 0