Find/replace string too long for MS Word - need to insert

Hello everyone,
Once again I humbly come, hat in hand, asking for some guidance.

After much searching, I still can’t find the answer to this one, but I’m willing to bet it something simple that will aggravate me for not finding it on my own.

What the script is supposed to do -

  • get information from inputs to build strings to be written into specific locations in a Word document using find and replace so that I can use place holder text.
    However, the final string (text) contains too many characters for Word’s find and replace to use and it throws an error "Microsoft Word got an error: Can’t set content of replacement of find id «data iWio80D2AA1E» of selection to… "

Basically, I’ve tested everything and the variables correctly return the text I need. One of the problems is that the insertion position of this text will change as it is written twice on different pages of a single document and the second occurrence will be in a different location each time based on how long the string being written is. (I hope that makes sense, I’ve been staring at this screen for too long today.)

I know how to move the text to the clipboard if Find and Replace is not the best way to do this, but I don’t know how to tell the clipboard to paste at a certain location based off finding the place holder text.

Extra thanks in advance.

Here is the code



-- set carriage return

set cr to ASCII character 13
set LF to ASCII character 10
set carriage_return to (cr & LF as Unicode text)


-- get reasons for order change
set chng_reas1 to button returned of (display dialog "Has it been three (3) years since the current order was entered?" buttons {"Yes", "No"}) as text

set chng_reas2 to button returned of (display dialog "Has Movant's gross monthly income changed by greater than 20%" buttons {"Yes", "No"}) as text
-- get GMI amounts
if chng_reas2 = "Yes" then
	set form_gmi to text returned of (display dialog "What was Movant's former gross monthly income upon which the current order is based?" default answer "$2000.00") as text
	set cur_gmi to text returned of (display dialog "What is Movant's current gross monthly income?" default answer "$2000.00") as text
end if

set chng_reas3 to button returned of (display dialog "Are there additional changes in circumstances supporting a modification of child support?" buttons {"Yes", "No"}) as text

if chng_reas1 = "Yes" then
	set reas1_txt to ("It has been at least three (3) years sin the last order and mot_cap is requesting a review pursuant to NRS 125B.145;") as text
end if

-- set text for reasons

if chng_reas2 = "Yes" then
	set reas2_txt to ("mot_cap'S gross monthly income has changed by at least twenty percent (20%) since the last child support order was entered.  At the time of the current order mot_cap'S gross monthly income was" & form_gmi & ". mot_cap'S gross monthly income is now " & cur_gmi & ".") as text
end if

if chng_reas3 = "Yes" then
	set reas3_txt to ("The following changes(s) in circumstances has/have occurred since the last order which justify an adjustment of the previous order:  INSERT OTHER REASONS FOR MODIFCATION OF ORDER.") as text
	
end if

-- concatenate needed strings for document
if (chng_reas1 = "Yes" and chng_reas2 = "No" and chng_reas3 = "No") then
	set reas_txt to reas1_txt
	
else if (chng_reas1 = "Yes" and chng_reas2 = "Yes" and chng_reas3 = "No") then
	set reas_txt to reas1_txt & carriage_return & reas2_txt
	
else if (chng_reas1 = "Yes" and chng_reas2 = "Yes" and chng_reas3 = "Yes") then
	set reas_txt to reas1_txt & carriage_return & reas2_txt & carriage_return & reas3_txt
	
else if (chng_reas1 = "Yes" and chng_reas2 = "No" and chng_reas3 = "Yes") then
	set reas_txt to reas1_txt & carriage_return & reas3_txt
	
else if (chng_reas1 = "No" and chng_reas2 = "Yes" and chng_reas3 = "Yes") then
	set reas_txt to reas2_txt & carriage_return & reas3_txt
	
else if (chng_reas1 = "No" and chng_reas2 = "Yes" and chng_reas3 = "No") then
	set reas_txt to reas2_txt
	
else if (chng_reas1 = "No" and chng_reas2 = "No" and chng_reas3 = "Yes") then
	set reas_txt to reas3_txt
	
end if

-- put the final string on the clipboard in case I need it there in the solution
set the clipboard to reas_txt

tell application "Microsoft Word"
	
	tell application "Finder"
		set homdir to (get (path to home folder from user domain) as string)
	end tell
	
	tell application "Microsoft Word"
		activate
		
		-- open test template		
		set targdoc to homdir & "Library:Application Support:Microsoft:Office:User Templates:My Templates:test.dot"
		open file targdoc
		
	end tell
	
	set selFind to find object of selection
	
	-- find and replace place holder text	
	tell selFind
		set content to "reas_txt"
		set content of replacement of selFind to reas_txt
		execute find replace replace all
	end tell
	
end tell

MS Word v 12.2.0

Amazing what a good night’s sleep can do.

I figured it out…


	tell selFind
		set content to "reas_txt"
		execute find
		
		tell active document
			tell application "System Events" to keystroke "v" using {command down}
		end tell
	end tell

This will tell Word to find & highlight the text, then paste from the clipboard. Hope this helps somebody else.

Now, if I can just get it to hold the outline formatting… :cool: