Microsoft Word and Customize Outline Numbered List

I wrote an applescript to set up a word document with the appropriate styles to give the document an academic setup, much like what Xelatex produces. This is a work in progress and I have a problem.

I have successfully applied the style changes to each Header and I have applied the desired outline numbered list template “list template 5 of list gallery 3”, however I would like to add tab stops to each one in order to make each header look slick like in Latex.

To do what I want manually in Microsoft Word 2011 mac I would click Bullets and Numbering, customize, under text position i would check the tab stop box and put in some value (1 cm, for example)

The following is an approximation of the result of running this script on a document with headings 1-9 applied consequently to each line, respectively. Note how they are not even. I think this is an issue with the template, but I want to be able to run this script on any mac without extra template files.

1 Heading 1
1.1 Heading 2
1.1.1 Heading 3
1.1.1.1 Heading 4
1.1.1.1.1 Heading 5
1.1.1.1.1.1 Heading 6
1.1.1.1.1.1.1 Heading 7
1.1.1.1.1.1.1.1Heading 8
1.1.1.1.1.1.1.1.1 Heading 9


(*
Academic Paper Setup

Copyright © 2012 macmadness86

Just kidding there is no copyright. staying open source. stupid SOPA.

You may use this code however you like. It would be nice 
if you made reference to the original code when sharing.

THINGS TO DO:
find a fix for changing font color of headings without hardcoding it.
fix headings 4-9
*)



(*<Properties to be set by the user*)
--number suffix corresponds to heading 1, heading 2, etc.
--NOTE: To change the coloring of headings, it must be hardcoded. must find a fix.
property fontName1 : "CMU Serif Roman"
property fontName2 : "CMU Serif Roman"
property fontName3 : "CMU Serif Roman"
property fontName4 : "CMU Serif Roman"
property fontName5 : "CMU Serif Roman"
property fontName6 : "CMU Serif Roman"
property fontName7 : "CMU Serif Roman"
property fontName8 : "CMU Serif Roman"
property fontName9 : "CMU Serif Roman"
property fontSize1 : 15
property fontSize2 : 14
property fontSize3 : 13
property fontSize4 : 12
property fontSize5 : 12
property fontSize6 : 12
property fontSize7 : 12
property fontSize8 : 12
property fontSize9 : 12
property boldTF : true
property italicTF : false
property highlightTF : false
(*Properties to be set by the user>*)

