Searching for multiple text instances in Word and formatting it...

I have a paragraph of text in Word composed of strings etc that gets printed to my document that looks this. I put the # % around the text so that I can find it later using those delimiters:

∙Header
#subtext%

∙Header2
#subtext2%

∙Header3
#subtext3%

I was wondering if it’s possible to search through that text, select everything between the # % and change the font size and italicize it.

I’ve had some luck finding the first instance of text between the delimiters # % but I can’t seem to select all the instances…

So is there any way to search though a range and format all of the text between delimiters?

Here’s a rather awkward way of accomplishing what you want.


property newFontSize : 24 as integer

set startChar to 0 as integer
repeat
	set startChar to my processPair(startChar)
	try
		if startChar > 0 then set startChar to startChar as integer
	on error
		exit repeat
	end try
end repeat


on processPair(startChar)
	tell application "Microsoft Word"
		activate
		set theCharN to the number of characters in the active document as integer
		try
			set textRange to (create range active document start startChar end theCharN)
			select textRange
			set findRange to find object of selection
			tell findRange
				execute find find text "#"
			end tell
			set startChar to (selection start of the selection) + 1 as integer
			select textRange
			tell findRange
				set foundIt to execute find find text "%"
				if foundIt is false then return false
			end tell
			set endChar to (selection end of the selection) - 1 as integer
			select (create range active document start startChar end endChar)
			set (font size of font object of selection) to newFontSize
			set italic of font object of selection to true
			set newStartChar to endChar + 2 as integer
			return newStartChar
		end try
	end tell
end processPair

Wow, that’s pretty amazing. Thank you for the script… I was just expecting some pointers or snippets. :smiley:

Is there an easy way to remove the special characters once the text is formatted? The text will be inside a table cell and it seems that the script gets caught in a loop as it continues to find the first special character and never move on from the first instance. I suspect something having to do with the table cell is messing it up.

The full script (the relevant part) is below. I tried modifying it but it doesn’t respond.

Nothing like a little Applecript to make Cocoa seem simple… !

tell application "Microsoft Word"
	set proposal to active document
	insert text " " at end of text object of active document
	set work1 to return
	set work1 to work1 & "・Header" & return
	set work1 to work1 & "     %subrow information^" & return
	set work1 to work1 & "・Header 2" & return
	set work1 to work1 & "・Header 3" & return
	set work1 to work1 & "     %subrow information 2^" & return
	
	tell application "Microsoft Word"
		--Create the proposal items tables
		set myRange to text object of active document
		set myRange to (collapse range myRange direction collapse end) make new table at active document with properties {number of rows:14, number of columns:3}
		
		set proposalTable to table 1 of active document
		
		merge cell (cell 1 of row 1 of proposalTable) with (cell 3 of row 1 of proposalTable)
		merge cell (cell 1 of row 12 of proposalTable) with (cell 3 of row 12 of proposalTable)
		set table item height (row 12 of proposalTable) row height 150 height rule row height auto
		merge cell (cell 2 of row 14 of proposalTable) with (cell 3 of row 14 of proposalTable)
		split cell (cell 1 of row 12 of proposalTable) number of columns 2
		set content of text object of (get cell from table proposalTable row 12 column 1) to work1
		set content of text object of (get cell from table proposalTable row 12 column 2) to work1
		set aCell to (get cell from table proposalTable row 12 column 1)
		set aRange to create range active document start ((end of content of text object of aCell) - 2) end ((end of content of text object of aCell) + 2)
		set content of aRange to ""
		set aCell to (get cell from table proposalTable row 12 column 2)
		set aRange to create range active document start ((end of content of text object of aCell) - 2) end ((end of content of text object of aCell) + 2)
		set content of aRange to ""
		set alignment of paragraph format of text object of (get cell from table proposalTable row 3 column 3) to align paragraph right
		merge cell (cell 3 of row 3 of proposalTable) with (cell 3 of row 10 of proposalTable)
		
		insert text " " at end of text object of active document
	end tell
end tell

property newFontSize : 8 as integer

set startChar to 0 as integer
repeat
	set startChar to my processPair(startChar)
	try
		if startChar > 0 then set startChar to startChar as integer
	on error
		exit repeat
	end try
end repeat


