Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-04-2023, 05:17 PM
Wigi Wigi is offline Get only endnotes of selected text Windows 10 Get only endnotes of selected text Office 2019
Novice
Get only endnotes of selected text
 
Join Date: Jun 2019
Posts: 7
Wigi is on a distinguished road
Default Get only endnotes of selected text

Hello all,

I would like to programmatically loop through the endnotes that are part of the selected text in a Word document.

I can do that, but the code gives me ALL endnotes in the document. It seems to ignore the fact that I only want the endnotes from what text is selected (text contains the superscript index numbers).

Can anyone help please ? Thanks a lot !

In the screenshot, I selected 2 lines of text, still I get all my 327 endnotes of the active document. I would expect only a couple of endnotes in the Immediate window.

HTML Code:
Sub forum()

    Dim e                     As Endnote

    For Each e In Selection.Range.Endnotes
        Debug.Print e.Index
    Next

End Sub

Attached Images
File Type: png endnotes.PNG (5.3 KB, 10 views)
Reply With Quote
  #2  
Old 01-04-2023, 09:42 PM
macropod's Avatar
macropod macropod is offline Get only endnotes of selected text Windows 10 Get only endnotes of selected text Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With Selection
  Set Rng = .Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = False
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = False
    .Text = "^e"
  End With
  Do While .Find.Execute = True
   If .InRange(Rng) = False Then Exit Do
   MsgBox .Endnotes(1).Index
   .Collapse wdCollapseEnd
  Loop
  Rng.Select
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-04-2023, 10:04 PM
BrianHoard BrianHoard is offline Get only endnotes of selected text Windows 10 Get only endnotes of selected text Office 2019
Advanced Beginner
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

I just created this as I see that Macropod already answered you. So here's my attempt in case it helps.
This method checks that each marker text is within your selection range, and if so, prints the endnote number.

Code:
Sub selectedEndnotes()
  ' Loop through selected endnotes.
  
  Dim eNotes As Endnotes
  Dim eNote As Endnote
  Dim rng_selected As Range
  Dim rng_marker As Range
  
  Set eNotes = ActiveDocument.Endnotes
  Set rng_selected = Selection.Range
  
  ' If nothing is selected, show a message
  If rng_selected.Start = rng_selected.End Then
    MsgBox "Please make a selection."
  End If
  
  Debug.Print ("The following endnotes are in selection:")
   
  For Each eNote In eNotes
    Set rng_marker = eNote.Reference.Characters.First ' Get marker range
    ' Only process endnotes whose marker .Start is between selection range.
    If rng_marker.Start >= rng_selected.Start And rng_marker.End <= rng_selected.End Then
      ' ---- Do whatever you're going to do to each endnote here. ---
      Debug.Print (eNote.Index)
    End If
  Next eNote

End Sub
Reply With Quote
  #4  
Old 01-05-2023, 12:53 AM
Wigi Wigi is offline Get only endnotes of selected text Windows 10 Get only endnotes of selected text Office 2019
Novice
Get only endnotes of selected text
 
Join Date: Jun 2019
Posts: 7
Wigi is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With Selection
  Set Rng = .Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = False
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = False
    .Text = "^e"
  End With
  Do While .Find.Execute = True
   If .InRange(Rng) = False Then Exit Do
   MsgBox .Endnotes(1).Index
   .Collapse wdCollapseEnd
  Loop
  Rng.Select
End With
Application.ScreenUpdating = True
End Sub
Hello,

Many thanks, this works perfectly !

Wim
Reply With Quote
  #5  
Old 01-05-2023, 12:57 AM
Wigi Wigi is offline Get only endnotes of selected text Windows 10 Get only endnotes of selected text Office 2019
Novice
Get only endnotes of selected text
 
Join Date: Jun 2019
Posts: 7
Wigi is on a distinguished road
Default

Quote:
Originally Posted by BrianHoard View Post
I just created this as I see that Macropod already answered you. So here's my attempt in case it helps.
This method checks that each marker text is within your selection range, and if so, prints the endnote number.

