PDA

View Full Version : Finding the next specific coloured highlight in end & footnotes



johndavidson
09-17-2013, 09:22 AM
I'm trying to modify an existing macro that finds the next instance of highlighted text of a particular colour. It works fine in the main text story, but I don't know how to make it work when I run it in the end or footnotes pane in Normal view. It's probably a question of setting the range to the endnotes story, but how? Any solutions would be much appreciated. The code is:

Sub HighlightNextYellow()
' Find next yellow highlight
Call HighlightColour(wdYellow)
End Sub


Private Sub HighlightColour(hColour As String)
' Find next highlighted string and leave selected


Dim r As Range
Dim currDoc As Word.Document

Set currDoc = Application.ActiveDocument
Set r = currDoc.Range

' This will set the search start position
' to the cursor position or to the end of any selected text
Selection.Start = Selection.End
r.Start = Selection.Start

With r.Find
.Highlight = True
Do While .Execute(FindText:="", Forward:=True) = True
If r.HighlightColorIndex = hColour Then
r.Select
'Selection.Start = Selection.End ' Uncomment if you want the text to be unselected
Exit Do
End If
Loop
End With
End Sub

Thanks

John Davidson

gmaxey
09-17-2013, 11:32 AM
I never use normal (draft view) but you might be able to adapt the following:


Sub HighlightNextYellow()
HighlightColour wdYellow
End Sub

Private Sub HighlightColour(lngColor As Long)
Dim oDoc As Word.Document
Dim oRng As Range
Set oDoc = ActiveDocument
If Not Selection.Range.InRange(oDoc.StoryRanges(wdFootnotesStory)) Then
oDoc.StoryRanges(wdFootnotesStory).Select
Selection.Collapse wdCollapseStart
End If
Set oRng = oDoc.StoryRanges(wdFootnotesStory)
oRng.Start = Selection.Range.End
With oRng.Find
.Highlight = True
If .Execute(FindText:="", Forward:=True) = True Then
If oRng.HighlightColorIndex = lngColor Then oRng.Select
End If
End With
lbl_Exit:
Exit Sub
End Sub

johndavidson
09-17-2013, 02:10 PM
Thanks - Based on your code, I've got something working, tho' my code is a bit repetitive and kludgy. What is the code to determine the current active story type and save it in a variable that can then be used to select that story as a range? Then the routine can simply search in whatever story is currently selected. Something like - ?

Dim currStory As WdStoryType

' Set currStory = current active story type

Set oRng = oDoc.StoryRanges(currentStory)

' Now do the search as per your routine

Thanks

John D.

gmaxey
09-17-2013, 02:36 PM
Dim oSR As Range
Set oSR = ActiveDocument.StoryRanges(Selection.StoryType)

johndavidson
09-17-2013, 02:59 PM
Thanks again. So in case it is of use to anyone out there, here's the code to find the next occurrence of a specified highlight colour in the current active story. It searches all highlights for the next occurence (if any) of the specified colour.

Sub HighlightNextYellow()
' Find next yellow highlight
Call HighlightColour(wdYellow)
End Sub


Private Sub HighlightColour(lngColor As Long)
Dim oDoc As Word.Document
Dim oRng As Range


Set oRng = ActiveDocument.StoryRanges(Selection.StoryType)
oRng.Start = Selection.Range.End
With oRng.Find
.Highlight = True
Do While .Execute(FindText:="", Forward:=True) = True
If oRng.HighlightColorIndex = lngColor Then
oRng.Select
'Selection.Start = Selection.End ' Uncomment if you want the text to be unselected
Exit Do
End If
Loop
End With
End Sub

Rgds

John D.

gmaxey
09-17-2013, 04:46 PM
While I suppose it doesn't' hurt, you don't need the Do .... Loop


If .Execute(FindText:="", Forward:=True) = True Then
If oRng.HighlightColorIndex = lngColor Then
oRng.Select
'Selection.Start = Selection.End ' Uncomment if you want the text to be unselected
End If
End If

fumei
09-17-2013, 07:59 PM
I am curious as to why you need to have the Active StoryRange be the endnotes story - that is, Selection is in THAT story. You can action in the Endnotes story without even having anything at all to do with Selection.

johndavidson
09-17-2013, 08:54 PM
Greg - Trouble with that approach is that if there are highlights of other colours before the first specified colour then the insertion point stays where it is and the routine exits. Trouble with the existing method is that it has to find every highlight and then check its colour, which is noticeable in a long doc with lots of highlights. Is there a way of searching only for the specified colour? Something like .Highlight.HighlightColorIndex(lngColor) = True if such syntax were valid.

johndavidson
09-17-2013, 08:56 PM
Fumei - I started off with that approach, but my code must have been defective because it always switched back to main text story when I set the range. Hence the need to determine and define the current story.