gpt4 book ai didi

vba - 需要在 MS Word 中捕获当前滚动位置(不是光标位置)

转载 作者:行者123 更新时间:2023-12-05 01:20:38 25 4
gpt4 key购买 nike

我需要在 Word 文档中获取当前滚动位置(我认为这是正确的术语;即我的虚拟镜头指向文档的位置,而不是我的光标所在的位置),以便在我运行宏时最后我可以回到那个位置。

我编写了一个宏,从当前光标位置向后搜索专利申请零件号,确定候选的下一个可用零件号为前一个最大值 + 1,然后搜索出现在光标下方的数字排序列表,看看是否候选零件编号与下一个使用的编号发生冲突,如果发生冲突,则将候选零件编号加 1 并再次检查是否发生碰撞,直到找到最小的未使用整数,以尽可能遵循在正文中引入零件的约定按零件号顺序描述的。

然后它将那个数字插入到当前光标位置。它工作正常;但是,它会将当前行移动到查看窗口的顶部,这会让人迷失方向。我宁愿把卷轴留在原处。这是我的代码:

Sub InsertLocalNextPartNum()

Application.ScreenUpdating = False

Dim re As VBScript_RegExp_55.RegExp
Set re = New VBScript_RegExp_55.RegExp


re.pattern = "\b(\d{2,3}\b)"

' 2-3 位数字对于零件号来说有点过于包容,但出于我的问题的目的,我们不需要完整的正则表达式

re.IgnoreCase = False
re.Global = True

Dim txt As String
Dim allLongMatches As MatchCollection, m As Match
Dim nums() As Long
Dim numsColl As New Collection
Dim maxNum As Long
maxNum = 0
Dim nextPartNum As String
Dim localNextPartNum As String
localNextPartNum = 0
Dim i As Long
Dim j As Long
Dim k As Long


Selection.HomeKey Unit:=wdStory, Extend:=wdExtend

txt = Selection.Range.text

If re.Test(txt) Then
Set allLongMatches = re.Execute(txt)
For Each m In allLongMatches

numsColl.Add (m.Value)

Next m
End If
ReDim nums(1 To numsColl.Count)

For i = 1 To numsColl.Count
nums(i) = numsColl(i)
If nums(i) > maxNum Then maxNum = nums(i)
Next i

localNextPartNum = maxNum + 1


Selection.MoveRight
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
txt = Selection.text

If re.Test(txt) Then
Set allLongMatches = re.Execute(txt)
For Each m In allLongMatches
numsColl.Add (m.Value)
Next m
End If

ReDim nums(1 To numsColl.Count)
Dim numTemp As String
Dim lngMin As Long
Dim lngMax As Long
lngMin = LBound(nums())
lngMax = UBound(nums())

For i = 1 To numsColl.Count
nums(i) = numsColl(i)
Next i

For j = lngMin To lngMax - 1
For k = j + 1 To lngMax
If nums(j) > nums(k) Then
numTemp = nums(j)
nums(j) = nums(k)
nums(k) = numTemp
End If
Next k
Next j

For i = 1 To numsColl.Count
If localNextPartNum < nums(i) Then Exit For
' Debug.Print nums(i)
If localNextPartNum = nums(i) Then localNextPartNum = nums(i) + 1
Next i
Selection.MoveLeft
Selection.InsertAfter (localNextPartNum & " ")
Selection.MoveRight

End Sub

最佳答案

要捕获窗口滚动位置,请使用:

scrollPosition = ActiveWindow.ActivePane.VerticalPercentScrolled

将其设置回原来的用途:

ActiveWindow.ActivePane.VerticalPercentScrolled = scrollPosition

关于vba - 需要在 MS Word 中捕获当前滚动位置(不是光标位置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30765458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com