Jump to content

Action with object has failed, how to avoid?


Terenz
 Share

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

 

Link to comment
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 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

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

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