tell application "Microsoft Word"
	tell active document
		(*<normal code BLOCK*)
		set name of font object of Word style style normal to "CMU Serif Roman"
		set font size of font object of Word style style normal to 12
		set bold of font object of Word style style normal to false
		set italic of font object of Word style style normal to false
		set color index of font object of Word style style normal to black
		(*normal code BLOCK>*)
		(*<heading 1 code BLOCK*)
		set name of font object of Word style style heading1 to fontName1
		set font size of font object of Word style style heading1 to fontSize1
		set bold of font object of Word style style heading1 to boldTF
		set italic of font object of Word style style heading1 to italicTF
		set color index of font object of Word style style heading1 to black
		--set paragraph format left indent of paragraph format of Word style style heading1 to (centimeters to points centimeters 0) --indent sections relative to margin
		--set tab stop position of tab stop of Word style style heading1 to (centimeters to points centimeters 1)
		--set tab hanging indent of paragraph format of Word style style heading1 to count 1
		(*heading 1 code BLOCK>*)
		(*<heading 2 code BLOCK*)
		set name of font object of Word style style heading2 to fontName2
		set font size of font object of Word style style heading2 to fontSize2
		set bold of font object of Word style style heading2 to boldTF
		set italic of font object of Word style style heading2 to italicTF
		set color index of font object of Word style style heading2 to black
		(*heading 2 code BLOCK>*)
		(*<heading 3 code BLOCK*)
		set name of font object of Word style style heading3 to fontName3
		set font size of font object of Word style style heading3 to fontSize3
		set bold of font object of Word style style heading3 to boldTF
		set italic of font object of Word style style heading3 to italicTF
		set color index of font object of Word style style heading3 to black
		(*heading 3 code BLOCK>*)
		(*<heading 4 code BLOCK*)
		set name of font object of Word style style heading4 to fontName4
		set font size of font object of Word style style heading4 to fontSize4
		set bold of font object of Word style style heading4 to boldTF
		set italic of font object of Word style style heading4 to italicTF
		set color index of font object of Word style style heading4 to black
		(*heading 4 code BLOCK>*)
		(*<heading 5 code BLOCK*)
		set name of font object of Word style style heading5 to fontName5
		set font size of font object of Word style style heading5 to fontSize5
		set bold of font object of Word style style heading5 to boldTF
		set italic of font object of Word style style heading5 to italicTF
		set color index of font object of Word style style heading5 to black
		(*heading 5 code BLOCK>*)
		(*<heading 6 code BLOCK*)
		set name of font object of Word style style heading6 to fontName6
		set font size of font object of Word style style heading6 to fontSize6
		set bold of font object of Word style style heading6 to boldTF
		set italic of font object of Word style style heading6 to italicTF
		set color index of font object of Word style style heading6 to black
		(*heading 6 code BLOCK>*)
		(*<heading 7 code BLOCK*)
		set name of font object of Word style style heading7 to fontName7
		set font size of font object of Word style style heading7 to fontSize7
		set bold of font object of Word style style heading7 to boldTF
		set italic of font object of Word style style heading7 to italicTF
		set color index of font object of Word style style heading7 to black
		(*heading 7 code BLOCK>*)
		(*<heading 8 code BLOCK*)
		set name of font object of Word style style heading8 to fontName8
		set font size of font object of Word style style heading8 to fontSize8
		set bold of font object of Word style style heading8 to boldTF
		set italic of font object of Word style style heading8 to italicTF
		set color index of font object of Word style style heading8 to black
		(*heading 8 code BLOCK>*)
		(*<heading 9 code BLOCK*)
		set name of font object of Word style style heading9 to fontName9
		set font size of font object of Word style style heading9 to fontSize9
		set bold of font object of Word style style heading9 to boldTF
		set italic of font object of Word style style heading9 to italicTF
		set color index of font object of Word style style heading9 to black
		(*heading 9 code BLOCK>*)
		
	end tell
	
	(*<apply numbering to the sections code BLOCK*)
	set selFind to find object of selection --selects text of execute find command
	clear formatting selFind --reset selFind just to make sure
	set style of selFind to style heading1 --set style we are looking for
	execute find selFind find text "" wrap find find continue with find format and match forward
	if found of selFind is true then
		--display dialog (get content of text object of selection) --for debugging only
		(*<code BLOCK word__set paragraph style*)
		--I apply the formatting to the previously found text
		set myLT to list template 5 of list gallery 3 of active document
		apply list format template (list format of text object of selection) ¬
			list template myLT
	end if
	(*Apply numbering to the sections code BLOCK>*)
	
	
	
	
	
	
	
	
	
	
	
	
	(*******************************************
	(*<code BLOCK word_get style.scpt*)
	--i select the header 1 stuff
	set selFind to find object of selection --selects text of execute find command
	clear formatting selFind --reset selFind just to make sure
	set style of selFind to style heading1 --set style we are looking for
	execute find selFind find text "" wrap find find continue with find format and match forward
	if found of selFind is true then
		--display dialog (get content of text object of selection) --for debugging only
		(*<code BLOCK word__set paragraph style*)
		--I apply the formatting to the previously found text
		set theRange to text object of active document --i say where to apply the script
		set myStyle to Word style of object of selection of active document --debug only
		--set myStyle to make new Word style at active document with properties ¬
		{name local:"Section", style type:style type paragraph}
		tell font object of selFind
			set name to fontName1
			set font size to fontSize1
			set bold to true
		end tell
		set myLT to list template 5 of list gallery 3 of active document
		apply list format template (list format of text object of selection) ¬
			list template myLT
		(*code BLOCK word__set paragraph style>*)
		--set style of theRange to myStyle --i actually apply the settings
		update styles active document
	end if
	(*code BLOCK word_get style.scpt>*)
	*********************************)
	
end tell

Any experts care to help?

I never solved this problem but I made progress. I was able to create a plist to store the Word Doc settings. The plist is created from an excel document for easy editing of settings. I hope this helps someone:

set userDir to (path to current user folder) as string
--set theFile to (choose file with prompt "Please select a Microsoft Excel file") as string
set theFile to userDir & "Documents:Everything Else:academic paper settings.xlsx"

display dialog theFile as string
set theColumn to text returned of (display dialog "Please select a range" default answer "A2:C86")
set theRange to (theColumn) as string
tell application "Microsoft Excel"
	open workbook workbook file name theFile
	set aList to get value of range theRange of active sheet
end tell

