vba 如何将 Microsoft Word 中当前选定段落的段落编号获取到 AppleScript 变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16596725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:37:26  来源:igfitidea点击:

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

vbams-wordapplescriptword-vba

提问by Jonathan Komar

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

这旨在作为原始问题的扩展,当前段落的编号?我怎样才能得到它?,这很好地回答了 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)

我想在 Microsoft Word 中做同样的事情。我找到了一个可以抓取段落编号的 VBA 脚本(请阅读下文),但我不知道 VBA 脚本是如何工作的,因此我被卡住了(也请阅读下文)



UPDATE 17-05-2013

更新 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.

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

Therefore, I see two possible solutions to my question:

因此,对于我的问题,我看到了两种可能的解决方案:

  1. Use the AppleScript equivalent of the VBA CurPosin 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
  1. 使用与 VBA 等效的 AppleScriptCurPos来创建从位置 0 到位置的文档范围cursor。计算范围内的段落。
  2. 使用 VBA 脚本将段落编号设置为变量并通过 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

以下 VBA 脚本提供了一个弹出对话框,其中显示了我需要的数据等(段落、绝对行号、相对行号)。也许有人可以帮助我将此脚本的输出设置为我可以通过 AppleScript 访问的文档变量

Open this Scriplet in your Editor:

get variable value of variable "paragraphNum" of active document

Here is the VBA Script:

这是 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

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)

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

回答by Jonathan Komar

I dislike answering my own questions, but there have been only 18 views on this page at the time of writing this and I have solved my question in the meantime.

我不喜欢回答我自己的问题,但在撰写本文时,此页面上只有 18 次查看,同时我已经解决了我的问题。

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

该解决方案基于问题中列出的第 1 个解决方案(纯 AppleScript 解决方案)

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

要获取当前选定段落的段落编号,您可以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