on processPair(startChar)
	tell application "Microsoft Word"
		activate
		set theCharN to the number of characters in the active document as integer
		try
			set textRange to (create range active document start startChar end theCharN)
			select textRange
			set findRange to find object of selection
			tell findRange
				execute find find text "%"
			end tell
			set startChar to (selection start of the selection) + 1 as integer
			select textRange
			tell findRange
				set foundIt to execute find find text "^"
				if foundIt is false then return false
			end tell
			set endChar to (selection end of the selection) - 1 as integer
			select (create range active document start startChar end endChar)
			set (font size of font object of selection) to newFontSize
			set italic of font object of selection to true
			
                        --This doesn't do anything except break the loop that the script gets stuck in.
			--set fixedRange to text object of (get cell from table proposalTable row 12 column 1)
			--tell fixedRange's find object
			--	clear formatting
			--	clear formatting its replacement
			--	execute find find text "%" replace with "      " replace replace all
			--	execute find find text "^" replace with "" replace replace all
			--end tell
			
			set newStartChar to endChar + 2 as integer
			return newStartChar
		end try
	end tell
end processPair

Sorry, I didn’t know the text you were concerned with was in a table. That requires a different approach to your problem. I was wondering if you wanted to delete those special characters. The fastest way is to simply do an Advanced Find and Replace for each of the characters. If its all in a single cell, select the entire cell and then do the Advanced Find and Replace.

To do it programmatically, this will work:


tell application "Microsoft Word"
	activate
	set findRange to find object of (create range active document start 0 end 0)
	tell findRange
		execute find find text "#" replace with "" replace replace all
		execute find find text "%" replace with "" replace replace all
	end tell
end tell

SO what do I do to stop the loop? Right now the script finds the first instance of %subrow information^ and properly acts on it but then it keeps finding the same instance (the first one) and just re-acting on it… the script never moves to the second instance of %…^ It’s just stuck in a loop on the first instance…

I’m guessing that right after the script changes the size and italics of the instance it found, the % and ^ need to be removed before it searches for another instance…?

I just ran the replace all routine on an open word document that starts with two sentences, then the exact text you had in your first post, followed by a two row table with three columns. Your exact text was in cells R1C1 and R2C2. It worked perfectly. Sorry…

Oh yeah the replace all routine works. The problem I’m running into, and you can see if you try and run my code is that the script gets stuck in a loop after changing the font size and italics of the first instance. The script gets stuck in a loop and never advances to the second or third instance where the %. ^ are.

If I run it on a document without the table it works fine but with the table it just gets stuck…

Solved it.

I had to add the find replace routine right after the text was formatted but modify it to only find and replace once…

Thanks for all the help!!



property newFontSize : 8 as integer

set startChar to 0 as integer
repeat
	set startChar to my processPair(startChar)
	try
		if startChar > 0 then set startChar to startChar as integer
	on error
		exit repeat
	end try
end repeat


on processPair(startChar)
	tell application "Microsoft Word"
		activate
		set theCharN to the number of characters in the active document as integer
		try
			set textRange to (create range active document start startChar end theCharN)
			select textRange
			set findRange to find object of selection
			tell findRange
				execute find find text "*"
			end tell
			set startChar to (selection start of the selection) --+ 1 as integer
			select textRange
			tell findRange
				set foundIt to execute find find text "^"
				if foundIt is false then return false
			end tell
			set endChar to (selection end of the selection) --- 1 as integer
			select (create range active document start startChar end endChar)
			set (font size of font object of selection) to newFontSize
			set italic of font object of selection to true
			
			set findRange to find object of selection
			tell findRange
				execute find find text "*" replace with "" replace replace one --changing replace all to replace one was the key
				execute find find text "^" replace with "" replace replace one --changing replace all to replace one was the key
			end tell
			
			set newStartChar to endChar + 1 as integer
			return newStartChar
		end try
	end tell
end processPair

Why don’t you pay someone to fix your code…

Ouch, was that necessary?

I asked a question, you posted some code and we had a few back and forths. Because of your help I was able to achieve what I wanted. I might have got there without your help, maybe it would have taken me some more time but that’s why we ask for help… If you didn’t want to help you didn’t have to but I don’t understand why you would come back with snark like that.

I’ve posted my solved code so that if anyone else ever comes across a problem like it they too can benefit.