AutoHotkey Community

It is currently May 26th, 2012, 3:16 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: March 5th, 2009, 8:01 pm 
Offline

Joined: March 5th, 2009, 7:59 pm
Posts: 23
I'm trying to automate inputting data into Excel sheets in MS Office XP (under Vista Business).
I Run input.xls, then go using arrows strokes simulation to cell B2, copy name of output file to write to clipboard, then Run this file, activate input.xls, copy some data, activate output.xls... and the issue is that sometimes the window doesen't get activated. No matter whether I use WinActivate, %output_title_pattern% (where output_title_pattern is name of the file to activate) or Send !{Tab}
#WinActivateForce doesn't change anything
SetTitleMatchMode, 2 - I set it to 2 because sometimes title of an excel window is "filename", sometimes "microsoft excel - filename".
I also tried to add some Sleep, 100 but it didn't work
I mean, sometimes it works, but not always. I want it to change about 400 files in a row so I must be sure it will do everything correctly.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2009, 10:14 pm 
Check for "MS Office" at the Scripts & FUnctions section. Sean (and others) have provided something there which might fit.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2009, 10:26 pm 
Offline

Joined: March 5th, 2009, 7:59 pm
Posts: 23
Did you mean this post?
I don't think it could help me.
I prepared a test: http://daroc.ovh.org/_various/ahk/test.zip
Just extract it to a dir, run go.ahk and press Win+A
The script should read filename in in.xls, open it and copy data in in.xls to out.xls
However it copies only several lines of data because there's an issue when activating windows, ie. sometimes File menu is being activated or window is not activated at all.
Sorry for a bit messed up code (I deleted as much as I could now). Just jump to "Loop" and see what's there.
There are a lot of Sleeps but without it Excel behaves strange (selects not only one row but several rows etc...)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 5th, 2009, 11:01 pm 
Interesting that you need to ask. A search for Keyword: MS Office, Author: Sean, Section: Scripts & Functions delivers > 2 < threads.
Not that much to start further investigations, or? :wink:

http://www.autohotkey.com/forum/post-136876.html#136876
http://www.autohotkey.com/forum/post-139698.html#139698


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2009, 10:13 am 
Offline

