Word 2008 find/replace italics applescript

Hello,
I have been trying to obtain an applescript that does the following.

Search for italic
wrap around the found text the corresponding html code for italic

I can do this manually by using the find/replace window in Word 2008.

Up to now I have done the following.

I have taken the following script

--Clean Up Text
tell application "Microsoft Word"
	set findRange to find object of selection
	tell findRange
		execute find find text "^p^p" replace with "¶" replace replace all
		execute find find text "^p" replace with " " replace replace all
		execute find find text "¶" replace with "^p" replace replace all
		execute find find text "^w" replace with " " replace replace all
		execute find find text "^p^w" replace with "^p" replace replace all
	end tell
end tell

Modified it like this

tell application "Microsoft Word"
	set findRange to find object of selection
	tell findRange
		execute find find [b]text[/b] "" replace with "<i>^&</i>" replace replace all
	end tell
end tell

The text is where I need to insert the parameter to find the text formatted in italic if I understand correctly.

Please note that if in the above script you insert some parameter like

execute find find text “cat”

and there is the word cat, then the italic html code is inserted properly.

So what I am missing is “just” the find portion of the script.

Thank you

That worked like magic!
Thank you

I had tried this but didn’t work

tell application "Microsoft Word"
	activate
	set textRange to font object of active document
	set italic of font object to true
	execute find find text "" replace with "<i>^&</i>" replace replace all
	
end tell

Thank you very much.
:slight_smile:

I need to ask your help again.
I have modified the script to change italic, bold and underline. My problem is that I need it to clear the search box formatting after each search or it will not search for any formatting except the ones searched before.

I tried using
set italic of font object to false
but it doesn’t solve the problem

Any suggestions?

tell application "Microsoft Word"
	set findRange to find object of selection
	tell findRange
		set italic of font object to true
		execute find find text "" replace with "<i>^&</i>" replace replace all
		set italic of font object to false
	end tell
	set findRange to find object of selection
	tell findRange
		set bold of font object to true
		execute find find text "" replace with "<b>^&</b>" replace replace all
		set bold of font object to false
	end tell
	set findRange to find object of selection
	tell findRange
		set underline of font object to true
		execute find find text "" replace with "<u>^&</u>" replace replace all
		set underline of font object to false
	end tell
end tell

Thank you again.
I have slightly modified it because it wouldn’t replace already searched styles.

tell application "Microsoft Word"
	set findRange to find object of selection
	tell findRange
		clear formatting
		clear formatting replacement of it
		set italic of font object to true
		execute find find text "" replace with "<i>^&</i>" replace replace all
		set italic of font object to false
		clear formatting
		set bold of font object to true
		execute find find text "" replace with "<b>^&</b>" replace replace all
		set bold of font object to false
		clear formatting
		set underline of font object to true
		execute find find text "" replace with "<u>^&</u>" replace replace all
		clear formatting
	end tell
end tell

Thank you

Here’s what I use for posting to the web:

tell application “Microsoft Word”

set curly to false -- change to true if curly quotes desired
set wasSmartQuotes to auto format as you type replace quotes of settings
set auto format as you type replace quotes of settings to curly
set myFind to find object of selection
tell myFind
	clear formatting myFind
	--replace ems and quotes
	execute find find text "”" replace with "--" replace replace all
	execute find find text "'" replace with "'" replace replace all
	execute find find text quote replace with quote replace replace all
	-- remove extra spaces
	execute find find text " ^w" replace with " " replace replace all
	-- replace paragraphs
	execute find find text "^p" replace with "<p>" replace replace all
	-- format ital
	clear formatting
	set italic of font object to true
	execute find find text "" replace with "<i>^&</i>" replace replace all
	set italic of font object to false
	-- format bold
	clear formatting
	set bold of font object to true
	execute find find text "" replace with "<b>^&</b>" replace replace all
	set bold of font object to false
end tell

set auto format as you type replace quotes of settings to wasSmartQuotes

end tell

(*
Notes:
Select the range of text you want to convert first.
You may have to fix the em-dash in quotes after “find text” in the first Execute Find line when pasting into Script Editor.
Fixing the smart quotes is a little tricky, as Find’s behavior varies based on the SmartQuotes property. So the script turns off smartQuotes, does the replace quote with quote and then restores SmartQuotes.
Save the script into Documents/Microsoft User Data/Word Script Menu Items to add it to your script menu.
*)

Remember to select the text first.

Steve