darkshark Posted August 26, 2015 Share Posted August 26, 2015 #include <Array.au3> #include <Excel.au3> Local $aWins = WinList() For $i = 1 To $aWins[0][0] If StringInStr($aWins[$i][0], " - Microsoft Excel") Then ExitLoop Next Local $oWorkBook = _Excel_BookAttach($aWins[$i][0], "Title") Local $sSelection = $oWorkBook.Application.Selection.Address(False, False) Local $aResult = _Excel_RangeRead($oWorkbook, Default, $sSelection, 2) MsgBox(0,'',@error) _ArrayDisplay($aResult)hello, I need a little help, I need to read the cells that are selected in an excel file that is already open.When I enter the code above, I get an error number 2, referring to the worksheet, however, there is no problem ... until the part to get the address of the cell the code works normally, and using the sample code _Excel_RangeRead(), the sample code also works perfectly ... I've tried using $oWorkbook.ActiveSheet, but keeps giving error.anyone help me, please? thanks a lot Link to comment Share on other sites More sharing options...
water Posted August 26, 2015 Share Posted August 26, 2015 Two things:I see not _Excel_Open in your code.Change the following linesLocal $sSelection = $oWorkBook.Application.Selection.Address(False, False) Local $aResult = _Excel_RangeRead($oWorkbook, Default, $sSelection, 2)toLocal $aResult = _Excel_RangeRead($oWorkbook, Default, $oExcel.Selection, 2)Where $oExcel is the application object returned by _Excel_Open. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
darkshark Posted August 27, 2015 Author Share Posted August 27, 2015 Thanks a lot, water Link to comment Share on other sites More sharing options...
darkshark Posted August 27, 2015 Author Share Posted August 27, 2015 (edited) The problem persists =/ #include <Array.au3> #include <Excel.au3> Local $aWins = WinList() For $i = 1 To $aWins[0][0] If StringInStr($aWins[$i][0], " - Microsoft Excel") Then ExitLoop Next Local $oExcel = _Excel_Open() Local $oWorkBook = _Excel_BookAttach($aWins[$i][0], "Title") $oExcel.Selection.Columns.AutoFit Local $aResult = _Excel_RangeRead($oWorkbook, Default, $oExcel.Selection, 2) MsgBox(0,'',@error) _ArrayDisplay($aResult) Edited August 27, 2015 by darkshark Link to comment Share on other sites More sharing options...
water Posted August 27, 2015 Share Posted August 27, 2015 What's the value of @error after _Excel_BookAttach? My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
darkshark Posted August 27, 2015 Author Share Posted August 27, 2015 (edited) 2i'm using this test fileteste.xlsx EDIT:Sorry, in the BookAttack i'm not giving error, only in RangeRead EDIT2:updated part for obtaining a window title#include <Array.au3> #include <Excel.au3> Local $sTitle = WinGetTitle("[CLASS:XLMAIN]") Local $oExcel = _Excel_Open() Local $oWorkBook = _Excel_BookAttach($sTitle, "Title") MsgBox(0,'',@error) $oExcel.Selection.Columns.AutoFit Local $aResult = _Excel_RangeRead($oWorkbook, Default, $oExcel.Selection, 2) MsgBox(0,'',@error) _ArrayDisplay($aResult) Edited August 27, 2015 by darkshark Link to comment Share on other sites More sharing options...
water Posted August 27, 2015 Share Posted August 27, 2015 This works quite well for me. Notice that the Window title in Excel has "Microsoft Excel" at the end whereas it is at the beginning when returned from WinList.#include <Excel.au3> Opt("WinTitleMatchMode", -2) Global $aWins = WinList("Microsoft Excel - ") If $aWins[0][0] = 0 Then Exit MsgBox(0, "Error", "No open Excel Workbooks found!") Global $oExcel = _Excel_Open() Global $oWorkBook = _Excel_BookAttach($aWins[1][0], "Title") ; $oExcel.Selection.Columns.AutoFit Global $vResult = _Excel_RangeRead($oWorkBook, Default, $oExcel.Selection, 2) If IsArray($vResult) Then _ArrayDisplay($vResult) Else MsgBox(0, '', $vResult) EndIf My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
darkshark Posted August 27, 2015 Author Share Posted August 27, 2015 with your code, the problem still persists....I did some tests here and managed to solve the problem.I changed the line:Global $vResult = _Excel_RangeRead($oWorkBook, Default, $oExcel.Selection, 2)per:Global $vResult = _Excel_RangeRead($oWorkBook, $oExcel.ActiveSheet.Name, $oExcel.Selection, 2)and it worked.I'm using autoit version v3.3.14.1, Microsoft Office 2013 and Windows 10!Thank you so much for your help, water.you helped me a lot.Thanks again! Link to comment Share on other sites More sharing options...
water Posted August 27, 2015 Share Posted August 27, 2015 I don't understand why it doesn't work. Maybe MS decided to change Excel 2013 a bit.But I'm glad it is working now My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now