 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
WaywardMonk
Joined: 20 Jun 2005 Posts: 17
|
Posted: Fri Apr 27, 2007 11:43 am Post subject: SendMode Play vs SendMode Input question- see example script |
|
|
Where I work now, the word processor operators use MS Word and MS Word tables for lists that I want to see in MS Excel. They also insert lots of hard returns in the tables which makes it difficult to import the table into MS Excel properly. I tried importing the tables as HTML documents - the hard returns still cause data to spread over several cells rather than appearing one cell.
So I wrote this little script to copy the table data over to a spreadsheet for me.
My questions are:
1. The script works if I use SendMode Play, but not if I use SendMode Input - can somebody tell me why? (What happens is, with SendMode Input the script doesn't stop when it reaches "end" in the last Word table row - almost as if it didn't detect it.
2. Would it actually run any faster if I used SendMode Input rather than SendMode Play?
The script works well for me, if a little slowly - fortunately, MS Word tables get unwieldy if they get too big so the tables are not usually much bigger than about 200 rows. It will most likely only work correctly with simple tables - i.e. no nested tables, merged cells, etc.
To use the script, You have to insert "end" (without the quotes) into a blank cell at the end of the table to tell the script where it must stop. You will be asked how many columns there are in the table, and you will have to position the cursor in the first table cell.
| Code: |
; AutoHotkey Version: 1.0.46x
; Language: English
; Platform: Win9x/NT
; Author: WaywardMonk, April 2007
;
; Script Function:
; Read a Word table cell by cell into an array;
; Then open Excel and paste the contents of the array (i.e. each table cell) to an
; Excel spreadsheet cell by cell.
; This version of the script uses "Send" to paste the array contents.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Play
SetTitleMatchMode, 2 ; Window title can contain "Microsoft Excel" anywhere to be a match
clipboard := "" ; Make sure clipboard is empty - just in case
WinActivate, Microsoft Word ; Make sure Word is uppermost window
WinWaitActive, Microsoft Word
GetCols:
InputBox, ColCount, No of columns, How many columns in the table?`rNOTE: This cannot be 0 or blank ; Get the no of columns in the table
If ErrorLevel ; Exit the script if "Cancel" is clicked
Goto, Quit
If(ColCount = 0 or ColCount = "") ; No of columns cannot be 0 or blank
Goto, GetCols
ColCount := ColCount + 1 ; Add 1 to the no of columns else the array won't work correctly
WinActivate, Microsoft Word ; Make sure Word is uppermost window
WinWaitActive, Microsoft Word
MsgBox, 4160, ALERT, Make sure you have typed "end" (without the quotes)`rin a blank cell at the end of the table.`rThen place the cursor in the first table cell`r and click OK to continue ; This gives me time to ensure the cursor is in the right place
Num := 1 ; Initialise the array variable
WinActivate, Microsoft Word ; Make sure Word is uppermost window
WinWaitActive, Microsoft Word
Send !ace ; Tell Word to select the cell where the cursor is
Loop ; systematically read the table cell by cell into an array
{
WinActivate, Microsoft Word ; Make sure Word is uppermost window
WinWaitActive, Microsoft Word
clipboard := "" ; clear the clipboard - just in case
Send ^c{TAB} ; copy the selected cell contents and move to next Word cell
If(clipboard = "end" or clipboard = "END" or clipboard = "End")
{
Break ; Stop this loop if you've reached the end of the table data
}
Cell%Num% = %clipboard% ; Put the copied data into the array variable
Var := ++Num ; Increment the array variable
}
Col := 1 ; Set Col = 1 - this variable counts the columns so the script will know when to start a new Excel row
IfWinNotExist, Microsoft Excel ; Start Excel if it is not running
{
Run, excel
WinActivate, Microsoft Excel ; Make sure Excel is uppermost window
WinWaitActive, Microsoft Excel
Sleep, 1000
}
MsgBox, 4160, ALERT, Click OK to continue`rwhen your blank spreadsheet is open and ready ; This gives me time to ensure Ecel is open and the cursor is in the right place
Loop %Num% ; Start the loop equal to the no. of array variables
{
WinActivate, Microsoft Excel ; Make sure Excel is uppermost window
WinWaitActive, Microsoft Excel
If(Col = ColCount) ; check the no of columns in case it is necessary to start a new Excel row
{ ; if it is, then
Send {DOWN}^{LEFT} ; move to next row of spreadsheet
Col = 1 ; reset the Col to Col 1
}
clipboard := Cell%A_Index% ; Place array contents into clipboard
Send {F2}^v{TAB} ; Paste the variable contents
Var := ++Col ; Increment Col
}
FormatExcel: ; Format the Excel spreadsheet to look like a table
WinActivate, Microsoft Excel ; Make sure Excel is uppermost window
WinWaitActive, Microsoft Excel
Send ^{HOME}+^{RIGHT}^b+^{DOWN} ; Go to first Excel cell, select to last cell of top row, make bold then select to last cell of sheet
Send !oca!oeb!o!i{ENTER} ; Tell Excel to autofit column width of selection, and turn borders on
Send ^{HOME} ; go to first cell of spreadsheet
Quit: ; Exit the script
ExitApp
; ==============================================================
; Set up a pause hotkey- just in case I need to pause the script
PAUSE::Pause, Toggle
|
|
|
| Back to top |
|
 |
Micahs
Joined: 01 Dec 2006 Posts: 338
|
Posted: Sat Apr 28, 2007 3:46 am Post subject: |
|
|
I don't have Word to test it to see if it makes any difference, but have you tried ClipWait after "Send ^c{TAB}"? _________________
 |
|
| Back to top |
|
 |
WaywardMonk
Joined: 20 Jun 2005 Posts: 17
|
Posted: Sun Apr 29, 2007 1:39 pm Post subject: |
|
|
Thanks for the reminder
I tried it after you reminded me. This time the script detected "end" in the last table cell and moved to the next loop, but no data was copied over to Excel - all was blank, as if the array was empty.
I did speed the script up considerably by removing "WinActivate" and "WinWaitActive" from the loop, but the script still only works with SendMode Play, not SendMode Input. |
|
| Back to top |
|
 |
Micahs
Joined: 01 Dec 2006 Posts: 338
|
Posted: Tue May 01, 2007 1:46 am Post subject: |
|
|
I've done some stuff with 'send' and found that sometimes it can help to separate send commands with delays or whatnot. Maybe:
| Code: |
Send ^c
Clipwait, 2
Send {TAB}
|
or
| Code: |
Send ^c
Sleep,100
Send {TAB}
|
Hope this helps
[EDIT] Hmm, then again if it detects the 'end' it read the clipboard correctly. But this may be the case with some of the other sends, maybe. _________________
 |
|
| Back to top |
|
 |
BETLOG
Joined: 27 Nov 2006 Posts: 218 Location: Queensland, Australia
|
Posted: Tue May 01, 2007 6:36 am Post subject: |
|
|
doing a :
clipboard = %clipboard%
will strip out any formatting and other unexpected stuff. I am frequently surprised at instances where I need to use this.. especially with excel data. |
|
| Back to top |
|
 |
WaywardMonk
Joined: 20 Jun 2005 Posts: 17
|
Posted: Sun May 13, 2007 7:11 am Post subject: |
|
|
Thanks, Micahs and BETLOG for your inputs.
I'm afraid using clipboard =%clipboard% produced an AHK script error along the lines of "/" being an illegal character.
Using ClipWait made the script wait at every empty table cell. Clipwait, 2 was better BUT I still experienced the same problem trying to use Sendmode Input. <sigh>
I guess it's something in MS Excel that just works a little oddly.
On the plus side, I've managed to refine the script a bit and it now works a bit faster. Wish I understood the reason for the Sendmode Input problem, though. |
|
| Back to top |
|
 |
BETLOG
Joined: 27 Nov 2006 Posts: 218 Location: Queensland, Australia
|
Posted: Sun May 13, 2007 8:47 am Post subject: |
|
|
Not sure if this is helpful, but I have also had success with a similar recent problem by using piping. <, >, etc
fieldContents = Your problem Text`r`n
FileDelete,TextContainingHardReturnsThatAreAProblem.txt
FileAppend,%fieldContents%,TextContainingHardReturnsThatAreAProblem.txt
run curl.exe -F "somefield=<TextContainingHardReturnsThatAreAProblem.txt"
Obviously my context is different, and I'm not surehow this applies in the absence of a %comspec% command line, but you might find it useful anyway. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|