Jump to content

Get excel workbook and sheet name


ahha
 Share

Recommended Posts

The .Application.Caption will display the objects top most window, for example from your previous post $oExcel1 would be Book2 (since it was opened last for $oExcel1), $oExcel2 would be _Excel1.xls - [Read Only] because that was opened last.  If you selected Book3 after the MsgBox, than the top most window for $oExcel2 (all 3 instances) would change to Book3 (although I only write the application.caption for the workbook that has the same name i.e. stringinstr)

Hope that makes sense.

Link to comment
Share on other sites

Thanks still testing. 

This is what I initially got when I select a cell in _Excel1.xls.  I was surprised to see no Active Window Title.

---------------------------

---------------------------
Active Window Title:

Active WorkBook: _Excel1.xls

Active Sheet: Tabelle2
---------------------------
OK   
---------------------------

 

When I select a cell in another workbook (not _Excel1.xls) it reverts back to the _Excel1.xls sheet not the one I selected.  Ugh.

*Edit* is this because of the 2 in  $iSearch = _ArraySearch($aWorkBooks, StringReplace($aWinList[$i][0], " - Excel", ""), 0, 0, 0, 1, 1, 2)

 

Here's the code:

#AutoIt3Wrapper_run_debug_mode=Y

#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>
#include <Debug.au3>

;Illustrate issue I'm having.  For a user seletecd range (possibly multiple $oWorkbooks open with multiple sheets), I need to determine the Excel application object of the selected cells and the sheet
;I need $oWorkbook, $WorkSheet, $Range
;look at: https://www.autoitscript.com/forum/topic/197347-get-excel-workbook-and-sheet-name/

;Simulate issue - in real world user may have opened Excel and I have no knowledge of the object
$oExcel1 = _Excel_Open()        ;open first instance
_Excel_BookNew($oExcel1)        ;workbook with 3 sheets
_Excel_BookNew($oExcel1)        ;another workbook in same instance with 3 sheets

$oExcel2 = _Excel_Open(Default, Default, Default, Default, True)    ;open second instance
_Excel_BookNew($oExcel2)        ;workbook with 3 sheets
_Excel_BookNew($oExcel2)        ;another workbook in same instance with 3 sheets
_Excel_BookOpen($oExcel2, "C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Extras\_Excel1.xls")    ;open an existing workbook that has been saved so we have an entry in Col2 for full pathname

;now here's what I know without a priori knowledge of the objects
;the workbook names are unigue - that is there will never be a Book1 in any but one of the instances or filename (i.e. single open instance of a particular file)

$aWorkBooks = _Excel_BookList()     ;get an array of all workbooks open
;Success: a two-dimensional zero based array with the following information:
;col 0 - Object of the workbook
;col 1 - Name of the workbook/file
;col 2 - Complete path to the workbook/file
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error listing workbooks.", "@error = '" & @error & "'" & @CRLF &"@extended = '" & @extended & "'")
_DebugArrayDisplay($aWorkBooks, "$aWorkBooks = _Excel_BookList()        ;get an array of all workbooks open.  Col 0 = Object, Col 1 = workbook name, Col 2 = full filename path")
;at this point we have the Object associated with the book name but no full filename path if not saved yet or previously

;now list the sheets for each Object Workbook
For $i = 0 to UBound($aWorkBooks, $UBOUND_ROWS) - 1 ;0 based
    $aWorkSheets = _Excel_SheetList($aWorkBooks[$i][0]) ;Col 0 = Workbook object
    ;Success: a two-dimensional zero based array with the following information:
   ; 0 - Name of the worksheet
   ; 1 - Object of the worksheet
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error listing Worksheets.", "@error = '" & @error & "'" & @CRLF & "@extended = '" & @extended & "'")
    _ArrayDisplay($aWorkSheets, "_Excel_SheetList() $aWorkSheets for $aWorkBooks[" & $i & "]" & " Col 0 = Worksheet Name, Col 1 = Worksheet object")
Next

MsgBox($MB_SYSTEMMODAL + $MB_OK, "Info", "Select a range in any Excel instance, any Workbook, and any sheet.  Then click OK.")

