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