'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)



Solution 1:[1]

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

Solution 2:[2]

If You enter this code, you will see the number of paragraph where cursor is /current paragraph/

Some changes are needed to see current paragraph in selected range. /in Selection object/. Maybe I'll post a new post later.

Greetings!

Sub paragraphs_above_cursor()
    'pos = ActiveDocument.Paragraphs(1).Range.Start
    pos = 0
    pos2 = Selection.Range.End
    Set myrange = ActiveDocument.Range(Start:=pos, End:=pos2)
    'myrange.Select
    MsgBox "Current Paragraph Number is " & myrange.Paragraphs.Count + 1
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Orwellophile