Jump to content

how to take control of the active window with an open Excel book?


Recommended Posts

Hello dear friends!

The stock application I work with generates reports in the form of Excel files that it opens on the screen. I would like to automatically receive some information from them, and then close these particular windows. But I can't figure out how to work with this window using Autoit tools? For example, when I try to close this particular window, all Excel windows are closed. Or just this process - but several at once.

Quote

while True
WinWaitActive("_CurrentDay_","", 0);жду окна
;Send ( "!{F4}")

WinClose("_CurrentDay_")

sleep(5000)

WEnd

 

I tried to iterate over all Excel processes and all open books to select the ones I needed and close them. But this procedure often gives an error.

Quote

$nCount = 0
While True
    Local $obj = ObjGet("", "Excel.Application", $nCount + 1)
    If @error Then ExitLoop
     
    
     $books=$obj.Application.Workbooks.Count
     MsgBox(4096, "",$books&" books")
     for $i=1 to $books
        $obj.DisplayAlerts = False

        MsgBox(4096, "",$obj.application.workbooks($i).name)
        $obj.application.workbooks($i).close

        If @error Then ExitLoop
          sleep(500)
     Next

     
    $nCount += 1
WEnd

What am I doing wrong?

I would be grateful for your advice.

Link to comment
Share on other sites

2 hours ago, Subz said:

Have a look at the _Excel functions in the help file, try _Excel_BookAttach then _Excel_BookClose to close the workbook.

Subz thanks for the answer! i tried to use your advice. unfortunately, I didn't succeed. Doesn't return values from the cell.

#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>
Opt("WinTitleMatchMode", 2)

while True
WinWaitActive("_CurrentDay","", 0)
$Active=WinGetHandle("_CurrentDay")
$oExcel = _Excel_open()
$oWorkbook = _Excel_Bookattach($Active)
$read=_Excel_Rangeread($oWorkbook,Default,"A1")
MsgBox(0,0,$read)

_Excel_BookClose($oWorkbook)
sleep(5000)
WEnd

Maybe I got it wrong somewhere?

Link to comment
Share on other sites

#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>
Opt("WinTitleMatchMode", 2)

while True
WinWaitActive("_CurrentDay","", 0)
$Active=WinGetTitle("_CurrentDay")
$oExcel = _Excel_open()
$oWorkbook = _Excel_Bookattach($Active,"Title")
$read=_Excel_Rangeread($oWorkbook,Default,"A1")
MsgBox(0,0,$read)

_Excel_BookClose($oWorkbook)
sleep(5000)
WEnd

fixed obvious bugs. that doesn't work either. Doesn't return a value or close the workbook. Does not give errors. only one book is open.

Link to comment
Share on other sites

I suggest to add some error checking:

#include <Excel.au3>
While True
    WinWaitActive("_CurrentDay", "", 0)
    $sWindowTitle = WinGetTitle("_CurrentDay")
    $oWorkbook = _Excel_BookAttach($sWindowTitle, "Title")
    If @error = 0 Then
        $sRangeRead = _Excel_RangeRead($oWorkbook, Default, "A1")
        MsgBox(0, 0, $sRangeRead)
        _Excel_BookClose($oWorkbook, False)
    EndIf
    Sleep(5000)
WEnd

 

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

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