我有一个Word宏,可以在整个文档中找到所选文本,突出显示所有匹配项,然后返回起点(通过它首先设置的书签)。唯一的问题是,在运行它之后,Word会在书签上方显示页面(光标所在的位置)。光标根本不在屏幕上。
运行后,我可以点击一个键,屏幕会移动以显示入口点。我添加了一行来在宏中做同样的事情(向右移动一个字符),它仍然在我上面的页面上。我尝试在窗口上使用SmallScroll,但这会移动插入点。我需要在我开始的地方结束。
我该怎么办?
很抱歉之前没有发布代码,我无法将其格式化为代码。 (我在联邦网络上责备IE8。)
Sub AcronymHilighter()
''''''''''''''''''''''''''''''''''''''''
' Check that user has selected some text
''''''''''''''''''''''''''''''''''''''''
Dim strGetAcronym As String
If Not Selection.Type = wdSelectionNormal Then
MsgBox "Please select some text.", vbInformation
Exit Sub
End If
'''''''''''''''''''
' Set a bookmark at the current location
'''''''''''''''''''
ActiveDocument.Bookmarks.Add _
Name:="MarkReturn", Range:=Selection.Range
'''''''''''''''''''
' Save the current track changes state, then turn it off
'''''''''''''''''''
Dim bTrackingAsWas As Boolean
bTrackingAsWas = ActiveDocument.TrackRevisions
ActiveDocument.TrackRevisions = False
ActiveDocument.ShowRevisions = False
''''''''''''''''''
' Find & highlight the text
''''''''''''''''''
strGetAcronym = Selection.Text
Selection.Range.HighlightColorIndex = wdYellow
Selection.Collapse wdCollapseEnd
With Selection.Find
.Text = strGetAcronym
.Highlight = False
.Wrap = wdFindContinue
.MatchCase = True
Do While .Execute
Selection.Range.HighlightColorIndex = wdYellow
Loop
End With
'''''''''''''''''''''''''''''
' Return to the starting point,
' restore Track Changes to the previous state
'''''''''''''''''''''''''''''
Selection.GoTo what:=wdGoToBookmark, Name:="MarkReturn"
ActiveWindow.SmallScroll Down:=2
ActiveDocument.TrackRevisions = bTrackingAsWas
ActiveDocument.ShowRevisions = True
End Sub
请原谅我的过度评论,我想提醒自己一切都在做什么;我最后一次编写代码时,它是在1985年的BASIC中。
答案 0 :(得分:0)
我不完全清楚你的问题(因为你没有发布你的代码),但这一点完成你所描述的工作:
Sub MarkAndGoBack()
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Bookmark"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Dim doc As Document
Dim para As Paragraph
Set doc = ActiveDocument
For Each aWord In doc.Words
If aWord.Text = "WantedWord" Then aWord.HighlightColorIndex = wdYellow
Next aWord
Selection.GoTo What:=wdGoToBookmark, Name:="Bookmark"
ActiveDocument.Bookmarks("Bookmark").Delete
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Sub
它搜索单词“WantedWord”并将其突出显示为黄色。之后,您将转到最初创建的书签。它为我显示了带光标的正确页面。如果您想进行输入搜索,则必须将“WantedWord”替换为InputBox
。