Jump to content

Ripping my hair out with _excel functions


Recommended Posts

We need an example script from you to show us what direction you were going.

.... We can't read minds and write anything that will work for you.

8)

Sure. I am starting here by trying to attach to the spreadsheet I want Book2.xls.

CODE
#include <Excel.au3>

#include <File.au3>

Func _ExcelAttach($s_string, $s_mode = "Title")

$s_mode = StringLower($s_mode)

Local $o_Result, $o_workbook, $o_workbooks

$o_Result = ObjGet("", "Excel.Application")

If @error Or Not IsObj($o_Result) Then

ConsoleWrite("Unable to attach to existing Excel.Application object." & @CR)

Return SetError(1, 0, 0)

EndIf

$o_workbooks = $o_Result.Application.Workbooks

If Not IsObj($o_workbooks) Or $o_workbooks.Count = 0 Then

ConsoleWrite("There were no open excel windows." & @CR)

Return SetError(1, 0, 0)

EndIf

;~ ConsoleWrite($o_workbooks.count & @CR)

For $o_workbook In $o_workbooks

Switch $s_mode

Case "filename"

;~ ConsoleWrite($o_workbook.Name & @CR)

If $o_workbook.Name = $s_string Then

$o_workbook.Activate

Return $o_workbook.Application

EndIf

Case "filepath"

;~ ConsoleWrite($o_workbook.FullName & @CR)

If $o_workbook.FullName = $s_string Then

$o_workbook.Activate

Return $o_workbook.Application

EndIf

Case "title"

;~ ConsoleWrite($o_workbook.Application.Caption & @CR)

If ($o_workbook.Application.Caption) = $s_string Then

$o_workbook.Activate

Return $o_workbook.Application

EndIf

Case Else

; Invalid Mode

ConsoleWrite("You have specified an invalid mode." & @CR)

Return SetError(1, 0, 0)

EndSwitch

Next

Return SetError(1, 0, 0)

EndFunc ;==>_ExcelAttach

$oExcel = _ExcelBookAttach("Microsoft Excel - Book2", "Title") ;with $s_mode = "Title" ==> Title of the Excel window

_ExcelWriteCell($oExcel, "If you can read this, then Success!", 1, 1) ;Write to the Cell

I have Book2 open when executing this macro.

Edited by m0raca
Link to comment
Share on other sites

Good that was some effort... I lied, here is an example of what you asked for..

I want to simply copy a value from excel in to the copy/paste buffer.

#include <Excel.au3>

Local $oExcel = _ExcelBookNew() ;Create new book, make it visible

For $i = 1 To 5 ;Loop
    _ExcelWriteCell($oExcel, $i, $i, 1) ;Write to the Cell
Next

For $i = 1 To 5 ;Loop
    $sCellValue = _ExcelReadCell($oExcel, $i, 1)
    MsgBox(0, "", "The Cell Value is: " & @CRLF & $sCellValue, 2)
Next

MsgBox(0, "Exiting", "Press OK to Save File and *Copy To Clipboard* then Exit")
ClipPut($sCellValue)
_ExcelBookSaveAs($oExcel, @TempDir & "\Temp.xls", "xls", 0, 1) ; Now we save it into the temp directory; overwrite existing file if necessary
_ExcelBookClose($oExcel) ; And finally we close out

; USE CTRL + V TO PASTE THE READ FROM CELL #5

8)

NEWHeader1.png

Link to comment
Share on other sites

Good that was some effort... I lied, here is an example of what you asked for..

I want to simply copy a value from excel in to the copy/paste buffer.

#include <Excel.au3>

Local $oExcel = _ExcelBookNew() ;Create new book, make it visible

For $i = 1 To 5 ;Loop
    _ExcelWriteCell($oExcel, $i, $i, 1) ;Write to the Cell
Next

For $i = 1 To 5 ;Loop
    $sCellValue = _ExcelReadCell($oExcel, $i, 1)
    MsgBox(0, "", "The Cell Value is: " & @CRLF & $sCellValue, 2)
Next

MsgBox(0, "Exiting", "Press OK to Save File and *Copy To Clipboard* then Exit")
ClipPut($sCellValue)
_ExcelBookSaveAs($oExcel, @TempDir & "\Temp.xls", "xls", 0, 1) ; Now we save it into the temp directory; overwrite existing file if necessary
_ExcelBookClose($oExcel) ; And finally we close out

; USE CTRL + V TO PASTE THE READ FROM CELL #5

8)

Hey thanks for your help... I am on the road now, somewhat. I can get what I need in and out of Excel and see the variables to increment where needed when I want to pull new info from excel on the next cell down. Now any suggested threads to figure out IE and frames? Not looking for code necessarily, just guidance.

Now that I have the data from excel I need to plug it in to an annoying web page.

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