Tuesday, September 21, 2021

#1 2008-09-05 04:25:34 am

going_blind
Member
Registered: 2008-09-05
Posts: 4

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

Applescript:

--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

Applescript:

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


Filed under: microsoft

Offline

 

#2 2008-09-05 08:37:59 am

going_blind
Member
Registered: 2008-09-05
Posts: 4

Re: Word 2008 find/replace italics applescript

That worked like magic!
Thank you

I had tried this but didn't work

Applescript:

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.
smile


Filed under: microsoft

Offline

 

#3 2008-09-08 08:36:59 pm

going_blind
Member
Registered: 2008-09-05
Posts: 4

Re: Word 2008 find/replace italics applescript

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?

Applescript:

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

Last edited by going_blind (2008-09-08 08:37:35 pm)


Filed under: microsoft

Offline

 

#4 2008-09-09 03:15:38 am

going_blind
Member
Registered: 2008-09-05
Posts: 4

Re: Word 2008 find/replace italics applescript

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

Applescript:

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


Filed under: microsoft

Offline

 

#5 2009-06-22 07:45:10 am

sjohnson717
Member
Registered: 2009-06-22
Posts: 2

Re: Word 2008 find/replace italics applescript

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)