Insert page break in front of a specific title

Hi everyone,

I need to insert a page break into a document before a title. I tried using find and select and that doesn’t work. This is what I have but it doesn’t work:

tell application "Microsoft Word"
	set findRange to find object of selection
	tell findRange
		execute find find text "Scope of Work”Installation at Venue"
		select findRange
		insert break at text object of selection break type page break
	end tell
end tell

This is part of a larger AppleScript that creates a legal document from FileMaker data. Everything else I got working, except this part.

Thanks a lot in advance!

Agi

Model: Mac Pro
AppleScript: 2.2.4
Browser: Safari 537.36
Operating System: Mac OS X (10.8)

Hi,

your script considers only the selected text, so before execute find you have to select the entire text
Then the variable insertionLocation is set to the beginning of the paragraph and the page break is inserted


tell application "Microsoft Word"
	select text object of active document
	set findRange to find object of selection
	execute find findRange find text "Scope of Work”Installation at Venue"
	
	if found of findRange is true then
		set insertionLocation to create range active document start (start of content of ¬
			text object of paragraph 1 of selection) end (start of content of ¬
			text object of paragraph 1 of selection)
		insert break at insertionLocation -- page break is the default value
	end if
end tell

That’s a bit more extensive than I would’ve thought. Thank you so much, Stefan!

One last question (I hope):

Can I center a title based on text? E.g. I’d like to center “Terms & Conditions”

This is how I center now:

set theRange to (create range active document start 0 end 23)
select theRange
tell paragraph format of theRange to set alignment to align paragraph center

But I will not know the character position of the titles I need to center.

Thanks again.

Agi

This can also be done with the execute find command


tell application "Microsoft Word"
	select text object of active document
	set findRange to find object of selection
	execute find findRange find text "Terms & Conditions"
	
	if found of findRange is true then
		tell paragraph format of text object of selection to set alignment to align paragraph center
	end if
end tell


or if the paragraphs have a unique paragraph style you could use that as search criteria

Thanks, Stefan!