![]() |
|
![]() |
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]()
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 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 The thing is that the find.execute moves the focus to the heading found. Is there a way to disable this focus change? Thanks! |
#2
|
||||
|
||||
![]()
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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
||||
|
||||
![]()
Therein lies your problem. There should be no need to select anything to add a hyperlink to a heading.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
![]()
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 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 HTML Code:
.Parent.Paragraphs(1).Range 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 from the following string array: HTML Code:
.GetCrossReferenceItems(wdRefTypeHeading) 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. |
![]() |
Tags |
find focus |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |
![]() |
lawrencehare | Word | 4 | 11-08-2012 01:08 PM |
![]() |
CabbageTree | Outlook | 2 | 05-14-2012 11:24 AM |
![]() |
cksm4 | Word VBA | 1 | 10-22-2011 11:36 PM |