;new approach by Subz
ReDim $aWorkBooks[UBound($aWorkBooks)][5]
For $i = 0 To UBound($aWorkBooks) - 1
    $aWorkBooks[$i][3] = StringInStr($aWorkBooks[$i][0].Application.Caption, $aWorkBooks[$i][1]) > 0 ? $aWorkBooks[$i][0].Application.Caption : ""
    $aWorkBooks[$i][4] = $aWorkBooks[$i][0].ActiveSheet.Name
Next
Local $aWinList = WinList()
For $i = 1 To $aWinList[0][0]
    $iSearch = _ArraySearch($aWorkBooks, StringReplace($aWinList[$i][0], " - Excel", ""), 0, 0, 0, 1, 1, 2)
    If $iSearch > -1 Then
        MsgBox(4096, "", "Active Window Title: " & $aWorkBooks[$iSearch][3] & @CRLF & "Active WorkBook: " & $aWorkBooks[$iSearch][1] & @CRLF & "Active Sheet: " & $aWorkBooks[$iSearch][4])
        $oDefaultActiveExcelObject = _Excel_BookAttach($aWinList[$i][0], "Title")
        ExitLoop
    EndIf
Next


MsgBox($MB_SYSTEMMODAL, "Info", "Pause before exit.")

;force close all Excel w/o saving
_Excel_Close($oExcel1, False, True)
_Excel_Close($oExcel2, False, True)

Exit

 

 

Edited by ahha
Just tested with a sheet not _Excel1.xls
Link to comment
Share on other sites

Sorry forgot to update the second loop it should have been:

MsgBox($MB_SYSTEMMODAL + $MB_OK, "Info", "Select a range in any Excel instance, any Workbook, and any sheet.  Then click OK.")
ReDim $aWorkBooks[UBound($aWorkBooks)][5]
For $i = 0 To UBound($aWorkBooks) - 1
    $aWorkBooks[$i][3] = StringInStr($aWorkBooks[$i][0].Application.Caption, $aWorkBooks[$i][1]) > 0 ? $aWorkBooks[$i][0].Application.Caption : ""
    $aWorkBooks[$i][4] = $aWorkBooks[$i][0].ActiveSheet.Name
Next
Local $aWinList = WinList()
For $i = 1 To $aWinList[0][0]
    $iSearch = _ArraySearch($aWorkBooks, $aWinList[$i][0], 0, 0, 0, 1, 1, 3)
    If $iSearch > -1 Then
        MsgBox(4096, "", "Active Window Title: " & $aWorkBooks[$iSearch][3] & @CRLF & "Active WorkBook: " & $aWorkBooks[$iSearch][1] & @CRLF & "Active Sheet: " & $aWorkBooks[$iSearch][4])
        $oDefaultActiveExcelObject = _Excel_BookAttach($aWinList[$i][0], "Title")
        ExitLoop
    EndIf
Next

 

Link to comment
Share on other sites

Subz,

I now get neither working.  Not not even _Excel1.xls.  Could it be the 3 in $iSearch = _ArraySearch($aWorkBooks, $aWinList[$i][0], 0, 0, 0, 1, 1, 3) ?

Here's the full code now.

#AutoIt3Wrapper_run_debug_mode=Y

#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>
#include <Debug.au3>

;Illustrate issue I'm having.  For a user seletecd range (possibly multiple $oWorkbooks open with multiple sheets), I need to determine the Excel application object of the selected cells and the sheet
;I need $oWorkbook, $WorkSheet, $Range
;look at: https://www.autoitscript.com/forum/topic/197347-get-excel-workbook-and-sheet-name/

;Simulate issue - in real world user may have opened Excel and I have no knowledge of the object
$oExcel1 = _Excel_Open()        ;open first instance
_Excel_BookNew($oExcel1)        ;workbook with 3 sheets
_Excel_BookNew($oExcel1)        ;another workbook in same instance with 3 sheets

