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:
- Use the AppleScript equivalent of the VBA
CurPos
in order to create a range spanning the document from position 0 to positioncursor
. Count paragraphs in range. - 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)