MS Word - Table cell formatting

Hi Guys,

I am newbie in applescript.

I have created the following script to get values from excel, and Based on that Excel data, to create a 2 column table and 3 column table in an existing Word File (MS Word).

Script was running fine.

But when the Existing word file’s last paragraph was in Right Align.

The Newly created Tables (Cells) are also having Right align.
How to fix this?

Any help will be appreciated…

Thanks in advance,
John


tell application "Microsoft Excel"
	
	tell active sheet
		
		tell used range
			delay 2
			select (range "g1:g74")
			copy range (range "g1:g74")
			set thetext to the clipboard as list
			set thetext to thetext as text
		end tell
		
		set paracount to count of paragraphs of (the clipboard)
		repeat while thetext contains (return & return)
			set AppleScript's text item delimiters to return & return
			set theTextList to text items of thetext
			set AppleScript's text item delimiters to return
			set thetext to theTextList as text
		end repeat
		
		set reallist to paragraphs of thetext as text
		set totalcount to count of paragraphs of thetext
		set subcount to totalcount - 1
		set Firstcount to totalcount - 3
		if Firstcount > 1 then
			set finalcount to Firstcount / 3
		end if
		
	end tell
	
	
	if Firstcount is 0 then
		
		tell application "Microsoft Word"
			activate
			tell active document
				
				set nP to (count paragraph)
				
				-- the doc must have at least two blank paragraphs at end of it before creating the textbox
				
				if (content of text object of paragraph (nP - 1)) is not return then insert paragraph at after last character
				if (content of text object of paragraph nP) is not return then insert paragraph at after last character
				tell paragraph format of last paragraph to set alignment to align paragraph left
				delay 0.1
				select text object of last paragraph -- select the last blank paragraph
				
			end tell
			
			set oDoc to active document
			
			set oTable to make new table at oDoc with properties {number of rows:1, number of columns:2} ¬
				
			set iCount to 1
			repeat with oCell in (get cells of text object of oTable)
				set Newtext to paragraph iCount of thetext
				insert text Newtext at text object of oCell
				set iCount to iCount + 1
				if iCount = 3 then
					exit repeat
				end if
			end repeat
			set tcount to count of tables of oDoc
			tell paragraph format of column 1 of table tcount to set alignment to align paragraph center
			set tbl to table tcount of active document
			tell border options of tbl
				set inside line style to line style single
				set inside line width to line width50 point
				set outside line style to line style single
				set outside line width to line width50 point
			end tell
			
			
		end tell
	else
		tell application "Microsoft Word"
			activate
			
			tell active document
				
				set nP to (count paragraph)
				
				-- the doc must have at least two blank paragraphs at end of it before creating the textbox
				
				if (content of text object of paragraph (nP - 1)) is not return then insert paragraph at after last character
				if (content of text object of paragraph nP) is not return then insert paragraph at after last character
				select text object of last paragraph -- select the last blank paragraph
				
			end tell
			
			set oDoc to active document
			
			set oTable to make new table at oDoc with properties {number of rows:1, number of columns:2} ¬
				
			set iCount to 1
			repeat with oCell in (get cells of text object of oTable)
				set Newtext to paragraph iCount of thetext
				insert text Newtext at text object of oCell
				set iCount to iCount + 1
				if iCount = 3 then
					exit repeat
				end if
			end repeat
			set tcount to count of tables of oDoc
			set tbl to table tcount of active document
			tell border options of tbl
				set inside line style to line style single
				set inside line width to line width50 point
				set outside line style to line style single
				set outside line width to line width50 point
			end tell
			tell active document
				
				
				set nP to (count paragraph)
				
				-- the doc must have at least two blank paragraphs at end of it before creating the textbox
				
				if (content of text object of paragraph (nP - 1)) is not return then insert paragraph at after last character
				if (content of text object of paragraph nP) is not return then insert paragraph at after last character
				set aa to content of text object of last paragraph
				if aa = "" then
					delete last paragraph
				end if
				select text object of last paragraph -- select the last blank paragraph
				
			end tell
			
			set oDoc to active document
			
			try
				set oTable to make new table at oDoc with properties {number of rows:finalcount, number of columns:3} ¬
					
				set iCount to 3
				repeat with oCell in (get cells of text object of oTable)
					set Newtext to paragraph iCount of thetext
					insert text Newtext at text object of oCell
					set iCount to iCount + 1
					
				end repeat
				set tcount to count of tables of oDoc
				set tbl to table tcount of active document
				tell border options of tbl
					set inside line style to line style single
					set inside line width to line width50 point
					set outside line style to line style single
					set outside line width to line width50 point
				end tell
			end try
		end tell
	end if
end tell

Have you considered setting the paragraphs alignment right after you select the paragraph just before you create the table?

Insert something like

tell paragraph format of selection to set alignment to align paragraph center

or

tell paragraph format of selection to set alignment to align paragraph left

Model: Mac Pro (Mid 2010)
AppleScript: 2.7
Browser: Firefox 79.0
Operating System: macOS 10.14

Hi haolesurferdude

Where to exactly insert these below lines?
As I am newbie in Scripting, Please advice me… I have placed it in 2 different places. But both are not working.

tell paragraph format of thesel to set alignment to align paragraph center

if (content of text object of paragraph (nP - 1)) is not return then insert paragraph at after last character
				if (content of text object of paragraph nP) is not return then insert paragraph at after last character
				select text object of last paragraph -- select the last blank paragraph
				[b]tell paragraph format of thesel to set alignment to align paragraph center[/b]
						
			end tell
			

			set oDoc to active document
					
			set oTable to make new table at oDoc with properties {number of rows:1, number of columns:2} ¬
				
			set iCount to 1
			repeat with oCell in (get cells of text object of oTable)
				set Newtext to paragraph iCount of thetext
				insert text Newtext at text object of oCell
				[b]tell paragraph format of thesel to set alignment to align paragraph center[/b]
				set iCount to iCount + 1
				if iCount = 3 then
					exit repeat
				end if
			end repeat

Hi,

Your script is not very clear to me and I uninstalled Word, but I will try to help you using the Sherlock Holmes logic method. You don’t like the alignment of the new table cells here. So center its cells, and leave the previous paragraph alone.

Replace this:


set oTable to make new table at oDoc with properties {number of rows:1, number of columns:2} ¬

with this:

set oTable to make new table at oDoc with properties {number of rows:1, number of columns:2}
tell paragraph format of column 1 of oTable to set alignment to align paragraph center
tell paragraph format of column 2 of oTable to set alignment to align paragraph center

I think, this should work too:


set oTable to make new table at oDoc with properties {number of rows:1, number of columns:2}
tell paragraph format of columns of oTable to set alignment to align paragraph center