$oExcel2 = _Excel_Open(Default, Default, Default, Default, True)    ;open second instance
_Excel_BookNew($oExcel2)        ;workbook with 3 sheets
_Excel_BookNew($oExcel2)        ;another workbook in same instance with 3 sheets
_Excel_BookOpen($oExcel2, "C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Extras\_Excel1.xls")    ;open an existing workbook that has been saved so we have an entry in Col2 for full pathname

;now here's what I know without a priori knowledge of the objects
;the workbook names are unigue - that is there will never be a Book1 in any but one of the instances or filename (i.e. single open instance of a particular file)

$aWorkBooks = _Excel_BookList()     ;get an array of all workbooks open
;Success: a two-dimensional zero based array with the following information:
;col 0 - Object of the workbook
;col 1 - Name of the workbook/file
;col 2 - Complete path to the workbook/file
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error listing workbooks.", "@error = '" & @error & "'" & @CRLF &"@extended = '" & @extended & "'")
_DebugArrayDisplay($aWorkBooks, "$aWorkBooks = _Excel_BookList()        ;get an array of all workbooks open.  Col 0 = Object, Col 1 = workbook name, Col 2 = full filename path")
;at this point we have the Object associated with the book name but no full filename path if not saved yet or previously

;now list the sheets for each Object Workbook
For $i = 0 to UBound($aWorkBooks, $UBOUND_ROWS) - 1 ;0 based
    $aWorkSheets = _Excel_SheetList($aWorkBooks[$i][0]) ;Col 0 = Workbook object
    ;Success: a two-dimensional zero based array with the following information:
   ; 0 - Name of the worksheet
   ; 1 - Object of the worksheet
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error listing Worksheets.", "@error = '" & @error & "'" & @CRLF & "@extended = '" & @extended & "'")
    _ArrayDisplay($aWorkSheets, "_Excel_SheetList() $aWorkSheets for $aWorkBooks[" & $i & "]" & " Col 0 = Worksheet Name, Col 1 = Worksheet object")
Next

MsgBox($MB_SYSTEMMODAL + $MB_OK, "Info", "Select a range in any Excel instance, any Workbook, and any sheet.  Then click OK.")

;new approach by Subz - see his latest entry
ReDim $aWorkBooks[UBound($aWorkBooks)][5]
For $i = 0 To UBound($aWorkBooks) - 1
    $aWorkBooks[$i][3] = StringInStr($aWorkBooks[$i][0].Application.Caption, $aWorkBooks[$i][1]) > 0 ? $aWorkBooks[$i][0].Application.Caption : ""
    $aWorkBooks[$i][4] = $aWorkBooks[$i][0].ActiveSheet.Name
Next
Local $aWinList = WinList()
For $i = 1 To $aWinList[0][0]
    $iSearch = _ArraySearch($aWorkBooks, $aWinList[$i][0], 0, 0, 0, 1, 1, 3)
    If $iSearch > -1 Then
        MsgBox(4096, "", "Active Window Title: " & $aWorkBooks[$iSearch][3] & @CRLF & "Active WorkBook: " & $aWorkBooks[$iSearch][1] & @CRLF & "Active Sheet: " & $aWorkBooks[$iSearch][4])
        $oDefaultActiveExcelObject = _Excel_BookAttach($aWinList[$i][0], "Title")
        ExitLoop
    EndIf
Next

MsgBox($MB_SYSTEMMODAL, "Info", "Pause before exit.")

;force close all Excel w/o saving
_Excel_Close($oExcel1, False, True)
_Excel_Close($oExcel2, False, True)

Exit

 

Link to comment
Share on other sites

Here's what I'm getting it looks like what you call Active Window (Application.Caption) is blank.  I'm not sure why I'm using your code from above example.

It appears to be this line: 

$aWorkBooks[$i][3] = StringInStr($aWorkBooks[$i][0].Application.Caption, $aWorkBooks[$i][1]) > 0 ? $aWorkBooks[$i][0].Application.Caption : ""

image.png.3ba71db554a4c3b8bd909bbef041a642.png

Link to comment
Share on other sites

I've been working in parallel on another variation, and so far this seems to work.

#AutoIt3Wrapper_run_debug_mode=Y

#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>
#include <Debug.au3>