--set up new plist file using the empty dictionary list item as contents
set plistfilePath to userDir & "Library:Preferences:msword.Academicsetup.plist"
tell application "System Events"
	set the parent_dictionary to make new property list item with properties {kind:record}
	set outFile to make new property list file with properties {contents:parent_dictionary, name:plistfilePath}
	--add values to plist
	tell outFile
		repeat with i from 1 to (count of aList)
			set keyName to (item 1 of (item i of aList) as string)
			set keyValue to (item 2 of (item i of aList) as string)
			set keyType to (item 3 of (item i of aList) as string)
			if keyType is "boolean" then
				make new property list item at end with properties ¬
					{kind:boolean, name:keyName, value:keyValue}
			end if
			if keyType is "string" then
				make new property list item at end with properties ¬
					{kind:string, name:keyName, value:keyValue}
			end if
			if keyType is "integer" then
				--important step to avoid real numbers (with decimals), coerse keyValue to be integer
				-- create an integer entry
				make new property list item at end with properties ¬
					{kind:number, name:keyName, value:(keyValue as integer)}
			end if
		end repeat
	end tell
end tell
tell application "Microsoft Excel"
	close workbook "academic paper settings.xlsx"
end tell
say "Dieses Skript ist fertig!"

Then I use the following script which refers to the aforecreated plist and implements the settings:

set plistQ to button returned of (display dialog "Would you like to use an existing plist?" buttons {"No", "Yes"} default button "Yes")
if plistQ is "No" then choose file with prompt "Please choose an excel file with the settings"
(*USE EXISTING PLIST START*)

set userDir to (path to current user folder) as string
set plistfilePath to userDir & "Library:Preferences:msword.Academicsetup.plist"
global plistfilePath
(*USE EXISTING PLIST*)

