AppleScript with Microsoft Word - how to find and mark selections

We have a “boilerplate” document in a Word Template. Based on user input to a series of menu questions, I want to be able to locate and delete blocks of text which may be several paragraphs long.

I have worked with both AppleScript and Visual Basic for Application in the past. I have the 2014 Word Dictionary reference. However, it is very complex and no “finished” examples seem available.

It looks like most of the code for manipulation would be in AppleScript. The only beginners tutorial I’ve found simply assumes selections in the Word document are made manually; I need to perform selection programmatically based on “found” points in the document and then delete.

Could someone point me to a book, overview article or specfic examples?

I’m going to answer this one since it seems like something anyone working with Word would want to do.

To find and delete text I use the following handler. You have to pass the parameters for first words in paragraph to find and the number of paragraphs to delete.


on FindDelete(FindText, NumParagraphs) 
	
set AllParagraphs to false 	
set ParagraphNum to NumParagraphs 	

tell application "Microsoft Word" 		
     set MyDoc to active document 		
     --the following selects the first word of doc, necessary for subsequent find to work 		
     select word 1 of MyDoc 		

     set findRange to find object of selection 		
     clear formatting findRange -- clear any previous formatting used in a find operation 		
     set forward of findRange to true -- find forward 		
     set wrap of findRange to find continue 		
     set foundIt to false --initialize found indicator 		 		
     set ParagraphNum to 1 		 		
     repeat while foundIt is false 			 			
          --this locates the text 			
          tell findRange 				
               set foundIt to execute find find text FindText -- do the search true-found false-not found 			
           end tell 			 			
                repeat while AllParagraphs is false 				
                     if ParagraphNum = NumParagraphs then 					
                          set AllParagraphs to true --this is the last paragraph to delete 				
                     end if 				
                     --the "finding" process apparently moves focus to the rest of the document 				
                     --limit to paragraph 1 				
                      delete (text object of paragraph 1 of selection) -- delete it 				
                      set ParagraphNum to ParagraphNum + 1 			
                  end repeat 		
      end repeat 	
      end tell 
end FindDelete     

I put in lovely indentations for readability which the forum parser seems to have removed. Thanks, anyway.

I found that deleting rows from tables was very easy. You just have to know the sequential number of the table in the document and realize that the numbers will change with table insertions and deletions.
The first part of the code is the method of opening a new document based on a template. You will have to edit the path information.
Then, one line deletes row 8 of table 1. Row numbering and table numbering are one-based. (First one is #1)


--address the application and tell it to open the selected file
tell application "Microsoft Word"
	activate
	set p to get default file path file path type user templates path
	set templatePath to p & ":My Templates:Proposal and GenInfo:Proposal.dotx"
	make new document with properties {attached template:templatePath}
	
	delete row 8 of table 1 of active document
end tell