可靠地识别Word文档中用于VBA代码的位置

可靠地识别Word文档中用于VBA代码的位置,vba,ms-word,Vba,Ms Word,在Word中,我无休止地以递增的数字运行以下命令,以查找需要使用VBA修改的文档部分: MsgBox(ActiveDocument.段落(56).范围.文本) 还有桌子。这是非常缓慢和尴尬的;文档上是否有方法标识光标所在的VBA“位置” 例如,如果我突出显示一个单词,它会告诉我“这是段落x,句子y,单词z”等?要根据选择获取段落编号,您必须对范围对象使用一点jiggery扑克 Function GetParagraphNumber(ByVal ipRange As Word.Range) As

在Word中,我无休止地以递增的数字运行以下命令,以查找需要使用VBA修改的文档部分:

MsgBox(ActiveDocument.段落(56).范围.文本)

还有桌子。这是非常缓慢和尴尬的;文档上是否有方法标识光标所在的VBA“位置”


例如,如果我突出显示一个单词,它会告诉我“这是段落x,句子y,单词z”等?

要根据选择获取段落编号,您必须对范围对象使用一点jiggery扑克

Function GetParagraphNumber(ByVal ipRange As Word.Range) As Long

    ' NB The ByVal is critical
    ipRange.Start = 0
    GetParagraphNumber =ipRange.Paragraphs.Count + 1

End Function

可以为单词和字符派生类似的函数。

要根据选择获取段落编号,您必须对range对象使用一点jiggery扑克

Function GetParagraphNumber(ByVal ipRange As Word.Range) As Long

    ' NB The ByVal is critical
    ipRange.Start = 0
    GetParagraphNumber =ipRange.Paragraphs.Count + 1

End Function

可以为单词和字符派生类似的函数。

例如,下面标识文档正文中任意长度的选择的开始和结束位置,包括表索引和单元格地址(如果适用):

Sub Demo()
Dim RngStart As Range, RngEnd As Range, RngSel As Range
Dim RngTxtStart As Range, RngTxtEnd As Range
Dim StrOut As String, StrAddr As String
With Selection
  Set RngSel = .Range
  Do While .Characters.Last Like "[ " & Chr(160) & "]"
    .End = .End - 1
    If .Start = .End Then Exit Do
  Loop
  Do While .Characters.First.Previous Like "[ " & Chr(160) & "]"
    .Start = .Start - 1
    If .Start = .End Then Exit Do
  Loop
End With
With ActiveDocument
  Set RngStart = .Range(0, Selection.Start)
  Set RngEnd = .Range(0, Selection.End)
  Set RngTxtStart = .Range(Selection.Paragraphs.First.Range.Start, Selection.Start)
  Set RngTxtEnd = .Range(Selection.Paragraphs.Last.Range.Start, Selection.End)
  StrOut = "The Selection starts in paragraph " & RngStart.Paragraphs.Count & _
    " at word " & RngTxtStart.ComputeStatistics(wdStatisticWords)
  If RngTxtStart.Information(wdWithInTable) = True Then
    With Selection
      StrAddr = " and the Selected "
      If .Cells.Count = 1 Then
        StrAddr = StrAddr & "cell address is: "
      Else
        StrAddr = StrAddr & "cells span: "
      End If
      StrAddr = StrAddr & ColAddr(.Cells(1).ColumnIndex) & .Cells(1).RowIndex
      If .Cells.Count > 1 Then
        StrAddr = StrAddr & ":" & ColAddr(.Characters.Last.Cells(1).ColumnIndex) & _
          .Characters.Last.Cells(1).RowIndex
      End If
    End With
  End If
  StrOut = StrOut & vbCr & "The Selection also starts in table " & RngTxtEnd.Tables.Count & StrAddr
    
  StrOut = StrOut & vbCr & "The Selection ends in paragraph " & RngEnd.Paragraphs.Count & _
    " at word " & RngTxtEnd.ComputeStatistics(wdStatisticWords)
  If RngTxtEnd.Information(wdWithInTable) = True Then
    With Selection
      StrAddr = " and the Selected "
      If .Cells.Count = 1 Then
        StrAddr = StrAddr & "cell address is: "
      Else
        StrAddr = StrAddr & "cells span: "
      End If
      StrAddr = StrAddr & ColAddr(.Cells(1).ColumnIndex) & .Cells(1).RowIndex
      If .Cells.Count > 1 Then
        StrAddr = StrAddr & ":" & ColAddr(.Characters.Last.Cells(1).ColumnIndex) & _
          .Characters.Last.Cells(1).RowIndex
      End If
    End With
    StrOut = StrOut & vbCr & "The Selection also ends in table " & RngTxtEnd.Tables.Count & StrAddr
  End If
  RngSel.Select
  MsgBox StrOut
