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

问题描述 投票:0回答:3

这是原始问题的扩展,当前段落的编号?我怎样才能得到它?,它很好地回答了 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 访问该变量

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

以下 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) 浏览器:火狐20.0 操作系统:Mac OS X (10.8)

vba applescript ms-word
3个回答
8
投票

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

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

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

0
投票

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

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

您好!

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

0
投票

要使用 Word VBA 获取当前段落的编号,这里有一个返回它的函数:

Public Function CurrentParagraphNum() As Integer
'AIM: Return the number of the paragraph where the cursor currently is placed.
'NOTE: If more than one paragraph is selected, it returns the number of the first parahraph in the selection.
Dim myrange As Range
Set myrange = ActiveDocument.Range(Start:=0, End:=Selection.Start + 1)
CurrentParagraphNum = myrange.Paragraphs.Count
End Function
© www.soinside.com 2019 - 2024. All rights reserved.