需要在MS Word中捕获当前滚动位置(而不是光标位置) [英] Need to capture current scroll position (not cursor position) in MS Word

查看:130
本文介绍了需要在MS Word中捕获当前滚动位置(而不是光标位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to get the current scroll position (I think that is the right term; i.e., where is my virtual lens pointed at the document, not where my cursor is) in a Word document, so that when I run a macro I can return to that position at the end.

我编写了一个宏,该宏从当前光标位置向后搜索专利申请部件号,将候选的下一个可用部件号确定为前一个max +1,然后搜索出现在光标下方的数字排序列表,以查看是否候选零件编号与下一个使用的编号相冲突,如果确实如此,则将其添加到候选零件上,然后再次检查碰撞,直到找到最小的未使用整数为止,以尽可能地遵循在体内引入零件的约定描述按零件号顺序排列.

I wrote a macro that searches backward from the current cursor position for patent application part numbers, determines a candidate next available part number as the previous max + 1, then searches a sorted list of numbers appearing below the cursor to see if the candidate part number collides with the next used number, and if it does, it adds 1 to the candidate and checks for a collision again, until it finds the smallest unused integer, to follow as closely as possible the convention of introducing parts in the body of the description sequentially by part number.

然后将其插入当前光标位置.它工作正常;但是,它将当前行移动到查看窗口的顶部,这令人迷惑.我宁愿将滚动条保持在原位.这是我的代码:

Then it inserts that number at the current cursor position. It works fine; however, it moves the current line to the top of the viewing window, which is disorienting. I would rather keep the scroll where it is. Here is my code:

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位数字对于零件号来说有点过头,但是出于我的问题,我们不需要完整的正则表达式

' 2-3 digit numbers are a bit over-inclusive for a part number, but for purposes of my question we don't need the full regular expression

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

推荐答案

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

To capture the window scroll position use:

scrollPosition = ActiveWindow.ActivePane.VerticalPercentScrolled

要将其设置回原始用途:

To set it back to the original use:

ActiveWindow.ActivePane.VerticalPercentScrolled = scrollPosition

这篇关于需要在MS Word中捕获当前滚动位置(而不是光标位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