darkshark

_Excel_RangeRead Error

9 posts in this topic

#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

 

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks a lot, water

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by darkshark

Share this post


Link to post
Share on other sites

What's the value of @error after _Excel_BookAttach?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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!

 

 

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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