Joined: March 5th, 2009, 7:59 pm
Posts: 23
Ok, so I got to know another method of getting data from a sheet - using Excel_GetCell() instead of simulating Ctrl+C.
I hope it will help me get rid of problems with activating excel windows (I don't know exactly why it would help me but I will try).
I have included COM.ahk and functions.ahk (where I pasted all Excel functions copied from the thread) to my script and tried to use Excel_GetCell() but despite Excel window is open and active, it returns "Couldn't find Excel instance".
What am I doing wrong?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2009, 11:13 am 
Quote:
What am I doing wrong?
You didn't post your code. So the answer to that question could be "switch on your computer", which I doubt would be of any help, right?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2009, 4:07 pm 
Offline

Joined: March 5th, 2009, 7:59 pm
Posts: 23
I pasted a link to a test program which shows what's wrong. It's not the real program but is very similar and works in the same way.

Here is another example http://daroc.ovh.org/_various/ahk/test2.zip
where I try to use Excel_GetCell()

Code:
#include COM.ahk
#include functions.ahk
#include CoHelper.ahk

; INFORMATION
; Especially look at the Loop below


; Options
input_filename=in.xls
;input_title_pattern=Microsoft Excel - %input_filename%
input_title_pattern=%input_filename%
winactivate_timeout=4

; General
#SingleInstance
#Persistent
;#WinActivateForce
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2

; GUI
Menu, Tray, Tip, TestCase

; Menu
Menu, Tray, NoStandard
Menu, Tray, Add, Windows Spy, RunWinSpy
Menu, Tray, Add, Edit
Menu, Tray, Add, Reload
Menu, Tray, Add, Exit
Menu, Tray, Default, Reload
Return
RunWinSpy:
Run, C:\Program Files\AutoHotkey\AU3_Spy.exe
return
Edit:
Edit
Return
Reload:
Reload
Return
Exit:
ExitApp
return

; Predefined functions

ToolTipRemove:
  SetTimer, ToolTipRemove2, 3000
  return
ToolTipRemove2:
  SetTimer, ToolTipRemove2, Off
  ToolTip
  return

; The Script

#a::
Sleep 500
FileGetSize, tmp, %input_filename%
if ErrorLevel
  MsgBox, in files does not exist
else {
  ifWinExist %input_title_pattern%
  {
    WinActivate
  }
  else {
    Run, %input_filename%
  }
  WinWaitActive, %input_title_pattern%, , %winactivate_timeout%
  if ErrorLevel
    MsgBox, error2
  else {
    Sleep 100
    Send {Esc}
    Send ^{Home}
    Sleep 500
    Send {Down 2}{Right}
    Sleep 50
    Send {F2}
    Sleep 50
    Send {End}
    Send +{Home}
    Sleep 50
    Send ^c
    Sleep 50
    Send {Esc}
    Sleep 50
    output_filename=%clipboard%.xls
    output_title_pattern=%output_filename%
    ifWinExist %output_title_pattern%
      WinActivate
    else {
      FileGetSize, tmp, %output_filename%
      if ErrorLevel {
        MsgBox, no file - %output_filename%
        ExitApp
      }
      else {
        ifWinExist %output_title_pattern%
        {
          WinActivate
          Sleep 50
        }
        else {
          Run, %output_filename%
        }
      }
    }
    WinWaitActive, %output_title_pattern%, , %winactivate_timeout%
    if ErrorLevel {
      MsgBox, couldn't open window
      ExitApp
    }
    else {
      Sleep 50
      Send ^{Home}
      Sleep 50
      WinActivate, %input_title_pattern%
      Send {Esc}
      Sleep 50
      Send {Down}{Home}
      Sleep 50
      Loop, 1 {
        A:=Excel_GetCell("A", 3)
        MsgBox % A
      }
    }
  }
}
;Progress, Off
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2009, 5:20 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
So much is wrong with that code

Firstly
cohelper is replaced by com dont use both
I am going to do this precisely once
I wont go into detail i dont have time
first activating windows with excel and COM is pointless activating isnt necesary

I think this covers the methods you need to do as you described i have not the time to offer more help on this
Code:
COM_CoInitialize()
OnExit,exits
if !oExcel := COM_GetActiveObject("Excel.Application")
{
   oExcel := COM_ActiveXObject("Excel.Application")
   COM_Invoke(oExcel, "Visible=", true)
;~    you could use either of these methods
;~    COM_Invoke(oExcel,"workbooks.open","Replace with path here")
;~    COM_Invoke(oExcel,"workbooks.add")
}
COM_Invoke(oExcel, "Visible=", true)
excelCount:=COM_Invoke(oExcel,"workbooks.count")
MsgBox   % " count of open workbooks " excelCount
If   excelCount!=2
{
   WB1:=COM_Invoke(oExcel,"workbooks.add")
;~    get the name of the worksheet
   WB1Name:=COM_Invoke(WB1,"Name")
   WB2:=COM_Invoke(oExcel,"workbooks.add")
;~    WB2:=COM_Invoke(oExcel,"workbooks.open","Replace with path here")
;~    get the name of the worksheet
   WB2Name:=COM_Invoke(WB2,"Name")
   COM_Release(WB1),WB1:=0
   COM_Release(WB2),WB2:=0
}
MsgBox % WB1Name
MsgBox % WB2Name
;~ you could also test if the required workbooks are open with something like this
If !WB:=COM_Invoke(oExcel,"workbooks.item",WB1Name) ;replace book 1 with the file name without extension
   ExitApp ; or you could use the previously described method to open the required file
;~ we didnt release excelCount because its a string not an object referenc object references need to be be released and set to nothing
COM_Release(WB),WB:=0
If !WB:=COM_Invoke(oExcel,"workbooks.item",WB2Name) ;replace book 1 with the file name without extension
   ExitApp ; or you could use the previously described method to open the required file
COM_Release(WB),WB:=0
;~ write a value to a cell
COM_Invoke(oExcel,"workbooks.item[" WB1Name "].worksheets.item[1].cells.item[2].item[1].value","first workbook b1")
;~ Read and save value to an ahk variable
readValue:=COM_Invoke(oExcel,"workbooks.item[" WB1Name "].worksheets.item[1].cells.item[2].item[1].value")
;~ Write that value to another workbook
COM_Invoke(oExcel,"workbooks.item[" WB2Name "].worksheets.item[1].cells.item[2].item[1].value",readValue)
;~ you can optionaly use the index of the workbook they will be in the order they were opened
COM_Invoke(oExcel,"workbooks.item[2].worksheets.item[1].cells.item[2].item[2].value","This is book 2 tho")
MsgBox click ok to close excel
exits:
COM_Error(0)
COM_Invoke(oExcel,"quit")
COM_CoUninitialize()
ExitApp

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 6th, 2009, 5:21 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
So much is wrong with that code

Firstly
cohelper is replaced by com dont use both
I am going to do this precisely once
I wont go into detail i dont have time
first activating windows with excel and COM is pointless activating isnt necesary

I think this covers the methods you need to do as you described i have not the time to offer more help on this
Code:
COM_CoInitialize()
OnExit,exits
if !oExcel := COM_GetActiveObject("Excel.Application")
{
   oExcel := COM_ActiveXObject("Excel.Application")
   COM_Invoke(oExcel, "Visible=", true)
;~    you could use either of these methods
;~    COM_Invoke(oExcel,"workbooks.open","Replace with path here")
;~    COM_Invoke(oExcel,"workbooks.add")
}
COM_Invoke(oExcel, "Visible=", true)
excelCount:=COM_Invoke(oExcel,"workbooks.count")
MsgBox   % " count of open workbooks " excelCount
If   excelCount!=2
{
   WB1:=COM_Invoke(oExcel,"workbooks.add")
;~    get the name of the worksheet
   WB1Name:=COM_Invoke(WB1,"Name")
   WB2:=COM_Invoke(oExcel,"workbooks.add")
;~    WB2:=COM_Invoke(oExcel,"workbooks.open","Replace with path here")
;~    get the name of the worksheet
   WB2Name:=COM_Invoke(WB2,"Name")
   COM_Release(WB1),WB1:=0
   COM_Release(WB2),WB2:=0
}
MsgBox % WB1Name
MsgBox % WB2Name
;~ you could also test if the required workbooks are open with something like this
If !WB:=COM_Invoke(oExcel,"workbooks.item",WB1Name) ;replace book 1 with the file name without extension
   ExitApp ; or you could use the previously described method to open the required file
;~ we didnt release excelCount because its a string not an object referenc object references need to be be released and set to nothing
COM_Release(WB),WB:=0
If !WB:=COM_Invoke(oExcel,"workbooks.item",WB2Name) ;replace book 1 with the file name without extension
   ExitApp ; or you could use the previously described method to open the required file
COM_Release(WB),WB:=0
;~ write a value to a cell
COM_Invoke(oExcel,"workbooks.item[" WB1Name "].worksheets.item[1].cells.item[2].item[1].value","first workbook b1")
;~ Read and save value to an ahk variable
readValue:=COM_Invoke(oExcel,"workbooks.item[" WB1Name "].worksheets.item[1].cells.item[2].item[1].value")
;~ Write that value to another workbook
COM_Invoke(oExcel,"workbooks.item[" WB2Name "].worksheets.item[1].cells.item[2].item[1].value",readValue)
;~ you can optionaly use the index of the workbook they will be in the order they were opened
COM_Invoke(oExcel,"workbooks.item[2].worksheets.item[1].cells.item[2].item[2].value","This is book 2 tho")
MsgBox click ok to close excel
exits:
COM_Error(0)
COM_Invoke(oExcel,"quit")
COM_CoUninitialize()
ExitApp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 7th, 2009, 1:48 pm 
Offline

Joined: March 5th, 2009, 7:59 pm
Posts: 23
Thank you very much for your help.
Editing worksheets via workbooks.item[number] method works great but if I try workbooks.item[" WB2Name "] it's ok until I try to use opened file, ie. I changed the script to open existing document instead of creating a new one:
Code:
   file:=A_ScriptDir . "\in" ; or \in.xls
   WB2:=COM_Invoke(oExcel,"workbooks.open",file)

The error occurs on
Code:
COM_Invoke(oExcel,"workbooks.item[" WB2Name "].worksheets.item[1].cells.item[2].item[1].value",readValue)

I also tried to change WB2Name from in.xls to in, but also this time there's no effect as well as when used d:\_fullpath_\in
(These are the only lines I changed in your example)

The error looks like:
Function Name: "item"
ERROR: wrong index (0x8002000B)
PROG:
DESC:
HELP: ,0
ERROR2: Unable to find group member (0x80020003)
Will Continue?

(I had to translate it to english so errors may be a bit different)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 7th, 2009, 5:10 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
Code:
 file:=A_ScriptDir . "\in" ; or \in.xls

should definately be
Code:
file:=A_ScriptDir . "\in.xls"

next i have to ask some questions
did the workbook open?
what appears in WB2Name variable before you call
Code:
COM_Invoke(oExcel,"workbooks.item[" WB2Name "].worksheets.item[1].cells.item[2].item[1].value",readValue)

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 7th, 2009, 5:19 pm 
Offline

Joined: March 5th, 2009, 7:59 pm
Posts: 23
tank wrote:
did the workbook open?
what appears in WB2Name variable before you call
Code:
COM_Invoke(oExcel,"workbooks.item[" WB2Name "].worksheets.item[1].cells.item[2].item[1].value",readValue)

in.xls
I tried to set wb2name to "in" but no effects (the example says it should be name without extension?)

It opens excel, shows number of workbooks (0), open in.xls, create Book1, display its' name in msgbox and then shows error.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 7th, 2009, 5:51 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
Quote:
display its' name in msgbox
but what was the exact text
were there periods or other special characters for instance if it shows in.xls then the problem is the period but this can be worked around but i need to know

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 7th, 2009, 6:24 pm 
Offline

Joined: March 5th, 2009, 7:59 pm
Posts: 23
msgbox '%wb2name%'
shows 'in.xls'


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 7th, 2009, 7:38 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
then that does explain the error
change
Code:
COM_Invoke(oExcel,"workbooks.item[" WB2Name "].worksheets.item[1].cells.item[2].item[1].value",readValue)
to
Code:
COM_Invoke(WB2,"worksheets.item[1].cells.item[2].item[1].value",readValue)
and dont forget to comment out the line where i release WB2
I forgot to mention this as an option but it isnt necesary to use the name if you open the workbook with the script or add it

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Google Feedfetcher, nyoe, patgenn123 and 17 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group