Jump to content

_Excel_RangeRead Error


Recommended Posts

#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

Two things:
I see not _Excel_Open in your code.
Change the following lines

Local $sSelection = $oWorkBook.Application.Selection.Address(False, False)
Local $aResult = _Excel_RangeRead($oWorkbook, Default, $sSelection, 2)

to

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

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 by darkshark
Link to comment
Share on other sites

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

2

i'm using this test file

teste.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 by darkshark
Link to comment
Share on other sites

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

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

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