Sorting Word Table data selectively

Ok so what I am looking to do is sort just a subset of data within a MS WORD table. An example being column 8 of the table contains the values 1, 2, or 3. I would like to sort the table, say by column 3, for just the items with column 8 containing the value 2. Is there a way of doing this. Also, to print out that same subset/selection of the table.


Model: iMac (late 2006)
AppleScript: 2.1.1
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

What version of MS Word?

Sorry forgot to include that…MS Word 2004

The easiest way to do that would be to copy the table, paste it into a new Excel workbook, use Excel’s filtering to hide the rows you want stationary, sort, un-filter and copy paste it back into Word.

I’ve been stuck in getting AdvancedFilter to work via AppleScript, but with 2004, you have VBA avaliable. (Much faster)

So there is no way in Word 2004 to accomplish this? I would think that there would since it has the table functionality. I couldn’t find anything under the dictionary though. If I need to move the data in and out of Excel (2004) that wouldn’t be too overly hard, but I was hoping for a within Word (2004) solution.

Well I did it…I found a way to selectively sort a Word (2004) table solely with Applescript. Took a little head banging but this is the result. If there are any more efficient commands please let me know, always looking to learn better ways. Pretty much explains itself. Here it is.

property wdPath : ((path to desktop) as text) & "yourfilenamehere.doc"
set dialogResult to display dialog "Which listing to sort (1,2,3...)?" buttons {"Cancel", "All", "OK"} default button "OK" cancel button "Cancel" default answer "1"
if button returned of dialogResult is "All" then
	set selectFilter to "All"
	set selectFilter to text returned of dialogResult
end if

--selectFilter is column to query out for sort
--it is expected that all items with the same selectFilter value are consecutive
tell application "Microsoft Word"
	open alias wdPath
	set theTable to table 1 of active document
	set rowCount to (count rows of theTable)
	set columnCount to (count columns of theTable)
	set columnReference to 8
--set the columnReference to the column to compare the selectFilter value to
	if selectFilter is "All" then
		set firstRow to 1
		set lastRow to rowCount
		set firstRow to missing value
		set lastRow to missing value
		repeat with rr from 1 to rowCount
			set sortCell to (get cell (columnReference) of row rr of theTable)
			set sortCell to text object of sortCell
			set sortCell to move end of range of sortCell by a character item count -1
			if ((get content of sortCell) is equal to selectFilter) then
				if firstRow is not missing value then
					set lastRow to rr
					set firstRow to rr
					set lastRow to rr
				end if
			end if
		end repeat
	end if
	if lastRow is not missing value then
		set aRange to create range active document start (start of content of text object of (cell 1 of row firstRow) of theTable) end ((end of content of text object of (cell columnCount of row lastRow) of theTable) - 1)
		select aRange
		if selectFilter is "all" then
			sort text object of selection field number (columnReference) sort field type sort field alphanumeric sort order sort order ascending field number two 3 sort field type two sort field alphanumeric sort order two sort order ascending with exclude header without sort column
			sort text object of selection field number 3 sort field type sort field alphanumeric sort order sort order ascending without sort column
		end if
	end if
end tell