如何将Microsoft Word中当前选定段落的段落编号转换为AppleScript变量?

时间:2013-05-16 20:19:43

标签: vba ms-word applescript word-vba

这是作为原始问题的延伸,当前段落的编号?我怎样才能得到它?这对于回答iWork Pages的问题做得很好。 http://macscripter.net/viewtopic.php?id=29125

我想在Microsoft Word中做同样的事情。 我发现了一个可以获取段落编号的VBA脚本(如下所示)但我不知道VBA脚本是如何工作的,因此我被卡住了(也见下文)


更新17-05-2013

这个问题有两个解决方案。在阅读了VBA并检查代码之后,我意识到VBA脚本如何确定段落编号。实际上这很简单。它设置从字符0到光标位置的范围,然后计算该范围内的段落。

因此,我认为我的问题有两种可能的解决方案:

  1. 使用VBA CurPos的AppleScript等效项,以创建从文档位置0到位置cursor的文档范围。计算范围内的段落。
  2. 使用VBA脚本将段落编号设置为变量并通过AppleScript
  3. 访问该变量

    我的最终目标是在文档上运行一个循环,找到所有表并在其之前和之后连续插入分节符。

    以下VBA脚本提供了一个弹出对话框,显示了我需要的数据和更多(段落,绝对行号,相对行号)。也许有人可以帮助我将此脚本的输出设置为我可以通过AppleScript

    访问的文档变量
    Open this Scriplet in your Editor:
    
    get variable value of variable "paragraphNum" of active document
    

    这是VBA脚本:

    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
    

    当我得到段落编号时,我可以通过执行与此类似的操作之前插入一个分节符(当然我需要选择前一段的最后一个字符和后一段的第一个字符,但我需要先得到表格的段号!):

    Open this Scriplet in your Editor:
    
    insert break at text object of selection break type section break continuous
    

    型号:Macbook Air 2011 AppleScript:2.5.1(138.1) 浏览器:Firefox 20.0 操作系统:Mac OS X(10.8)

2 个答案:

答案 0 :(得分:4)

我不喜欢回答我自己的问题,但在撰写本文时,此页面上只有18个观点,我在此期间解决了我的问题。

该解决方案基于上面问题(纯AppleScript解决方案)中列出的解决方案1 ​​

要获取当前所选段落的段落编号,您可以在以下脚本中访问变量paragraphNum

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

答案 1 :(得分:0)

如果您输入此代码,您将看到光标所在的段落数/当前段落/

需要进行一些更改才能在选定范围内查看当前段落。 /in 选择对象/。 也许我稍后会发布一个新帖子。

您好!

jsonTest = fromString "{\"title\": \"Lord of the rings\", \"author\": \"{\\\"666\\\": \\\"Tolkien\\\"}\"}"