Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-05-2024, 08:16 AM
Chain Chain is offline Remove the hyperlinks on paragraph marks Windows 11 Remove the hyperlinks on paragraph marks Office 2021
Novice
 
Join Date: Nov 2024
Posts: 2
Chain is on a distinguished road
Post

I need to remove the hyperlinks from paragraph marks (as this causes us some issues).


Sometimes a hyperlink at the end of a paragraph might extend to include the paragraph mark, but it could also be that a single hyperlink is applied across multiple paragraphs (rare, but the code needs to account for this).

I can't really code, so have attempted to use Copilot for this (with manual troubleshooting), but after a lot of trial-and-error, I keep running into the same issues.

This is the current version:

Code:
Sub ReapplyHyperlinksWithoutRemovingText1()
    Dim doc As Document
    Dim hl As hyperlink
    Dim hlRange As Range
    Dim hlText As String
    Dim startPos As Long
    Dim endPos As Long
    Dim i As Long
    Dim hlAddress As String
    Dim hlSubAddress As String
    Dim partRange As Range
    
    Set doc = ActiveDocument
    
    ' Loop through each hyperlink in the document
    For Each hl In doc.Hyperlinks
        Set hlRange = hl.Range
        hlText = hlRange.Text
        startPos = hlRange.Start
        endPos = hlRange.End
        hlAddress = hl.Address
        hlSubAddress = hl.SubAddress
        
        ' Check if the hyperlink text contains a paragraph break
        If InStr(hlText, vbCr) > 0 Or InStr(hlText, vbLf) > 0 Then
            ' Remove the hyperlink
            hl.Delete
            
            ' Split the text by paragraph breaks
            Dim parts() As String
            parts = Split(hlText, vbCr)
            
            ' Reapply hyperlink to the first part only
            If parts(0) <> "" Then
                Set partRange = doc.Range(startPos, startPos + Len(parts(0)))
                partRange.Hyperlinks.Add _
                    Anchor:=partRange, _
                    Address:=hlAddress, _
                    SubAddress:=hlSubAddress, _
                    TextToDisplay:=parts(0)
            End If
        End If
    Next hl
End Sub
It splits such hyperlink texts into multiple strings (excluding the paragraph marks) and reapplies the hyperlink to each part (first removing the original hyperlink). After reapplying the hyperlink to the first part of a multi-paragraph link, it should update the startPos to be at the start of the next paragraph/part, but the code for this (startPos = partRange.End + 1) seemingly ended up inside the field code of the added hyperlink, throwing everything off. So that's why the macro now just fixes the first string in parts(). This is accecptable if there's no easy solution here (since actual multi-paragraph links are super rare).

Next, this code only handles this in the body text of the document, but I need it to also do this for footnotes. This I was also unable to get working. It was essentially the exact code, but wrapped inside "For Each footnote In doc.Footnotes; For Each hl In footnote.Range.Hyperlinks" - and it did find and remove the hyperlink in a footnote, but it then reapplied the hyperlink to the first link in the body text and not the footnote. No idea why.



Anyone able to help with at least getting this to run on footnots? It would be super helpful!
Attached Files
File Type: docx Hyperlinks test document.docx (33.2 KB, 3 views)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove the hyperlinks on paragraph marks Remove paragraph marks in Excel from Word-sourced text Peterson Excel 6 08-18-2020 11:36 PM
Help me, remove the marks occur at begin and last paragraph ngocanhwdn Word 6 06-25-2019 07:57 AM
Remove the hyperlinks on paragraph marks Paragraph marks appear suddently leonardevens Word 1 07-13-2015 12:14 PM
Finding Paragraph Marks BZee Word 4 02-14-2015 02:12 PM
Remove the hyperlinks on paragraph marks Hide paragraph format marks Cara Word 2 04-07-2011 10:26 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:27 AM.


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