#1
|
|||
|
|||
word macro that reformats embedded links in a doc
Here's what I am trying to accomplish:
I want to add a subroutine into my word 2007 macro that will take the text and address for an embedded link in a document, and convert it to an href tag that uses the text as the display text, and the address for the html address. So a line that appears as "go here to get to google" with a blue underline, and when clicked goes to www.google.com, gets replaced with: <a target="_blank" href="http://www.google.com">go here to get to google</a> This routine is to be added to an existing macro that I already have that does some other minor formatting, so no need for headers and whatnot. That's it! If I am not explaining this well enough please don't hesitate to ask me any questions so I can clarify. Thanks! Editing to add- Played around with word a bit and I discovered the name of the structure I want to manipulate - I think. ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _ "www.google.com", _ SubAddress:="", ScreenTip:="", TextToDisplay:= _ "FBI Bulletin test display text", Target:="_blank" Selection.Collapse Direction:=wdCollapseEnd So it looks like the Address:="inside quotes" and TextToDisplay:= _"inside quotes" are the 2 variables I want to grab, store, and shove into the example string above, with that static code around it. I have a few lines in this macro that say, find underlined text, and replace with html tags around the text, but this one is a touch over my head. I also want to make sure if there's 5 links in a page that I grab them all as well. Thanks again! Last edited by EricT; 03-27-2012 at 06:37 PM. Reason: added content |
#2
|
||||
|
||||
Hi Eric,
Try: Code:
Sub TagHyperlinks() Dim HLnk As Hyperlink For Each HLnk In ActiveDocument.Hyperlinks HLnk.Range.InsertBefore "<a target=""_blank"" href=""" & HLnk.Address & """>" HLnk.Range.InsertAfter "</a>" Next End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Perfect! Thanks Paul.
One unforseen issue with what I am requesting - the display text is underlined by word once you make it a hyperlink. Unmaking it, such as we are here, still leaves the underline. I would like to remove that underline if at all possible, but only on the display text - I know how to remove all underlines in the entire doc but I'm unsure how to limit it to just that one string there. Any ideas? Just thought I would ask, this was really helpful, thank you! |
#4
|
||||
|
||||
Hi Eric,
With the code I posted, I was actually being careful to not 'unmake' the hyperlinks! Try: Code:
Sub TagHyperlinks() Dim i As Long With ActiveDocument For i = .Hyperlinks.Count To 1 Step -1 With .Hyperlinks(i) .Range.InsertBefore "<a target=""_blank"" href=""" & .Address & """>" .Range.InsertAfter "</a>" .Range.Fields(1).Unlink End With Next End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] Last edited by macropod; 03-28-2012 at 03:00 PM. Reason: Code fix. |
#5
|
|||
|
|||
Thank you for the code, i was looking for it ) but i get underlined blue text, i dont know how to use ClearFormatting... Can you help?
|
#6
|
||||
|
||||
Which version of the macro did you use? In any event, 'ClearFormatting' isn't applicable.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Code from post #4
|
#8
|
||||
|
||||
That suggests maybe your hyperlinks had been manually formatted at some stage, rather than relying on Word's automatic hyperlink formatting.
After the line: .Range.Fields(1).Unlink add: .Range.Font.Reset
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
link, macro, vba |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Convert html links into embedded hyperlinks | njcloud | Mail Merge | 23 | 02-04-2020 02:00 PM |
Can't open Office 2007 embedded as objects in Word | DK8220 | Word | 5 | 11-02-2011 01:06 AM |
Macro to loop in subfolders, change links, export xml data | Catalin.B | Excel Programming | 2 | 09-08-2011 11:37 PM |
Embedded links | spheon | Word | 1 | 06-07-2011 04:11 PM |
Embedded Excel in Word very slow to update | delaneybob | Word | 0 | 02-11-2011 10:03 AM |