End With
End Sub

Function ColAddr(i As Long) As String
If i > 26 Then
  ColAddr = Chr(64 + Int(i / 26)) & Chr(64 + (i Mod 26))
Else
  ColAddr = Chr(64 + i)
End If
End Function

对于句子来说,这是不可能的,因为VBA不知道语法句子是什么。

例如,以下内容确定了文档正文中任意长度的选择的开始和结束位置,包括表索引和单元格地址(如果适用):

Sub Demo()
Dim RngStart As Range, RngEnd As Range, RngSel As Range
Dim RngTxtStart As Range, RngTxtEnd As Range
Dim StrOut As String, StrAddr As String
With Selection
  Set RngSel = .Range
  Do While .Characters.Last Like "[ " & Chr(160) & "]"
    .End = .End - 1
    If .Start = .End Then Exit Do
  Loop
  Do While .Characters.First.Previous Like "[ " & Chr(160) & "]"
    .Start = .Start - 1
    If .Start = .End Then Exit Do
  Loop
End With
With ActiveDocument
  Set RngStart = .Range(0, Selection.Start)
  Set RngEnd = .Range(0, Selection.End)
  Set RngTxtStart = .Range(Selection.Paragraphs.First.Range.Start, Selection.Start)
  Set RngTxtEnd = .Range(Selection.Paragraphs.Last.Range.Start, Selection.End)
  StrOut = "The Selection starts in paragraph " & RngStart.Paragraphs.Count & _
    " at word " & RngTxtStart.ComputeStatistics(wdStatisticWords)
  If RngTxtStart.Information(wdWithInTable) = True Then
    With Selection
      StrAddr = " and the Selected "
      If .Cells.Count = 1 Then
        StrAddr = StrAddr & "cell address is: "
      Else
        StrAddr = StrAddr & "cells span: "
      End If
      StrAddr = StrAddr & ColAddr(.Cells(1).ColumnIndex) & .Cells(1).RowIndex
      If .Cells.Count > 1 Then
        StrAddr = StrAddr & ":" & ColAddr(.Characters.Last.Cells(1).ColumnIndex) & _
          .Characters.Last.Cells(1).RowIndex
      End If
    End With
  End If
  StrOut = StrOut & vbCr & "The Selection also starts in table " & RngTxtEnd.Tables.Count & StrAddr
    
  StrOut = StrOut & vbCr & "The Selection ends in paragraph " & RngEnd.Paragraphs.Count & _
    " at word " & RngTxtEnd.ComputeStatistics(wdStatisticWords)
  If RngTxtEnd.Information(wdWithInTable) = True Then
    With Selection
      StrAddr = " and the Selected "
      If .Cells.Count = 1 Then
        StrAddr = StrAddr & "cell address is: "
      Else
        StrAddr = StrAddr & "cells span: "
      End If
      StrAddr = StrAddr & ColAddr(.Cells(1).ColumnIndex) & .Cells(1).RowIndex
      If .Cells.Count > 1 Then
        StrAddr = StrAddr & ":" & ColAddr(.Characters.Last.Cells(1).ColumnIndex) & _
          .Characters.Last.Cells(1).RowIndex
      End If
    End With
    StrOut = StrOut & vbCr & "The Selection also ends in table " & RngTxtEnd.Tables.Count & StrAddr
  End If
  RngSel.Select
  MsgBox StrOut
End With
End Sub

Function ColAddr(i As Long) As String
If i > 26 Then
  ColAddr = Chr(64 + Int(i / 26)) & Chr(64 + (i Mod 26))
Else
  ColAddr = Chr(64 + i)
End If
End Function
这对于句子来说是不可能的,因为VBA不知道什么是语法句子