[英]Reliably identifying locations in Word document for use in VBA code
In Word I am endlessly running the below with incrementing numbers to find parts of document that I need to modify with VBA:在 Word 中,我不断地运行下面的递增数字来查找需要使用 VBA 修改的文档部分:
MsgBox (ActiveDocument.Paragraphs(56).Range.Text)
Also for tables.也适用于桌子。 This is very slow and awkward;
这是非常缓慢和尴尬的; is there a way on the document to identify VBA 'locations' at the cursor is at?
文档上有没有办法在 cursor 处识别 VBA 的“位置”?
For example if I highlight a word it would tell me "this is paragraph x, sentance y, word z" etc?例如,如果我突出显示一个单词,它会告诉我“这是第 x 段,句子 y,单词 z”等?
To get the paragraph number based on the selection you have to use a little jiggery pokery with the range object要根据选择获得段落编号,您必须使用范围为 object 的小技巧
Function GetParagraphNumber(ByVal ipRange As Word.Range) As Long
' NB The ByVal is critical
ipRange.Start = 0
GetParagraphNumber =ipRange.Paragraphs.Count + 1
End Function
SImilar functions can be derived for Words and Characters.可以为单词和字符派生类似的函数。
For example, the following identifies where a selection of any length in the document body begins & ends, including, if applicable, table indices and cell addresses:例如,以下标识了文档正文中任何长度的选择开始和结束的位置,包括(如果适用)表索引和单元格地址:
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
This isn't really possible for sentences, as VBA has no idea what a grammatical sentence is.这对于句子来说是不可能的,因为 VBA 不知道什么是语法句子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.