Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-16-2020, 01:43 PM
scientist101 scientist101 is offline Word macro to cut a range of text and apply to subsequent text as a hyperlink Windows 10 Word macro to cut a range of text and apply to subsequent text as a hyperlink Office 2019
Novice
Word macro to cut a range of text and apply to subsequent text as a hyperlink
 
Join Date: Jul 2020
Posts: 6
scientist101 is on a distinguished road
Default Word macro to cut a range of text and apply to subsequent text as a hyperlink

Hi. I have an EndNote bibliography in my Word file that I would like to link to specific pdf files that are located on my computer. When I format my bibliography I can make it display the references as follows. I specifically set-up the software to include the "<<" and ">>" marks.

1. <<file://C:\reference 1 pdf location>><<Reference 1 description>>
2. <<file://C:\reference 2 pdf location>><<Reference 2 description>>

Is there a way to build a macro that cuts and pastes that reference location and hyperlinks it to the subsequent text? Maybe something that goes in this order?

First

1. <<file://C:\reference 1 pdf location>><<Reference 1 description>>
2. <<file://C:\reference 2 pdf location>><<Reference 2 description>>

Then (with references now hyperlinked)

1. <<Reference 1 description>>
2. <<Reference 2 description>>


Final (final action to find and delete "<<" and ">>" characters)

1. Reference 1 description
2. Reference 2 description


I'm not sure if this is possible, but any help would be greatly appreciated.

Cheers
Reply With Quote
  #2  
Old 07-16-2020, 03:39 PM
Guessed's Avatar
Guessed Guessed is online now Word macro to cut a range of text and apply to subsequent text as a hyperlink Windows 10 Word macro to cut a range of text and apply to subsequent text as a hyperlink Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,855
Guessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of light
Default

Can you provide document containing a couple of these sample bits of text?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 07-16-2020, 03:57 PM
scientist101 scientist101 is offline Word macro to cut a range of text and apply to subsequent text as a hyperlink Windows 10 Word macro to cut a range of text and apply to subsequent text as a hyperlink Office 2019
Novice
Word macro to cut a range of text and apply to subsequent text as a hyperlink
 
Join Date: Jul 2020
Posts: 6
scientist101 is on a distinguished road
Default

No problem. I've uploaded a Word doc with five references in the format described in my original post. Thanks!
Attached Files
File Type: docx test doc.docx (17.9 KB, 4 views)
Reply With Quote
  #4  
Old 07-16-2020, 06:31 PM
Guessed's Avatar
Guessed Guessed is online now Word macro to cut a range of text and apply to subsequent text as a hyperlink Windows 10 Word macro to cut a range of text and apply to subsequent text as a hyperlink Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,855
Guessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of light
Default

Try this code
Code:
Sub MakeMyLink()
  Dim aRng As Range, sFound() As String, sLink As String, sText As String
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .Text = "\<\<(*)\>\>\<\<(*)\>\>"
    .MatchWildcards = True
    .Replacement.Text = "\1|\2"
    Do While .Execute
      sFound = Split(aRng.Text, ">><<")
      sLink = Replace(sFound(0), "<<", "")
      sText = Replace(sFound(1), ">>", "")
      ActiveDocument.Hyperlinks.Add Anchor:=aRng, Address:=sLink, TextToDisplay:=sText
      aRng.Collapse Direction:=wdCollapseEnd
    Loop
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 07-16-2020, 10:43 PM
scientist101 scientist101 is offline Word macro to cut a range of text and apply to subsequent text as a hyperlink Windows 10 Word macro to cut a range of text and apply to subsequent text as a hyperlink Office 2019
Novice
Word macro to cut a range of text and apply to subsequent text as a hyperlink
 
Join Date: Jul 2020
Posts: 6
scientist101 is on a distinguished road
Thumbs up

That worked like a charm. Thanks!
Reply With Quote
  #6  
