Jump to content


Photo

writing to excel or csv file?


  • Please log in to reply
7 replies to this topic

#1 Surreall

Surreall
  • Members
  • 31 posts

Posted 18 July 2012 - 03:31 PM

Hi all,

I have really managed to confuse myself.

I have two simple loops one reads values and inputs into an active window


With this code below i would like to input each line from the text file into either excel in column A, or into a csv file where i can also input values from another loop.

Loop, read, %A_ScriptDir%\itemstosearch.txt
{	
	
        ;"Say the first line in the text file is "Holy Moly" and the second one is "Jeremy Crackers""
        ;"Here i would like to put Holy Moly into cell 2 in column A in excel, and put jeremy crackers in cell 3 in Column A in the same ;spreadsheet"	

	Loop, Parse, A_LoopReadLine
		{
		x = %A_LoopField%
		if (x = "")
			{
			Send {Space}
			}
		Else
			{												
			Send {%x%}
			}
		}

}


The second loop returns "output" variable on each iteration which i would like to put into column b

I suppose i need to create an excel file in the script folder first, which i could name simply "results"

i read up about excel and csv files, but all i managed to was confuse myself

Any help would be greatly appreciated

Regards

Surreall

#2 FischGeek

FischGeek
  • Members
  • 991 posts

Posted 18 July 2012 - 04:13 PM

AHK Basic
Since CSV is exactly what it sounds like (comma separated values) just create your list first and append it all at once.
row1 := "a1,b1,c1"
row2 := "a2,b2,c2"

FileAppend, %row1%`r%row2%, %A_Desktop%\test.csv
Run, %A_Desktop%\test.csv
AHK_L
Since COM is built into _L, you can do something like this: <!-- m -->http://www.autohotke... ... 63#p401063<!-- m -->

#3 Guests

  • Guests

Posted 18 July 2012 - 05:10 PM

AHK Basic
Since CSV is exactly what it sounds like (comma separated values) just create your list first and append it all at once.

row1 := "a1,b1,c1"
row2 := "a2,b2,c2"

FileAppend, %row1%`r%row2%, %A_Desktop%\test.csv
Run, %A_Desktop%\test.csv
AHK_L
Since COM is built into _L, you can do something like this: <!-- m -->http://www.autohotke... ... 63#p401063<!-- m -->


That link is brilliant, ty, i will have a play tonight

Greatly appreciated

Regards

Surreall

#4 Guests

  • Guests

Posted 18 July 2012 - 05:25 PM

KK back to confused lol

I looked at that link and tried the without opening option

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Recommended for catching common errors.

`::

SetworkingDir %A_ScriptDir%

FilePath := "%A_ScriptDir%\book1.xls"                     ; example path
oWorkbook := ComObjGet(FilePath)                ; access Workbook object
MsgBox % oWorkbook.Sheets(1).Range("A1").Value  ; get value from A1 cell in first sheet

I created the excel worksheet in the same directory and called it book1.xls

I just wanted to start testing....so i ran the above and got the following error message

Error: 0x800401EA - Moniker cannot open file
on the line "oWorkbook := ComObjGet(FilePath) "

I am probably being thick and missing something obvious out

Rgds

Surreall

#5 Surreall

Surreall
  • Members
  • 31 posts

Posted 19 July 2012 - 08:38 AM

To get my script immediately above to work, do i have to download a new library for it?

Regards

Surreall

#6 Surreall

Surreall
  • Members
  • 31 posts

Posted 19 July 2012 - 09:53 AM

I am getting somewhere now, one last question ...maybe :)

with this code

oExcel.Range("A1").Value := 3

Is it possible to make the A1 dynamic, ie have "A%B%" for example (this doesnt actually work), where B is a variable?

Regards

Surreall

#7 Surreall

Surreall
  • Members
  • 31 posts

Posted 19 July 2012 - 10:19 AM

Solved :)

Instead of range("A1")

i used cells(1,1), as you can put a variable in replace of either 1

Regards

Surreall

#8 Guests

  • Guests

Posted 19 July 2012 - 10:25 AM

probably something like so
b=1

oExcel.Range("A" b)