Applescript to replace text in MS Word document based from Excel file

Hi,


I'd like to replace sentences in a Word DOCX document. I have an Excel XLSX file, in column A the sentences what are must be replaced, and in column B the new sentences. I made lot of search, but I have found only VBA scripts, but they are not working on Mac Excel.


Anybody can help?


Thanks

Laszlo

Mac Studio

Posted on Jan 21, 2023 11:23 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 21, 2023 3:30 PM

The following will replace text in Word 16.69.1 from the used range in Excel 16.69.1, where that used range is the value of your two columns of find and replace strings. This was tested on Monterey v12.6.2.


The AppleScript will prompt you to select the Excel file first, followed by a second prompt to select the Word document. The script will find and replace the strings in the Word document, closing and exiting Word when done. You can review the result by selecting that Word document and pressing the spacebar to use Quick Look. I recommend that initially, you use a copy of the Word document. The Excel content is unchanged.


(*

AppleScript to open an Excel spreadsheet and copy its used range into a list of rows
comprised of the find string and replacement strings. The script will then loop through
all rows in the AppleScript list and perform a find/replace in the Word document.

Tested: macOS Monterey 12.6.2
VikingOSX, 2023-01-21, Apple Support Communities, No warrany expressed or implied.
*)

use scripting additions

set theXLSX to (choose file of type ("org.openxmlformats.spreadsheetml.sheet"))
set theDOCX to (choose file of type {"org.openxmlformats.wordprocessingml.document"})

tell application "Microsoft Excel"
	activate
	
	open file theXLSX
	
	tell active sheet of document 1
		-- create a list of lists where each row will become a two component list item
		-- comprised of the find string and the replace string
		set arange to value of used range
	end tell
end tell
tell application "Microsoft Excel" to if it is running then quit

tell application "Microsoft Word"
	activate
	open file theDOCX

	set myfind to find object of text object of active document
	set properties of myfind to {match case:false, match whole word:false, match wildcards:false}
	
	repeat with arow in arange
		execute find myfind find text (item 1 of arow) replace with (item 2 of arow) replace replace all
	end repeat
	close active document saving yes
end tell
tell application "Microsoft Word" to if it is running then quit

return

Similar questions

3 replies
Sort By: 
Question marked as Top-ranking reply

Jan 21, 2023 3:30 PM in response to szl1973

The following will replace text in Word 16.69.1 from the used range in Excel 16.69.1, where that used range is the value of your two columns of find and replace strings. This was tested on Monterey v12.6.2.


The AppleScript will prompt you to select the Excel file first, followed by a second prompt to select the Word document. The script will find and replace the strings in the Word document, closing and exiting Word when done. You can review the result by selecting that Word document and pressing the spacebar to use Quick Look. I recommend that initially, you use a copy of the Word document. The Excel content is unchanged.


(*

AppleScript to open an Excel spreadsheet and copy its used range into a list of rows
comprised of the find string and replacement strings. The script will then loop through
all rows in the AppleScript list and perform a find/replace in the Word document.

Tested: macOS Monterey 12.6.2
VikingOSX, 2023-01-21, Apple Support Communities, No warrany expressed or implied.
*)

use scripting additions

set theXLSX to (choose file of type ("org.openxmlformats.spreadsheetml.sheet"))
set theDOCX to (choose file of type {"org.openxmlformats.wordprocessingml.document"})

tell application "Microsoft Excel"
	activate
	
	open file theXLSX
	
	tell active sheet of document 1
		-- create a list of lists where each row will become a two component list item
		-- comprised of the find string and the replace string
		set arange to value of used range
	end tell
end tell
tell application "Microsoft Excel" to if it is running then quit

tell application "Microsoft Word"
	activate
	open file theDOCX

	set myfind to find object of text object of active document
	set properties of myfind to {match case:false, match whole word:false, match wildcards:false}
	
	repeat with arow in arange
		execute find myfind find text (item 1 of arow) replace with (item 2 of arow) replace replace all
	end repeat
	close active document saving yes
end tell
tell application "Microsoft Word" to if it is running then quit

return

Reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to replace text in MS Word document based from Excel file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.