Old 07-17-2020, 12:23 PM
scientist101 scientist101 is offline Word macro to cut a range of text and apply to subsequent text as a hyperlink Windows 10 Word macro to cut a range of text and apply to subsequent text as a hyperlink Office 2019
Novice
Word macro to cut a range of text and apply to subsequent text as a hyperlink
 
Join Date: Jul 2020
Posts: 6
scientist101 is on a distinguished road
Default Follow-up question

Hi Guessed,

The macro works great, but I'm running into an odd follow-up problem. I can use Word's 'save as pdf' function, and the links in that pdf seem to work fine. However, there's a secondary program I use those pdfs in, and the links no longer function. However, any links I've made manually work.

What's odd is that in the Word file, when you hover over the links, the addresses (either using the macro or manually) look exactly the same. But when you save as pdf, they look slightly different. The macro links have the syntax of "file:///C:", while the manually entered hyperlinks have the syntax "file:///C|".

It seems like the secondary software I'm working with likes the latter format. I've attached some files if it's helpful.

1) bibliotest - Word doc showing macro and manual links

2) bibliotest (first pdf) - pdf that comes from using Word save as function. Links still work, but the manual and macro links are different (c: vs. c|)

3) bibliotest (second pdf) - pdf generated by my secondary software. The input was the first pdf. Now only the manually entered links are still functional.

Any thoughts?

Thanks!
Attached Files
File Type: docx bibliotest.docx (26.8 KB, 3 views)
File Type: pdf bibliotest (first pdf).pdf (14.8 KB, 1 views)
File Type: pdf bibliotest (second pdf).pdf (18.6 KB, 2 views)
Reply With Quote
  #7  
Old 07-19-2020, 06:01 PM
Guessed's Avatar
Guessed Guessed is online now Word macro to cut a range of text and apply to subsequent text as a hyperlink Windows 10 Word macro to cut a range of text and apply to subsequent text as a hyperlink Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,855
Guessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of light
Default

Looking at the hyperlink of the one you did manually, it is relative to the current document location ie.
../Refs/Clinical_5.4/AichingerG_2011,_Ross_River_vaccine.pdf
Because it starts with .. this tells the system to go up one folder level and then down in to the Refs folder.

The other hyperlinks (which fail) have the full path defined. Also, the direction of the slashes seems to vary although I'm not sure that this is important.
Is your secondary software failing to convert the hyperlinks because the absolute path is invalid but the relative path is fine?

