_leo_ Posted July 3, 2019 Posted July 3, 2019 Hey guys I'm new to autoit, so this could be a simple question. I'm trying to read the value from the currently selected cell in Excel. I read on the forum and tired to find videos, but I couldn't quite get to it. This is what I have got so far: Local $oExcel_1 = _Excel_Open() Local $var = "C:\Users\Acer\xy" Local $oWorkbook = _Excel_BookOpen($oExcel_1,$var) Local $_read1 = _Excel_RangeRead($oWorkbook, Default.Application.ActiveCell.Address) Whatever I try, I either get an error or it only reads "0". Thank you very much for any helpful thoughts!
Nine Posted July 3, 2019 Posted July 3, 2019 $var seems suspicious. Should that be ended with .xls ? The _Excel_RangeRead is also bizarre. What are trying to do exactly ? “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Subz Posted July 3, 2019 Posted July 3, 2019 You need to use _Excel_BookAttach to read ActiveCell, if the workbook isn't opened you need to define the cell you want to read, for example: #include <Excel.au3> Local $bOpenWorkBook = False, $oExcel = _Excel_Open() Local $sFilePath = @ScriptDir & "\Test.xlsx" Local $oWorkbook $oWorkbook = _Excel_BookAttach($sFilePath) If @error Then $oWorkbook = _Excel_BookOpen($oExcel, $sFilePath) $bOpenWorkBook = True EndIf If $bOpenWorkBook Then $sCell = _Excel_RangeRead($oWorkbook, Default, "A20") ;~ Workbook was opened so read cell A2 Else $sCell = _Excel_RangeRead($oWorkbook, Default, $oExcel.ActiveCell.Address) ;~ Workbook was already open, read the active cell. EndIf MsgBox(4096, "Excel Cell Value", "Value = " & $sCell) _leo_ 1
_leo_ Posted July 4, 2019 Author Posted July 4, 2019 23 hours ago, Subz said: You need to use _Excel_BookAttach to read ActiveCell, if the workbook isn't opened you need to define the cell you want to read, for example: #include <Excel.au3> Local $bOpenWorkBook = False, $oExcel = _Excel_Open() Local $sFilePath = @ScriptDir & "\Test.xlsx" Local $oWorkbook $oWorkbook = _Excel_BookAttach($sFilePath) If @error Then $oWorkbook = _Excel_BookOpen($oExcel, $sFilePath) $bOpenWorkBook = True EndIf If $bOpenWorkBook Then $sCell = _Excel_RangeRead($oWorkbook, Default, "A20") ;~ Workbook was opened so read cell A2 Else $sCell = _Excel_RangeRead($oWorkbook, Default, $oExcel.ActiveCell.Address) ;~ Workbook was already open, read the active cell. EndIf MsgBox(4096, "Excel Cell Value", "Value = " & $sCell) Thank you very much for the example! Yeah, the BookAttach was missing. As I corrected my script, I came up with the question, if it was possible to read from a cell near the active cell. With an offset function for example: Local $_read2 = _Excel_RangeRead($oWorkbook, Default, $oExcel.ActiveCell.Address.offset(2,2)) ? Is this possible with Autoit?
Subz Posted July 4, 2019 Posted July 4, 2019 Yes, it's possible use: $sCell = _Excel_RangeRead($oWorkbook, Default, $oExcel.ActiveCell.Offset(2,2).Address) _leo_ 1
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