Find/replace with footnotes in Word 2008

I’m trying to format an academic article that was typed in plain text and convert the author’s custom markup to actual rich text in Word 2008. Here’s an example of the author’s markup system:

Doing the bold and italic replacements is easy. I’m running into problems with the footnotes.

Here’s what I have so far:


tell application "Microsoft Word"
	set selFind to find object of selection
	set forward of selFind to true
	set wrap of selFind to find stop
	set match wildcards of selFind to true
	set content of selFind to "\\\\footnote\\{(*)\\}"
	set content of replacement of selFind to "\\1"
	execute find selFind replace replace all
end tell

It can successfully search the document using Word’s wildcard syntax and find all instances of \footnote{whatever} and as it stands it can replace the selected find with the first found group, or \1, Word’s syntax for the regex standard $1

That gets rid of the footnote syntax junk, but leaves the author’s reference in the text. I obviously need it to go in as a footnote.

Instead of finding and replacing the text, I’ve tried just cutting the whole selection out, inserting a footnote, and pasting the text, but I’ve run into problems with cut object and cut text range. Additionally, I’d have to run the find and replace again to get rid of the surrounding \footnote{} syntax in the footnotes. I’ve found that this partially works; it gets the text and saves it as a variable and adds a footnote, but doesn’t get rid of any text.


	...
	execute find selFind

	set strText to content of text object of selection
	collapse range text object of selection direction collapse end
	set myFootnote to make new footnote at active document with properties {text range:text object of selection}
	set content of text object of myFootnote to strText

Optimally, here’s what I’m trying to do, in pseudo code

Any ideas on how to get this all to work?

Thanks!

It’s really clunky, but I found a workaround. Here it is:


tell application "Microsoft Word"
	repeat
		set selFind to find object of selection
		set forward of selFind to true
		set wrap of selFind to find stop
		set match wildcards of selFind to true
		set content of selFind to "\\\\footnote\\{(*)\\}"
		
		if (not (execute find selFind)) then
			exit repeat
		end if
		
		set strText to content of text object of selection
		set content of selection to ""
		collapse range text object of selection direction collapse end
		set myFootnote to make new footnote at active document with properties {text range:text object of selection}
		set content of text object of myFootnote to strText
	end repeat
	
	activate
	
	tell application "System Events"
		tell process "Microsoft Word"
			tell menu bar 1
				tell menu bar item "View"
					tell menu "View"
						click menu item "Footnotes"
					end tell
				end tell
			end tell
		end tell
	end tell
	
	set selFind to find object of selection
	set forward of selFind to true
	set wrap of selFind to find continue
	set match wildcards of selFind to true
	set content of selFind to "\\\\footnote\\{(*)\\}"
	set content of replacement of selFind to "\\1"
	execute find selFind replace replace all
end tell

It searches for all the footnotes in the actual text, inserts them, and removes the markup. Since Word considers footnotes to be a separate story or section of the document, I have to then use GUI scripting to jump to the footnotes, and then fun another find and replace to remove the markup formatting surrounding the actual references.

Clunky, yes. Easier way to do it, probably. Work? Yep.