Jump to content

Recommended Posts

Posted

Hi

 

I am writing a script to get autoit to populate an excel workbook with functions.  I receive an error when it comes to the _Excel_RangeWrite function.  It tells me that the worksheet object is wrong or the sheet name is wrong and the com error code from Microsoft indicates that it is an incorrect function.  I've tried various iterations and can't get it to work.  Would you have any idea as to what I need to get it to work:

Code:

_Excel_RangeWrite($oWorkbook,"x", ClipGet(),$xc,TRUE)

-"x" is where it fails

Error: @error = 2 ; @extended = 1

 

@error:

https://www.autoitscript.com/autoit3/docs/libfunctions/_Excel_RangeWrite.htm

@extended:

https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

 

Examples attempted:

-I have tried to set the worksheet name into a variable and load it that way

 

#1

_Excel_RangeWrite($oWorkbook,$oWorkbook.Activatesheet, ClipGet(),$xc,TRUE)

#2

Local $vWorksheet = $oWorkbook.Sheets("Data Transfer")

_Excel_RangeWrite($oWorkbook,$oWorkbook.Activatesheet, ClipGet(),$xc,TRUE)

#3

Local $vWorksheet ="Data Transfer"

_Excel_RangeWrite($oWorkbook,$oWorkbook.Activatesheet, ClipGet(),$xc,TRUE)

 

  • Moderators
Posted

Moved to the appropriate forum.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted

There is a typo in your test it should be :

_Excel_RangeWrite($oWorkbook,$oWorkbook.Activesheet, ClipGet(),$xc,TRUE)