Are you able to run this macro on your Word document to see if that cures the problem when it hits the secondary software.
Code:
Sub ChangeHL()
  Dim aHL As Hyperlink, sLink As String
  For Each aHL In ActiveDocument.Hyperlinks
    sLink = aHL.Address
    sLink = Replace(sLink, "C:\Users\iaman\Desktop\eCTD_Office_Software\IND_Source_Folders\YFV_IND", "..")
    sLink = Replace(sLink, "\", "/")
    aHL.Address = sLink
  Next aHL
End Sub
If that 'repairs' the hyperlinks, you could customise the original macro to make the links relative on the first pass.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 07-20-2020, 08:50 AM
scientist101 scientist101 is offline Word macro to cut a range of text and apply to subsequent text as a hyperlink Windows 10 Word macro to cut a range of text and apply to subsequent text as a hyperlink Office 2019
Novice
Word macro to cut a range of text and apply to subsequent text as a hyperlink
 
Join Date: Jul 2020
Posts: 6
scientist101 is on a distinguished road
Default

Thanks so much. I had resorted to toggling field codes and then using find and replace. There's probably a more elegant way to do this, but I just hooked the two macros together:

Sub MakeMyLink()
Dim aRng As Range, sFound() As String, sLink As String, sText As String
Set aRng = ActiveDocument.Range
With aRng.Find
.Text = "\<\<(*)\>\>\<\<(*)\>\>"
.matchwildcards = True
.Replacement.Text = "\1|\2"
Do While .Execute
sFound = Split(aRng.Text, ">><<")
sLink = Replace(sFound(0), "<<", "")
sText = Replace(sFound(1), ">>", "")
ActiveDocument.Hyperlinks.Add Anchor:=aRng, Address:=sLink, SubAddress:="", ScreenTip:="", TextToDisplay:=sText
aRng.Collapse Direction:=wdCollapseEnd
Loop
End With
ChangeHL
End Sub

Sub ChangeHL()
Dim aHL As Hyperlink, sLink As String
For Each aHL In ActiveDocument.Hyperlinks
sLink = aHL.Address
sLink = Replace(sLink, "C:\Users\iaman\Desktop\eCTD_Office_Software\IND_S ource_Folders\YFV_IND", "..")
sLink = Replace(sLink, "", "/")
aHL.Address = sLink
Next aHL
End Sub


Cheers!
Reply With Quote
  #9  
Old 07-20-2020, 03:59 PM
Guessed's Avatar
Guessed Guessed is online now Word macro to cut a range of text and apply to subsequent text as a hyperlink Windows 10 Word macro to cut a range of text and apply to subsequent text as a hyperlink Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,855
Guessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of lightGuessed is a glorious beacon of light
Default

This code is not super reusable because of the hard coding of the path to be removed. It is probably possible to make this adapt dynamically by looking at the folder path where the Word document is saved and finding the overlap with the file path to the hyperlinks but that will take a fair bit of extra coding.

This is how you could make it back into a single macro with a hard coded path
Code:
Sub MakeMyLink()
  Dim aRng As Range, sFound() As String, sLink As String, sText As String
  
  'configure path to remove the path overlaps and make hyperlinks 'relative' to the location of the Word doc
  Const sCommon As String = "C:\Users\iaman\Desktop\eCTD_Office_Software\IND_Source_Folders\YFV_IND"
  
  Set aRng = ActiveDocument.Range
  With aRng.Find
    .Text = "\<\<(*)\>\>\<\<(*)\>\>"
    .MatchWildcards = True
    .Replacement.Text = "\1|\2"
    Do While .Execute
      sFound = Split(aRng.Text, ">><<")
      sText = Replace(sFound(1), ">>", "")
      sLink = Replace(sFound(0), "<<", "")
      sLink = Replace(sLink, sCommon, "..")
      sLink = Replace(sLink, "", "/")
      ActiveDocument.Hyperlinks.Add Anchor:=aRng, Address:=sLink, TextToDisplay:=sText
      aRng.Collapse Direction:=wdCollapseEnd
    Loop
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #10  
Old 07-20-2020, 04:57 PM
scientist101 scientist101 is offline Word macro to cut a range of text and apply to subsequent text as a hyperlink Windows 10 Word macro to cut a range of text and apply to subsequent text as a hyperlink Office 2019
Novice
Word macro to cut a range of text and apply to subsequent text as a hyperlink
 
Join Date: Jul 2020
Posts: 6
scientist101 is on a distinguished road
Default

Thanks! I'll give that a try.
Reply With Quote
Reply

Tags
hyperlinking, macro find text

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Highlighting applied to range ending in a paragraph continues to apply to text added after Peterson Word VBA 2 10-08-2018 02:50 PM
Macro how to add hyperlink to text in document hullsquashboy Word VBA 31 10-15-2017 08:37 AM
Word macro to cut a range of text and apply to subsequent text as a hyperlink Word changing my body text to heading text when I apply paragraph numbering ddiemetric Word 1 01-17-2017 01:10 PM
Microsoft Word macro to find text, select all text between brackets, and delete helal1990 Word VBA 4 02-05-2015 03:52 PM
Word macro to cut a range of text and apply to subsequent text as a hyperlink Formatting- Apply changes to highlighted text results in same change to other text sential Word 6 01-10-2014 03:22 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 09:38 PM.


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