Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-10-2022, 10:58 PM
WJSwanepoel WJSwanepoel is offline Place Hyperlink text inline Windows 10 Place Hyperlink text inline Office 2021
Advanced Beginner
Place Hyperlink text inline
 
Join Date: Dec 2019
Location: Krugersdorp, South Africa
Posts: 63
WJSwanepoel is on a distinguished road
Question Place Hyperlink text inline

I have a rather difficult (maybe not for experts!) request.



I have a large MS Word document (Office 365) which contains links to bookmarks. What I would like to do is to place the paragraph at the bookmark in-line with the text in square brackets.

I attach a small example.

I would appreciate any help I can get. My VBA skills are rather skimpy.
Attached Files
File Type: docx EXAMPLE OF HYPERLINKED TEXT.docx (12.9 KB, 10 views)
Reply With Quote
  #2  
Old 11-12-2022, 07:16 PM
BrianHoard BrianHoard is offline Place Hyperlink text inline Windows 10 Place Hyperlink text inline Office 2019
Advanced Beginner
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

Hi, this was a fun one.
I'm still learning VBA, so this could very likely be improved upon, but in my test, it resulted in exactly what you listed as your end goal.

Some areas that might need changed based on your documents:
  • This script is written specifically for your test document. It would likely have problems in you have hyperlinks that are not bookmarks in your doc.
  • I'm not doing any error checking to ensure you have some bookmarks to start with.
  • This is a first pass at the problem, so very likely areas could be optimized and improved.
Code:
Sub bhh_inlineBookmarks()
  ' Written by Brian Hoard, www.BrianHoard.com

  Dim scriptName As String
  scriptName = "bhh_inlineBookmarks"
  
  Application.ScreenUpdating = False
    
  ' Begin undo record
  Dim bhhUndo As UndoRecord
  Set bhhUndo = Application.UndoRecord
  bhhUndo.StartCustomRecord (scriptName)

  Dim f As Field
  Dim rngSource As Range
  Dim rng_bm As Range
  Dim i As Integer
  
  i = 1 ' Begin hyperlink counter
  For Each f In ActiveDocument.Fields
    If f.Type = wdFieldHyperlink Then
      f.Select
      Set rngSource = Selection.Range
      f.Delete ' Delete the link
      
      ' Define the range in the document body where the bookmark is at.
      With rngSource
        .Collapse
        .Font.Superscript = False
      End With
      
      ' Process the linked text.
      ' NOTE: This is subject to problems if the document includes hyperlinks that are not bookmarks.
      Set rng_bm = ActiveDocument.Bookmarks.Item(i).Range

      With rng_bm
        .Expand wdParagraph ' Expand the range to the full paragraph
        .End = .End - 1 ' Bring range End to the left 1 character to exclude the carriage return.
      End With

      rngSource.Text = (" [" & rng_bm.Text & "]")

    End If ' /f.Type = wdFieldHyperlink

    i = (i + 1) ' increment bookmark counter

  Next f

  ' End undo
  bhhUndo.EndCustomRecord
  
  Application.ScreenUpdating = True

End Sub
Reply With Quote
  #3  
Old 11-12-2022, 09:46 PM
WJSwanepoel WJSwanepoel is offline Place Hyperlink text inline Windows 10 Place Hyperlink text inline Office 2021
Advanced Beginner
Place Hyperlink text inline
 
Join Date: Dec 2019
Location: Krugersdorp, South Africa
Posts: 63
WJSwanepoel is on a distinguished road
Default

Thank you very much. I will test it on my large document.
Reply With Quote
  #4  
Old 11-13-2022, 04:51 AM
Italophile Italophile is online now Place Hyperlink text inline Windows 11 Place Hyperlink text inline Office 2021
Expert
 
Join Date: Mar 2022
Posts: 315
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

The following code will process the hyperlinks and the referenced bookmarked locations in the same document. The hyperlinks are replaced with the text from the bookmark and will retain the formatting of that text.

Code:
Sub MoveHyperlinkedText()
    Dim hlIdx As Long, hlink As Hyperlink, hlRng As Range
    Dim bmName As String, bmRng As Range
    
    If ActiveDocument.Hyperlinks.Count > 0 And ActiveDocument.Bookmarks.Count > 0 Then
        Application.ScreenUpdating = False
        'because hyperlinks will be deleted it is necessary to process them in reverse order
        For hlIdx = ActiveDocument.Hyperlinks.Count To 1 Step -1
            Set hlink = ActiveDocument.Hyperlinks(hlIdx)
            If hlink.Type = msoHyperlinkRange Then  'ignore any hyperlinked shapes
                If hlink.Address = vbNullString Then
                    'it's a link to a location in the same document so ensure that it exists
                    bmName = hlink.SubAddress
                    If ActiveDocument.Bookmarks.Exists(bmName) Then
                        'get range of bookmarked paragraph, minus the paragraph mark
                        Set bmRng = ActiveDocument.Bookmarks(bmName).Range
                        With bmRng
                            .Expand wdParagraph
                            .End = .End - 1
                        End With
                        Set hlRng = hlink.Range
                        hlink.Delete
                        With hlRng
                            'turn off superscript and replace text with brackets
                            .Font.Superscript = False
                            .Text = " []"
                            .Start = .Start + 2
                            .End = .End - 1
                            'add the bookmarked text and its formatting
                            .FormattedText = bmRng.FormattedText
                        End With
                    End If
                End If
            End If
        Next hlIdx
        Application.ScreenUpdating = True
    End If

End Sub
Reply With Quote
  #5  
Old 11-16-2022, 03:27 AM
WJSwanepoel WJSwanepoel is offline Place Hyperlink text inline Windows 10 Place Hyperlink text inline Office 2021
Advanced Beginner
Place Hyperlink text inline
 
Join Date: Dec 2019
Location: Krugersdorp, South Africa
Posts: 63
WJSwanepoel is on a distinguished road
Default Thank you!

Thank you very much for this. It does exactly what I need.
Reply With Quote
  #6  
Old 11-24-2022, 09:34 PM
WJSwanepoel WJSwanepoel is offline Place Hyperlink text inline Windows 10 Place Hyperlink text inline Office 2021
Advanced Beginner
Place Hyperlink text inline
 
Join Date: Dec 2019
Location: Krugersdorp, South Africa
Posts: 63
WJSwanepoel is on a distinguished road
Default

Good day Italophile,
Why would I get "Command failed" on the statement:
If hlink.Address = vbNullString Then

Regards,
WS
Reply With Quote
Reply

Tags
hyperlink text

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hyperlink to specific place in PDF michal.berenc Visio 1 11-23-2017 08:55 AM
Creating A Keyboard Shortcut In Place of Using Mouse To Select Hyperlink Object PowerPointerNeedsHelp PowerPoint 3 07-06-2017 01:47 AM
Place Hyperlink text inline How to put a table inline with the text Subterfuge Word Tables 2 09-22-2016 08:01 AM
Place Hyperlink text inline Hyperlink to Place in Document Issue atelem Word 9 06-22-2016 03:31 PM
Place Hyperlink text inline editing a number/text at one place and changes taking place wherever it appears anurag.butoliya Word 1 06-14-2014 06:27 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:07 PM.


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