or (assuming sheet #1)

_Excel_RangeWrite($oWorkbook,1, ClipGet(),$xc,TRUE)

or (assuming name is right)

_Excel_RangeWrite($oWorkbook,"Data Transfer", ClipGet(),$xc,TRUE)

 

Posted

So I had tried all three versions after fixing the typo and I still receive the same error.  It looks like the Excel functions were changed recently and I think the worksheet reference does not work correctly?  Earlier this week, it worked fine - it wasn't until Thursday that it started to go down.

 

Below is a screenshot - typo corrected and sheet name is correct

 

 

 

 

image.png

Posted

Which version of Excel do you run?
Which version of AutoIt do you run?
How do you create the workbook (use _Excel_BookOpen, _Excel_BookNew, _Excel_BookAttach)?

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

 

Posted

Microsoft Office Home & Student 2019

AutoIT:  3.3.14.5

I am opening up an existing worksheet.  I take values in a text file and copy it over to an excel spreadsheet.  I had updated my windows office mid-week, that may be why it is being thrown off.

excel open(), then excel book open

below is the code that I am using.

 

Thanks for the help!

 

#include <AutoItConstants.au3>
#include <WinAPI.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>
#include <String.au3>

;;EXIT OPTION
HotKeySet("{ESC}", "Terminate")
Func Terminate()
    Exit
 EndFunc   ;==>Terminate

Run("NOTEPAD_FILE")

Local $var = "EXCEL_FILE_LOCATION"
Local $oExcel_1= _Excel_Open()
Local $oWorkbook=_Excel_BookOpen($oExcel_1,$var)

ClipPut("")

WinSetState("NOTEPAD FILE", "", @SW_MAXIMIZE)
WinActivate ("EXCEL FILE")
WinSetState("EXCEL FILE","",@SW_RESTORE)
WinSetState("EXCEL FILE", "", @SW_MAXIMIZE)

For $i = 1 to 27 Step 1

IF $i <15 Then
   $h1 = 30+ 109*($i-1)  ;50, 155, 210, 265,320, 375,430,485,540,595,650,705,760,815,870,925,980
   ;Horizontal spacing between hourly cells: 0    109.43    218.86    328.29    437.72    547.15    656.58    766.01    875.44    984.87    1094.3    1203.73    1313.16    1422.59

   $v1 = 182
   $v2 = 497
   $v3 = 474
   $h2 = $h1+45
   $h3 = $h1+55
Else
   $h1 = 50+ 105*($i-15)
   $v1 = 660
   $v2 = 775
   $v3 = 475
   $h2 = $h1+45
   $h3 = $h1+55
endif


  MouseClick($MOUSE_CLICK_RIGHT, $h1, $v1, 1,25) ;RIGHT cLICK
   MouseClick($MOUSE_CLICK_LEFT, $h2, $v2, 1,25) ; LEFT CLICK - COPY


   $letter = $i+64
   $xc = Chr($letter)&"5"

if $i = 27 Then
   $xc = "AA5"
endif

WinActivate ("EXCEL_FILE")


_Excel_RangeWrite($oWorkbook,"SHEET1", ClipGet(),$xc,TRUE)
;_Excel_RangeWrite($oWorkbook,$oWorkbook.Activesheet, ClipGet(),$xc,TRUE)
;_Excel_RangeWrite($oWorkbook,3, ClipGet(),$xc,TRUE)

If @error Then MsgBox(0, "Range Write Error", "@error = " & @error & ", @extended = " & @extended)

ClipPut("")

next

Posted (edited)

Please check @error and @extended after each call to an _Excel_* function.
Example:

Local $oExcel_1= _Excel_Open()
ConsoleWrite("_Excel_Open: @error=" & @error & ", @extended=" & @extended & @CRLF)

 

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

 

Posted

I thought it'd be easier to understand with an notepad file.  I can't figure why it wouldn't work, I guess it's something deeper.  I am actually trying to generate a excel report from watch lists in TC2000 - a stock software.  I open up the TC2000, copy symbols from a watch list and paste them into excel.  Except the past function into excel stopped working.  I was going to then run an excel macro to do some data manipulation.  So maybe because it is an xlsm sheet, that's why? Except that it worked earlier in the week and stopped working after I updated to 2019?

 

BELOW IS THE ACTUAL CODE.  I also attached a screenshot to show you the TC2000 screen I am clicking from.  I wish there was a TC2000 API, for now Autotit will work.

 


#include <AutoItConstants.au3>
#include <WinAPI.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>
#include <String.au3>

;EXIT OPTION
HotKeySet("{ESC}", "Terminate")
Func Terminate()
    Exit
 EndFunc   ;==>Terminate


;OPEN PROGRAMS (TC2000,EXCEL)
Run("TC2000 EXE LOCATION")

 

Local $var = "EXCEL FILE LOCATION"
Local $oExcel_1= _Excel_Open()

If @error Then MsgBox(0, "Excel Open", "@error = " & @error & ", @extended = " & @extended & @CRLF)


 Local $oWorkbook=_Excel_BookOpen($oExcel_1,$var)

If @error Then MsgBox(0, "Excel book bopen", "@error = " & @error & ", @extended = " & @extended & @CRLF)

 

ClipPut("")

WinSetState("EXCEL FILE", "", @SW_MAXIMIZE)
WinActivate ("TC2000")
WinSetState("TC2000","",@SW_RESTORE)
WinSetState("TC2000", "", @SW_MAXIMIZE)



MouseClick($MOUSE_CLICK_LEFT, 291, 815, 1,5)

 

;$oWorkbook.Sheets("EXCEL SHEET").Activate

For $i = 1 to 27 Step 1

   WinActivate ( "TC2000")


IF $i <15 Then
   $h1 = 30+ 109*($i-1)  ;50, 155, 210, 265,320, 375,430,485,540,595,650,705,760,815,870,925,980
   ;Horizontal spacing between hourly cells: 0    109.43    218.86    328.29    437.72    547.15    656.58    766.01    875.44    984.87    1094.3    1203.73    1313.16    1422.59

   $v1 = 182
   $v2 = 497
   $v3 = 474
   $h2 = $h1+45
   $h3 = $h1+55
Else
   $h1 = 50+ 105*($i-15)
   $v1 = 660
   $v2 = 775
   $v3 = 475
   $h2 = $h1+45
   $h3 = $h1+55
endif

   MouseClick($MOUSE_CLICK_RIGHT, $h1, $v1, 1,25) ;MOVE TO TIME SYMBOL
   MouseClick($MOUSE_CLICK_LEFT, $h2, $v2, 1,25) ; COPY ALL SYMBOLS
   MouseClick($MOUSE_CLICK_LEFT, $h3, $v3, 1,25) ; COPY TO CLIPBOARD

   $letter = $i+64
   $xc = Chr($letter)&"5"

if $i = 27 Then
   $xc = "AA5"
endif

WinActivate ("EXCEL SHEET")
MouseClick($MOUSE_CLICK_LEFT, 257, 786, 1,5)

;_Excel_RangeWrite($oWorkbook,"EXCEL SHEET", ClipGet(),$xc,TRUE)
_Excel_RangeWrite($oWorkbook,"EXCEL SHEET", ClipGet(),$xc,TRUE)
;_Excel_RangeWrite($oWorkbook,$oWorkbook.Activesheet, ClipGet(),$xc,TRUE)
;_Excel_RangeWrite($oWorkbook,1, ClipGet(),$xc,TRUE)

If @error Then MsgBox(0, "Range Write Error", "@error = " & @error & ", @extended = " & @extended)

 

ClipPut("")

next

 

image.thumb.png.067bb3e864608a4623e47e5f4d5fb22b.png

Posted

I added in the ConsoleWrite code once I figured out what it does.  Below is the output, it errors on the _Excel_Open() but still works and then throws errors on the _Excel_rangewrite.

 

_Excel_Open: @error=0, @extended=1
_Excel_bookOpen: @error=0, @extended=0
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1
_Excel_rangewrite: @error=2, @extended=1

 

 

Why is this not just simple copy/paste?

Posted

Could you please replace _Excel_BookOpen with _Excel_BookNew?
If it works then we know that the existing workbook causes the problem.

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

 

Posted

The existing workbook caused the problem.  When I replaced _Excel_BookOpen with _Excel_BookNew, it fixed it.  I tried to create a second version by save as and it did not work.  I rebuilt the workbook and voilà, it works!

 

Thanks everyone for your help!

Posted

:)

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

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...