Microsoft Office Forums How to avoid find.execute moving focus and restore exact view after search

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-30-2016, 12:31 AM
diracsbracket diracsbracket is offline How to avoid find.execute moving focus and restore exact view after search Windows 7 64bit How to avoid find.execute moving focus and restore exact view after search Office 2010 64bit
Novice
How to avoid find.execute moving focus and restore exact view after search
 
Join Date: Aug 2014
Posts: 3
diracsbracket is on a distinguished road
Default How to avoid find.execute moving focus and restore exact view after search

Hi,
I have a sub that inserts a hyperlink to the heading matching the selected text in MS Word 2013.
The header's range is found using:

HTML Code:
Selection.Find.Execute
Before doing that, I save the current range (i.e. that of the selected text) in order to restore it after the search has found the heading's range.

Although the range is restored correctly (and I'm back at the selected text), usually the view of the page is shifted, as if it was scrolled up or down a bit.

Since these shifts are sometimes large, I find them annoying enough to want to avoid this.



Is that possible?
I tried this by saving/restoring various built-in MS Word bookmark ranges, for example:

HTML Code:
ActiveDocument.Bookmarks("\page").Range
But this still causes shifts....

The thing is that the find.execute moves the focus to the heading found. Is there a way to disable this focus change?

Thanks!
Reply With Quote
  #2  
Old 10-30-2016, 01:46 AM
gmayor's Avatar
gmayor gmayor is offline How to avoid find.execute moving focus and restore exact view after search Windows 10 How to avoid find.execute moving focus and restore exact view after search Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,899
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

This is one of the issues of using Selection as opposed to Ranges. For a selection to be a selection you have to actually select it and that causes things to move around.

Assuming your heading texts are unique, the following macro will locate the heading text of the heading before the selected text, bookmark it with a name that reflects its content and apply a hyperlink to the selected text to that heading bookmark, without moving anything:

Code:
Sub Macro1()
'Graham Mayor - http://www.gmayor.com - 30/10/2016 
Dim oHeading As Range
Dim oRng As Range
Dim strHeading As String
    Set oRng = Selection.Range
    Set oHeading = ActiveDocument.Range

    oHeading.End = oRng.Start
    With oHeading.Find
        .Style = "Heading 2" 'The style of the heading text
        Do While .Execute(Forward:=False)
            oHeading.End = oHeading.End - 1
            strHeading = oHeading.Text
            strHeading = Replace(strHeading, Chr(32), Chr(95))
            strHeading = "bm" & strHeading
            oHeading.Bookmarks.Add strHeading
            Exit Do
        Loop
    End With
    ActiveDocument.Hyperlinks.Add _
            Anchor:=oRng, _
            Address:="", _
            SubAddress:=strHeading, _
            ScreenTip:="", _
            TextToDisplay:=oRng.Text
lbl_Exit:
    Set oRng = Nothing
    Set oHeading = Nothing
    Exit Sub
End Sub
The problems will start if that bookmark name clashes with a bookmark in your document, so it might be better to start by bookmarking all your headings with unique bookmark names and instead of deriving a bookmark name as above you use the name already associated with the heading in question.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 10-30-2016, 03:10 AM
macropod's Avatar
macropod macropod is offline How to avoid find.execute moving focus and restore exact view after search Windows 7 64bit How to avoid find.execute moving focus and restore exact view after search Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,536
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by diracsbracket View Post
Hi,
I have a sub that inserts a hyperlink to the heading matching the selected text in MS Word 2013.
The header's range is found using:

HTML Code:
Selection.Find.Execute
Therein lies your problem. There should be no need to select anything to add a hyperlink to a heading.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #4  
Old 10-30-2016, 03:17 AM
diracsbracket diracsbracket is offline How to avoid find.execute moving focus and restore exact view after search Windows 7 64bit How to avoid find.execute moving focus and restore exact view after search Office 2013
Novice
How to avoid find.execute moving focus and restore exact view after search
 
Join Date: Aug 2014
Posts: 3
diracsbracket is on a distinguished road
Default

Graham, Paul,
Thanks for reacting!

I knew that I should have used ranges, but before I wrote my message I was using this:

HTML Code:
With Selection.Find
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    .ClearHitHighlight
    .ClearAllFuzzyOptions
    .Wrap = wdFindContinue ' needed because we don't start from first page
    .Style = "Heading " & CStr(level)
    
    Do While .Execute(FindText:=formStr)
        If Selection.Paragraphs(1).Range.ListFormat.ListString = headingNumber Then
            Set myRange = Selection.Paragraphs(1).Range.Duplicate
            headingFound = True
            Exit Do
        End If
        
        DoEvents
     Loop
End With
This works, but as expected, using selection instead of ranges, the page usually ends up somewhat shifted,
after restoring the original range of the selected text.

I tried using a range instead, but then the following didn't work as desired:
HTML Code:
myRange.Paragraphs(1).Range.ListFormat.ListString
That was until I realized (thanks to you) that I should use .Parent when using ranges:

HTML Code:
.Parent.Paragraphs(1).Range
So the following works now, without the page shifting:

HTML Code:
With ActiveDocument.Range.Find
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    .ClearHitHighlight
    .ClearAllFuzzyOptions
    .Wrap = wdFindContinue ' needed because we don't start from first page
    .Style = "Heading " & CStr(level)
    
    Do While .Execute(FindText:=formStr)
        Set myRange = .Parent.Paragraphs(1).Range
        
        If myRange.ListFormat.ListString = headingNumber Then
            headingFound = True
            Exit Do
        End If
        
        DoEvents
     Loop
End With
In the above, I use the heading level and heading number which were extracted from the string obtained
from the following string array:
HTML Code:
.GetCrossReferenceItems(wdRefTypeHeading)
This list provides the full header, inclusive the heading number, as single strings per header.

I search this list for all headings with text that contain my highlighted text, and created a Form that pops up when there is more than 1 match.

If there is only 1 header with the exact same text as my selection, then it is automatically selected (no Form shows), and I perform the heading search for the bookmark insertion using that heading number as reference instead of using the header text.

Both methods above work, but although it doesn't move the page, the 2nd method with the range is very slightly but noticeably slower than the selection method (I have a large document of about 700 pages here).

Thanks again for forcing me to think again about using ranges!

p.s. I do the bookmark creation an hyperlink insertion after that. If an existing bookmark exists, I first check its range, to see if it corresponds to that of the header just found. If a bookmark exists, but has different range, then I create a new bookmark by adding a simple index (1,2,...) to the bookmark name.
Reply With Quote
Reply

Tags
find focus

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to avoid find.execute moving focus and restore exact view after search How to find exact matches only in an excel spreadsheet zhead Excel 6 09-09-2016 07:52 AM
Outlook restore from backup didn't seem to find the Calendar. BudVitoff Outlook 0 09-08-2015 01:04 PM
How to avoid find.execute moving focus and restore exact view after search Moving focus to format style box from keyboard lawrencehare Word 4 11-08-2012 01:08 PM
How to avoid find.execute moving focus and restore exact view after search How to find exact phrase CabbageTree Outlook 2 05-14-2012 11:24 AM
How to avoid find.execute moving focus and restore exact view after search Find and Execute cksm4 Word VBA 1 10-22-2011 11:36 PM


All times are GMT -7. The time now is 11:16 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft