Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the paragraph number of the currently selected paragraph in Microsoft Word into an AppleScript variable?

This is intended as an extension of the original question, The number of the current paragraph? How can I get it?, which does a nice job answering the question for iWork Pages. http://macscripter.net/viewtopic.php?id=29125

I would like to do the same thing in Microsoft Word. I found a VBA script that can grab the paragraph number (read below) but I do not know how VBA script works, therefore I am stuck (also read below)


UPDATE 17-05-2013

There are two solutions to the problem. After reading up on VBA and examining the code, I realized how the VBA script determines the paragraph number. It is quite simple, actually. It sets a range spanning from character 0 to the cursor position, then counts the paragraphs in that range.

Therefore, I see two possible solutions to my question:

  1. Use the AppleScript equivalent of the VBA CurPos in order to create a range spanning the document from position 0 to position cursor. Count paragraphs in range.
  2. Use VBA Script to set paragraph number to variable and access that variable via AppleScript

My ultimate goal is to run a loop over the document that finds all tables and inserts a section break continuous before and after it.

The following VBA Script provides a pop-up dialog which shows the data I need and more (paragraph, absolute line number, relative line number). Maybe someone can help me set the output of this script to a document variable that I can access via AppleScript with

Open this Scriplet in your Editor:

get variable value of variable "paragraphNum" of active document

Here is the VBA Script:

Option Explicit

Sub WhereAmI()
    MsgBox "Paragraph number: " & GetParNum(Selection.Range) & vbCrLf & _
        "Absolute line number: " & GetAbsoluteLineNum(Selection.Range) & vbCrLf & _
        "Relative line number: " & GetLineNum(Selection.Range)
End Sub


Function GetParNum(r As Range) As Integer
    Dim rParagraphs As Range
    Dim CurPos As Integer

    r.Select
    CurPos = ActiveDocument.Bookmarks("\startOfSel").Start
    Set rParagraphs = ActiveDocument.Range(Start:=0, End:=CurPos)
    GetParNum = rParagraphs.Paragraphs.Count
End Function

Function GetLineNum(r As Range) As Integer
    'relative to current page
    GetLineNum = r.Information(wdFirstCharacterLineNumber)
End Function

Function GetAbsoluteLineNum(r As Range) As Integer
    Dim i1 As Integer, i2 As Integer, Count As Integer, rTemp As Range

    r.Select
    Do
        i1 = Selection.Information(wdFirstCharacterLineNumber)
        Selection.GoTo what:=wdGoToLine, which:=wdGoToPrevious, Count:=1, Name:=""

        Count = Count + 1
        i2 = Selection.Information(wdFirstCharacterLineNumber)
    Loop Until i1 = i2

    r.Select
    GetAbsoluteLineNum = Count
End Function

When I get the paragraph number, I can then insert a section break continuous before by doing something similar to this (of course I would need to do select the last character of the preceding paragraph and the first character of the subsequent paragraph, but I need to get the paragraph number of the table first!):

Open this Scriplet in your Editor:

insert break at text object of selection break type section break continuous

Model: Macbook Air 2011 AppleScript: 2.5.1 (138.1) Browser: Firefox 20.0 Operating System: Mac OS X (10.8)

like image 228
Jonathan Komar Avatar asked May 16 '13 20:05

Jonathan Komar


People also ask

How do you number existing paragraphs in word?

Select your paragraphs by dragging your cursor through them and head to the Home tab. To apply the default number style, click “Numbering” in the Paragraph section of the ribbon. To use a different number format, click the arrow next to the Numbering button and select an option.

How do you show the paragraph count?

Click the Review tab on the ribbon. Click the Word Count button in the Proofing group. You can also click the word count in the status bar to open the Word Count dialog box. Here you can see not only the word count, but also a character count (both with and without spaces), a paragraph count, and a line count.

How do you select a current paragraph in word?

To select a single word, quickly double-click that word. To select a line of text, place your cursor at the start of the line, and press Shift + down arrow. To select a paragraph, place your cursor at the start of the paragraph, and press Ctrl + Shift + down arrow.


1 Answers

The solution is based on solution number 1 listed above in the question (pure AppleScript solution)

To get the paragraph number of the currently selected paragraph, you can access variable paragraphNum in the following script

tell application "Microsoft Word"
    set myDoc to active document
    set myRange to create range myDoc start 0 end (start of content of text object of selection)
    set paragraphNum to (count paragraphs in myRange)
end tell
like image 82
Jonathan Komar Avatar answered Oct 02 '22 16:10

Jonathan Komar