tell application "Microsoft Word"
	--tell it to make a new document
	if (count document) = 0 then
		set newDoc to make new document
	else
		set newDoc to document 1
	end if
	
	
	
	(*###Insert Text in Document###*)
	set header to my pL("headL") & tab & my pL("headM") & tab & my pL("headR")
	set footer to my pL("footL") & tab & my pL("footM") & tab & my pL("footR") -- : lFoot & tab & mFoot & tab & rFoot
	
	
	my makeCover()
	
	tell active document
		
		(*###PARAGRAPH STYLES###*)
		(*<normal code BLOCK*)
		set name of font object of Word style style normal to my pL("fontNormal")
		set font size of font object of Word style style normal to my pL("sizeNormal") as integer
		set bold of font object of Word style style normal to my pL("boldNormal")
		set italic of font object of Word style style normal to my pL("italicsNormal")
		set color index of font object of Word style style normal to black
		(*normal code BLOCK>*)
		(*<heading 1 code BLOCK*)
		set name of font object of Word style style heading1 to my pL("fontHeading1")
		set font size of font object of Word style style heading1 to my pL("sizeHeading1")
		set bold of font object of Word style style heading1 to my pL("boldHeading1")
		set italic of font object of Word style style heading1 to my pL("italicsHeading1")
		set color index of font object of Word style style heading1 to black
		--set paragraph format left indent of paragraph format of Word style style heading1 to my (centimeters to my points centimeters 0) --indent sections relative to my margin
		--set tab sto myp position of tab sto myp of Word style style heading1 to my (centimeters to my points centimeters 1)
		--set tab hanging indent of paragraph format of Word style style heading1 to my count 1
		(*heading 1 code BLOCK>*)
		(*<heading 2 code BLOCK*)
		set name of font object of Word style style heading2 to my pL("fontHeading2")
		set font size of font object of Word style style heading2 to my pL("sizeHeading2")
		set bold of font object of Word style style heading2 to my pL("boldHeading2")
		set italic of font object of Word style style heading2 to my pL("italicsHeading2")
		set color index of font object of Word style style heading2 to black
		(*heading 2 code BLOCK>*)
		(*<heading 3 code BLOCK*)
		set name of font object of Word style style heading3 to my pL("fontHeading3")
		set font size of font object of Word style style heading3 to my pL("sizeHeading3")
		set bold of font object of Word style style heading3 to my pL("boldHeading3")
		set italic of font object of Word style style heading3 to my pL("italicsHeading3")
		set color index of font object of Word style style heading3 to black
		(*heading 3 code BLOCK>*)
		(*<heading 4 code BLOCK*)
		set name of font object of Word style style heading4 to my pL("fontHeading4")
		set font size of font object of Word style style heading4 to my pL("sizeHeading4")
		set bold of font object of Word style style heading4 to my pL("boldHeading4")
		set italic of font object of Word style style heading4 to my pL("italicsHeading4")
		set color index of font object of Word style style heading4 to black
		(*heading 4 code BLOCK>*)
		(*<heading 5 code BLOCK*)
		set name of font object of Word style style heading5 to my pL("fontHeading5")
		set font size of font object of Word style style heading5 to my pL("sizeHeading5")
		set bold of font object of Word style style heading5 to my pL("boldHeading5")
		set italic of font object of Word style style heading5 to my pL("italicsHeading5")
		set color index of font object of Word style style heading5 to black
		(*heading 5 code BLOCK>*)
		(*<heading 6 code BLOCK*)
		set name of font object of Word style style heading6 to my pL("fontHeading6")
		set font size of font object of Word style style heading6 to my pL("sizeHeading6")
		set bold of font object of Word style style heading6 to my pL("boldHeading6")
		set italic of font object of Word style style heading6 to my pL("italicsHeading6")
		set color index of font object of Word style style heading6 to black
		(*heading 6 code BLOCK>*)
		(*<heading 7 code BLOCK*)
		set name of font object of Word style style heading7 to my pL("fontHeading7")
		set font size of font object of Word style style heading7 to my pL("sizeHeading7")
		set bold of font object of Word style style heading7 to my pL("boldHeading7")
		set italic of font object of Word style style heading7 to my pL("italicsHeading7")
		set color index of font object of Word style style heading7 to black
		(*heading 7 code BLOCK>*)
		(*<heading 8 code BLOCK*)
		set name of font object of Word style style heading8 to my pL("fontHeading8")
		set font size of font object of Word style style heading8 to my pL("sizeHeading8")
		set bold of font object of Word style style heading8 to my pL("boldHeading8")
		set italic of font object of Word style style heading8 to my pL("italicsHeading8")
		set color index of font object of Word style style heading8 to black
		(*heading 8 code BLOCK>*)
		(*<heading 9 code BLOCK*)
		set name of font object of Word style style heading9 to my pL("fontHeading9")
		set font size of font object of Word style style heading9 to my pL("sizeHeading9")
		set bold of font object of Word style style heading9 to my pL("boldHeading9")
		set italic of font object of Word style style heading9 to my pL("italicsHeading9")
		set color index of font object of Word style style heading9 to black
		(*heading 9 code BLOCK>*)
		
		(*###PAGE SETUP###*)
		(*Set Header and Footer*)
		set marginpageL to (my pL("marginpageL") as integer)
		set marginpageR to (my pL("marginpageR") as integer)
		set marginpageT to (my pL("marginpageT") as integer)
		set marginpageB to (my pL("marginpageB") as integer)
		
		set pageSetup to page setup of section 1
		set left margin of pageSetup to (centimeters to points centimeters marginpageL)
		set right margin of pageSetup to (centimeters to points centimeters marginpageR)
		set top margin of pageSetup to (centimeters to points centimeters marginpageT)
		set bottom margin of pageSetup to (centimeters to points centimeters marginpageB)
		
		set headerlineAns to button returned of (display dialog "Would you like a line under the header?" buttons {"No", "Yes"} default button "Yes")
		set footerlineAns to button returned of (display dialog "Would you like a line above the footer?" buttons {"No", "Yes"} default button "Yes")
		if headerlineAns is "Yes" then
			set s1 to section 1
			set theBorder to get border of paragraph 1 of text object of (get header s1 index header footer primary) which border border bottom
			set line style of theBorder to line style single
			set line width of theBorder to line width25 point
		else
			set s1 to section 1
			set theBorder to get border of paragraph 1 of text object of (get header s1 index header footer primary) which border border bottom
			set line style of theBorder to line style none
		end if
		if footerlineAns is "Yes" then
			set s1 to section 1
			set theBorder to get border of paragraph 1 of text object of (get footer s1 index header footer primary) which border border top
			set line style of theBorder to line style single
			set line width of theBorder to line width25 point
		else
			set s1 to section 1
			set theBorder to get border of paragraph 1 of text object of (get header s1 index header footer primary) which border border bottom
			set line style of theBorder to line style none
		end if
		
	end tell
	(*<apply numbering to the sections code BLOCK*)
	set selFind to find object of selection --selects text of execute find command
	clear formatting selFind --reset selFind just to make sure
	set style of selFind to style heading1 --set style we are looking for
	execute find selFind find text "" wrap find find continue with find format and match forward
	if found of selFind is true then
		--display dialog (get content of text object of selection) --for debugging only
		(*<code BLOCK word__set paragraph style*)
		--I apply the formatting to the previously found text
		set myLT to list template 5 of list gallery 3 of active document
		apply list format template (list format of text object of selection) ¬
			list template myLT
	end if
	(*Apply numbering to the sections code BLOCK>*)
end tell




on pL(theItem)
	tell application "System Events"
		set p_list to property list file (my plistfilePath)
		value of property list item (theItem as string) of p_list
	end tell
end pL

(*Title Page Handler*)
on makeCover()
	tell application "Microsoft Word"
		set myRange to create range active document start 0 end 0
		insert text "[Title] " at myRange
		set style of paragraph 1 of active document to style title
	end tell
end makeCover

It is a work in progress but I hope it helps someone.