MS Word & Applescript (or VB, or both)

I’m looking for some sort of automatism in order to scan a MS Word file and, every time there is one WORD or maybe two WORDS with local formatting “Bold”, write some opening tag before the word and some closing tag after the word.

In other words:

Original text= “I am a bold word”
Resulting text="I am a @open code/bold@close code/ word

I tried to find out by myself, but VB is not a piece of cake, and I find the MS Word Applescript dictionary being a bit confusing…
thx,
Guido

This script works for me. Hope this helps


set Begin_code to "@open code/"
set End_Code to "@close code/"
set Begin_Code_Size to count of characters of Begin_code
set End_Code_Size to count of characters of End_Code


tell application "Microsoft Word"
	activate
	set curWord to word 1 of the active document
	set Start_Range to collapse range curWord direction collapse start
	set curWord to move range end until Start_Range characters " " & return & tab count go forward
	repeat while curWord is not equal to missing value
		if bold of font object of curWord then
			insert text End_Code at end of curWord
			set End_Tag_Start to collapse range curWord direction collapse end
			set End_Tag_Range to move end of range End_Tag_Start by a character item count End_Code_Size
			set the bold of the font object of End_Tag_Range to false
			insert text Begin_code at the beginning of curWord
			set End_Tag_Range to move end of range End_Tag_Range by a character item count End_Code_Size
			set curWord to collapse range End_Tag_Range direction collapse end
		else
			set curWord to move range curWord by a word item count 1
		end if
		if curWord is not equal to missing value then
			set Start_Range to collapse range curWord direction collapse start
			set curWord to move range end until Start_Range characters " " & return & tab count go forward
		end if
	end repeat
end tell

Yes, it does work, thx.
It needs maybe a bit of tweaking because, it’s a bit slow (but that could be a MS Word problem…) and it doesn’t recognize TWO consequent words in bold but it opens and closes every word, not considering if the next one is bold too.

It goes:
Original text= We are BOLD WORDS
Resulting text= We are @open code/BOLD@close code @open code/WORDS@close code/

instead of
Resulting text= We are @open code/BOLD WORDS@close code/

But I’ll try and figure it out by myself, if I can.

Thank you a gazillion, you’ve been so kind
g