|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
Can you provide document containing a couple of these sample bits of text?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
No problem. I've uploaded a Word doc with five references in the format described in my original post. Thanks!
|
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
That worked like a charm. Thanks!
|
#6
|
|||
|
|||
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! |
#7
|
||||
|
||||
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
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
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! |
#9
|
||||
|
||||
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 |
#10
|
|||
|
|||
Thanks! I'll give that a try.
|
Tags |
hyperlinking, macro find text |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro how to add hyperlink to text in document | hullsquashboy | Word VBA | 32 | 06-28-2022 06:30 PM |
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 |
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 |
Formatting- Apply changes to highlighted text results in same change to other text | sential | Word | 6 | 01-10-2014 03:22 PM |