;Illustrate issue I'm having.  For a user seletecd range (possibly multiple $oWorkbooks open with multiple sheets), I need to determine the Excel application object of the selected cells and the sheet
;I need $oWorkbook, $WorkSheet, $Range
;look at: https://www.autoitscript.com/forum/topic/197347-get-excel-workbook-and-sheet-name/

;Simulate issue - in real world user may have opened Excel and I have no knowledge of the object
$oExcel1 = _Excel_Open()        ;open first instance
_Excel_BookNew($oExcel1)        ;workbook with 3 sheets
_Excel_BookNew($oExcel1)        ;another workbook in same instance with 3 sheets

$oExcel2 = _Excel_Open(Default, Default, Default, Default, True)    ;open second instance
_Excel_BookNew($oExcel2)        ;workbook with 3 sheets
_Excel_BookNew($oExcel2)        ;another workbook in same instance with 3 sheets
_Excel_BookOpen($oExcel2, "C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Extras\_Excel1.xls")    ;open an existing workbook that has been saved so we have an entry in Col2 for full pathname

;now here's what I know without a priori knowledge of the objects
;the workbook names are unigue - that is there will never be a Book1 in any but one of the instances or filename (i.e. single open instance of a particular file)

$aWorkBooks = _Excel_BookList()     ;get an array of all workbooks open
;Success: a two-dimensional zero based array with the following information:
;col 0 - Object of the workbook
;col 1 - Name of the workbook/file
;col 2 - Complete path to the workbook/file
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error listing workbooks.", "@error = '" & @error & "'" & @CRLF &"@extended = '" & @extended & "'")
_DebugArrayDisplay($aWorkBooks, "$aWorkBooks = _Excel_BookList()        ;get an array of all workbooks open.  Col 0 = Object, Col 1 = workbook name, Col 2 = full filename path")
;at this point we have the Object associated with the book name but no full filename path if not saved yet or previously

;now list the sheets for each Object Workbook
For $i = 0 to UBound($aWorkBooks, $UBOUND_ROWS) - 1 ;0 based
    $aWorkSheets = _Excel_SheetList($aWorkBooks[$i][0]) ;Col 0 = Workbook object
    ;Success: a two-dimensional zero based array with the following information:
   ; 0 - Name of the worksheet
   ; 1 - Object of the worksheet
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error listing Worksheets.", "@error = '" & @error & "'" & @CRLF & "@extended = '" & @extended & "'")
    _ArrayDisplay($aWorkSheets, "_Excel_SheetList() $aWorkSheets for $aWorkBooks[" & $i & "]" & " Col 0 = Worksheet Name, Col 1 = Worksheet object")
Next

MsgBox($MB_SYSTEMMODAL + $MB_OK, "Info", "Select a range in any Excel instance, any Workbook, and any sheet.  Then click OK.")

;from Subz https://www.autoitscript.com/forum/topic/197347-get-excel-workbook-and-sheet-name/
Local $aWinList = WinList()
_DebugArrayDisplay($aWinList, "$aWinList ALL TOP LEVEL WINDOWS")
;~ For $i = 1 To $aWinList[0][0]    ;$aArray[0][0] = Number of windows returned
;~     ;$iSearch = _ArraySearch($aWorkBooks, StringReplace($aWinList[$i][0], " - Excel", ""), 0, 0, 0, 1, 1, 1)     ;original - partial search
;~     If $iSearch > -1 Then
;~         MsgBox(4096, "", "Current WorkBook: " & $aWorkBooks[$iSearch][1])
;~         $oDefaultActiveExcelObject = _Excel_BookAttach($aWorkBooks[$iSearch][1])
;~      MsgBox(0, "Info", "The name of the active sheet is '" & $oDefaultActiveExcelObject.ActiveSheet.Name & "'")
;~      ExitLoop
;~     EndIf
;~ Next

