Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

superscape

macrumors 6502a
Original poster
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
Hi,

I can't see anything obvious in the scripting dictionary, but does anyone know a way of finding the text of the last line on a single page of a Word document is (using AppleScript).

Any clever ideas appreciated!

Thanks
 
Hi superscape,

The sentences element looks like what you want. There's a similar thread : https://forums.macrumors.com/threads/applescript-ms-word-delete-last-sentence-paragraph.1767586/
Forum member snorkelman seems to know a lot about scripting MS Word. You can also fall back to VBA.

Example:

Code:
tell application "Microsoft Word"
    set myDoc to active document
    -- Working with the selection object
    select sentence -1 of myDoc
    get content of text object of selection
   
    -- Working with text range objects
    set myRange to create range myDoc start (start of content of sentence -1 of myDoc) end (end of content of sentence -1 of myDoc)
    get content of myRange
end tell
 
Last edited:
Thanks, kryten2. That works great. I think I made a mistake explaining the problem though - I should have said that I wanted the last sentence on *each* page of a multi page document (my mistake).

I'm not sure this is possible since Word doesn't seem to have a "page" object.

I may have to have a word with snorkelman... unless you or anyone else has other ideas?
 
When I was answering your question I was thinking good, he only wants to do something on a single page because like you noticed there isn't any page object in the dictionary. After a bit of searching I found an old post on macscripter. net and that didn't look good.

A while back I ran into a similar problem when scripting the Microsoft Office suite and unfortunately I did not find a solution. Microsoft Word is a word processor, not a page based layout application like InDesign, Quark (and RagTime). I guess that is why there is no «page object» available in Word's AppleScript dictionary.

When you are working in Word, you are adding and removing text, and the pages are deleted and added automatically for you (of course you can enter section breaks). So the «text object» is very powerful in Word. But I was not able to delete single pages from a Word document with AppleScript.

Even in the Windows world, which uses Visual Basic for Word automation, this problem is well known:

Q: Delete Pages Using VBA in Word?
A: Word does not make this easy, for good reason. What's a page, anyway?

Of course the link in the post is dead however I'll continue searching the net. There's got to be a way to do this.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.