Jump to content

_Excel_RangeRead return 0 when read text file


Recommended Posts

Hi,

I am very new to AutoIt, My goal is to copy a text content paste into excelsheet.

Below are simple program are suppose to:

1.) Open a text file

2.) Copy A2 content in the text file

3.) Open Excel sheet

4.) Paste the Text content to the Excel sheet D1

 

The problem occured when i use _Excel_RangeRead(), no matter what content in the text file it always return 0. 

 

#include <File.au3>
#include <Excel.au3>

Local $aField1[2] = [1, $xlGeneralFormat]
Local $aField2[2] = [2, $xlTextFormat]
Local $aField3[2] = [3, $xlGeneralFormat]
Local $aField4[2] = [4, $xlDMYFormat]
Local $aField5[2] = [5, $xlTextFormat]
Local $aFieldInfo[5] = [$aField1, $aField2, $aField3, $aField4, $aField5]

Local $Excel_1 = _Excel_Open()
Local $Workbook = "C:\Users\Z\Desktop\Autoit Testing\Text.txt"
_Excel_BookOpenText($Excel_1, $Workbook, Default, $xlDelimited, Default, True, "|", $aFieldInfo, ",", ".")

WinActivate($Workbook)

Local $read2 = _Excel_RangeRead($Workbook, Default, "A2")
MsgBox(0,"Read",$read2)

Local $Excel_2 = _Excel_Open()
Local $pWorkbook2 = "C:\Users\Z\Desktop\Autoit Testing\Book2.xls"
Local $Workbook2 = _Excel_BookOpen($Excel_2, $pWorkbook2)

WinActivate($Workbook2)

_Excel_RangeWrite($Workbook2, Default, $read2, "D1")

 

 

 

Text file content:

image.png.6f962ee4b230936efd5254f72abf64cf.png

 

May i know can _Excel_RangeRead() use in this way? or why this code doesnt not work?

 

Thank you!

 

Link to comment
Share on other sites

Please use this tool when you post code.

You were mixing string and object :

#include <Excel.au3>

Local $Excel_1 = _Excel_Open()
Local $sWorkbook = @ScriptDir & "\Test.txt"
Local $oWorkbook = _Excel_BookOpenText($Excel_1, $sWorkbook);, Default, $xlDelimited, Default, True, "|", $aFieldInfo, ",", ".")
ConsoleWrite (@error & @CRLF)
; WinActivate($Workbook)

Local $read2 = _Excel_RangeRead($oWorkbook, Default, "A2")
MsgBox(0,"Read",$read2)
Local $Excel_2 = _Excel_Open()
Local $pWorkbook2 = @ScriptDir & "\Test.xls"
Local $Workbook2 = _Excel_BookOpen($Excel_2, $pWorkbook2)

;WinActivate($Workbook2)

_Excel_RangeWrite($Workbook2, Default, $read2, "D1")

Edit : adding some error handling helps understand what the problems are

Edited by Nine
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...