;okay the above does a wrong way partial match when _Excel_BookList() returns
;Row|Col 0|Col 1|Col 2
;Row 4||_Excel1.xls|C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Extras
;and WinList returns
;Row|Col 0|Col 1
;Row 78|_Excel1.xls  [Read-Only]  [Compatibility Mode]|0x001318D4
;
;notice seaching "_Excel1.xls  [Read-Only]  [Compatibility Mode]" for a partial match with "_Excel1.xls"
;it should be looking for "_Excel1.xls" in "_Excel1.xls  [Read-Only]  [Compatibility Mode]"
;so writing our own
For $i = 1 To $aWinList[0][0]   ;$aArray[0][0] = Number of windows returned
    ;$aWinList[$i][0]   ;pull an entry from one row at a time in Col 0 which has the name
    For $j = 0 to UBound($aWorkBooks) - 1
        If StringInStr($aWinList[$i][0], $aWorkBooks[$j][1]) Then   ;found, otherwise go to next row
            ;because of the difference in what _Excel_Booklist() returns we need to keep what WinList returns for attaching
            ;then once we have the object we can just use that
            MsgBox(4096, "", "Current WorkBook: " & $aWinList[$i][0])
            ExitLoop 2  ;exit inner (j) and outer (i) loops
        EndIf
    Next
Next

;we have the current workbook so get other info from $aWorkBooks - recall
;$aWorkBooks[$j][0] ;has object
;$aWorkBooks[$j][1] ;has workbook name
;$aWorkBooks[$j][2] ;has full filename path

MsgBox(4096, "",    "Current WorkBook from $aWinList[$i][0]: " & $aWinList[$i][0] &@CRLF & _
                    "Current WorkBook from $aWorkBooks[$j][1]: " & $aWorkBooks[$j][1] &@CRLF)
;$aWorkBooks[$j][0] ;has object - use this rather than BookAttach since the names are different
MsgBox(0, "Info", "The name of the active sheet is '" & $aWorkBooks[$j][0].ActiveSheet.Name & "'")

MsgBox($MB_SYSTEMMODAL, "Info", "Pause before exit.")

;force close all Excel w/o saving
_Excel_Close($oExcel1, False, True)
_Excel_Close($oExcel2, False, True)

Exit

In either version _Excel_BookList() seems to be really slow.  Is it slow for you?

Edited by ahha
Link to comment
Share on other sites

Okay further testing done and there appears to be a fundamental problem using WinList - try the following program and you'll see that the Book in Excel is not listed in WinList - UGH :(  Or am I doing something wrong?

;test of WinList()
#AutoIt3Wrapper_run_debug_mode=Y

#include <Debug.au3>

MsgBox(0, "Info", "Open Excel manually then click OK.")

$aWinList = WinList()
MsgBox(0, "Info", "Try and find the Book in the array that follows.")
_DebugArrayDisplay($aWinList, "$aWinList ALL TOP LEVEL WINDOWS")

 

Link to comment
Share on other sites

Haven't read the whole thread but I would try the following.
This code is untested and might need a COM error handler to grab all COM errors so you can check @Error

#include <Excel.au3>
Global $oExcelTemp, $oExcelWindow
Global $oExcel = _Excel_Open()                      ; Connect to an already started Excel instance
Global $aWorkbooks = _Excel_BookList()              ; Get a list of workbooks for all Excel instances
For $i = 0 to UBound($aWorkbooks) - 1
   $oExcelTemp = $aWorkbooks[$i].Parent             ; Application object for the selected workbook
   $oExcelWindow = $oExcelTemp.ActiveWindow         ; Get active Window for the selected Excel instance
   If IsObj($oExcelWindow) Then                     ; Is there an active window for this Excel instance?
       $oExcelActiveCell = $oExcelWindow.ActiveCell ; Get the active cell of the window
       If @error = 0 Then ExitLoop                  ; If active cell found stop searching
   EndIf
Next

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

I get this error:

 ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
$oExcelTemp = $aWorkbooks[$i].Parent
$oExcelTemp = ^ ERROR

 

I added an ArrayDisplay and it shows

image.png.22e7544c9eafbd28d8fdadcfb512b506.png

 

Here's the slight mod