Code:
Sub selectedEndnotes()
  ' Loop through selected endnotes.
  
  Dim eNotes As Endnotes
  Dim eNote As Endnote
  Dim rng_selected As Range
  Dim rng_marker As Range
  
  Set eNotes = ActiveDocument.Endnotes
  Set rng_selected = Selection.Range
  
  ' If nothing is selected, show a message
  If rng_selected.Start = rng_selected.End Then
    MsgBox "Please make a selection."
  End If
  
  Debug.Print ("The following endnotes are in selection:")
   
  For Each eNote In eNotes
    Set rng_marker = eNote.Reference.Characters.First ' Get marker range
    ' Only process endnotes whose marker .Start is between selection range.
    If rng_marker.Start >= rng_selected.Start And rng_marker.End <= rng_selected.End Then
      ' ---- Do whatever you're going to do to each endnote here. ---
      Debug.Print (eNote.Index)
    End If
  Next eNote

End Sub
This also works 100%, many thanks !
Reply With Quote
  #6  
Old 01-05-2023, 12:59 AM
Wigi Wigi is offline Get only endnotes of selected text Windows 10 Get only endnotes of selected text Office 2019
Novice
Get only endnotes of selected text
 
Join Date: Jun 2019
Posts: 7
Wigi is on a distinguished road
Default

The one thing that the first code does, and the second doesn't (but it was not asked for but in testing I see it now):
the first code also gives the same result if the user makes selections in the endnotes texts. So if you select the superscript numbers in the endnotes themselves, the first code gives the numbers. The second code gives all endnotes numbers.

But this is a side effect, not sure if the user will ever do that.

Have a nice day !

Wim
Reply With Quote
  #7  
Old 01-05-2023, 01:09 AM
Wigi Wigi is offline Get only endnotes of selected text Windows 10 Get only endnotes of selected text Office 2019
Novice
Get only endnotes of selected text
 
Join Date: Jun 2019
Posts: 7
Wigi is on a distinguished road
Default

Here is my final code.

The user selects text in the Word document.
ALL hyperlinks within the text of ALL endnotes that are encountered, are opened through a Chrome browser.

Thanks for the support.


Code:
Sub Demo()

    'https://www.msofficeforums.com/word-vba/50191-get-only-endnotes-selected-text.html

    Dim Rng                   As Range
    Dim hl                    As Hyperlink

    Const chromeFileLocation  As String = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

    Application.ScreenUpdating = False

    With Selection
        Set Rng = .Range

        ' If nothing is selected, show a message
        If .Start = .End Then
            MsgBox "Please make a selection containing 1 or more endnotes."
            Application.ScreenUpdating = True
            Exit Sub
        End If

        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Format = False
            .Forward = True
            .Wrap = wdFindStop
            .MatchWildcards = False
            .Text = "^e"
        End With
        Do While .Find.Execute
            If Not .InRange(Rng) Then Exit Do

            For Each hl In .Endnotes(1).Range.Hyperlinks
                'Debug.Print e.Index & ": " & hl.Address
                Shell (Chr(34) & chromeFileLocation & Chr(34) & "-url " & hl.Address)
            Next
            .Collapse wdCollapseEnd
        Loop
        
        Rng.Select
        
    End With
    
    Application.ScreenUpdating = True
    
End Sub
Reply With Quote
Reply

Tags
endnote reference



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get only endnotes of selected text Footnote formatting>"Apply changes to selected text" not limiting changes to selected text Swarup Word 11 07-26-2022 01:51 PM
Get only endnotes of selected text Find and Replace Selected Text or Macro for finding selected text mrplastic Word VBA 4 12-20-2019 01:25 PM
Powerpoint-2019 Text in selected theme remains in All Caps even when small caps option is selected Tanasha4 PowerPoint 2 04-06-2019 07:53 PM
Get only endnotes of selected text How to convert endnotes in a text doc to Word endnotes? Dickison Word VBA 4 10-06-2012 09:11 PM
Get only endnotes of selected text Separating endnotes from text kenglade Word 22 09-14-2012 04:10 PM

Other Forums: Access Forums

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


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