#include <Excel.au3>
#include <Debug.au3>
MsgBox(0, "Info", "Open Excel manually then click OK.")
Global $oExcelTemp, $oExcelWindow
Global $oExcel = _Excel_Open()                      ; Connect to an already started Excel instance
Global $aWorkbooks = _Excel_BookList()              ; Get a list of workbooks for all Excel instances
_DebugArrayDisplay($aWorkbooks, "$aWorkbooks = _Excel_BookList()")  ;let's look at it
For $i = 0 to UBound($aWorkbooks) - 1
   $oExcelTemp = $aWorkbooks[$i].Parent             ; Application object for the selected workbook
   $oExcelWindow = $oExcelTemp.ActiveWindow         ; Get active Window for the selected Excel instance
;~    MsgBox(0, "Info",     "$aWorkbooks[$i].Parent = " & $aWorkbooks[$i].Parent & @CRLF & _
;~                      "$oExcelTemp.ActiveWindow = " & $oExcelTemp.ActiveWindow)
   If IsObj($oExcelWindow) Then                     ; Is there an active window for this Excel instance?
       $oExcelActiveCell = $oExcelWindow.ActiveCell ; Get the active cell of the window
       If @error = 0 Then ExitLoop                  ; If active cell found stop searching
   EndIf
Next

 

Link to comment
Share on other sites

My bad. It's a 2D array so the script should look like:

#include <Excel.au3>
Global $oExcelTemp, $oExcelWindow
Global $oExcel = _Excel_Open()                      ; Connect to an already started Excel instance
Global $aWorkbooks = _Excel_BookList()              ; Get a list of workbooks for all Excel instances
For $i = 0 to UBound($aWorkbooks) - 1
   $oExcelTemp = $aWorkbooks[$i][0].Parent          ; Application object for the selected workbook <== MODIFIED
   $oExcelWindow = $oExcelTemp.ActiveWindow         ; Get active Window for the selected Excel instance
   If IsObj($oExcelWindow) Then                     ; Is there an active window for this Excel instance?
       $oExcelActiveCell = $oExcelWindow.ActiveCell ; Get the active cell of the window
       If @error = 0 Then ExitLoop                  ; If active cell found stop searching
   EndIf
Next

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

BTW: Your debugging statement  won't work as the Parent and ActiveWindow property return an object which can't be displayed using MsgBox.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

#include <Excel.au3>
Global $oExcelTemp, $oExcelWindow
Global $oExcel = _Excel_Open()                       ; Connect to an already started Excel instance
Global $aWorkbooks = _Excel_BookList()               ; Get a list of workbooks for all Excel instances
For $i = 0 to UBound($aWorkbooks) - 1
   $oExcelTemp = $aWorkbooks[$i][0].Parent           ; Application object for the selected workbook <== MODIFIED
   $oExcelWindow = $oExcelTemp.ActiveWindow          ; Get active Window for the selected Excel instance
   If IsObj($oExcelWindow) Then                      ; Is there an active window for this Excel instance?
       $oExcelActiveCell = $oExcelWindow.ActiveCell  ; Get the active cell of the window
       If @error = 0 Then 
           MsgBox(0, "Result", $oActiveCell.Address) ; Displays the selected cell or the top left cell of a range
           ExitLoop                                  ; If active cell found stop searching
       EndIf
   EndIf
Next
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

I'm trying.  I got this:

image.thumb.png.22d8c06be86b4bbb0ae37923cf484fa7.png

So I put $oActiveCell in the Global at top.

Now I get:

0013: 0-0:            MsgBox(0, "Result", $oActiveCell.Address) ; Displays the selected cell or the top left cell of a range
"C:\Program Files (x86)\AutoIt3\AH Code\water's approach v1a_DebugIt.au3" (50) : ==> Variable must be of type "Object".:
MsgBox(0, "Result", $oActiveCell.Address)
MsgBox(0, "Result", $oActiveCell^ ERROR

 

So far not my day ;) but I appreciate all the help.

Link to comment
Share on other sites

I will need to check this tomorrow when I get my hands on a Windows system. Linux with LibreOffice doesn't help much at the moment ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

A quick question @ahha. Can you tell me what you see in the Excel windows title bar of all your instances.  Since I am on Win7 and using Office 2003, it is a bit hard to figure it out...

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

×
×
  • Create New...