Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-27-2012, 03:58 PM
EricT EricT is offline word macro that reformats embedded links in a doc Windows 7 64bit word macro that reformats embedded links in a doc Office 2007
Novice
word macro that reformats embedded links in a doc
 
Join Date: Mar 2012
Posts: 2
EricT is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 03-27-2012, 10:31 PM
macropod's Avatar
macropod macropod is offline word macro that reformats embedded links in a doc Windows 7 64bit word macro that reformats embedded links in a doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
To use it, you can simply add 'Call TagHyperlinks' at the appropriate place in your existing code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-28-2012, 07:57 AM
EricT EricT is offline word macro that reformats embedded links in a doc Windows 7 64bit word macro that reformats embedded links in a doc Office 2007
Novice
word macro that reformats embedded links in a doc
 
Join Date: Mar 2012
Posts: 2
EricT is on a distinguished road
Default

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!
Reply With Quote
  #4  
Old 03-28-2012, 02:59 PM
macropod's Avatar
macropod macropod is offline word macro that reformats embedded links in a doc Windows 7 64bit word macro that reformats embedded links in a doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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.
Reply With Quote
  #5  
Old 09-12-2012, 09:44 AM
Wasteful Wasteful is offline word macro that reformats embedded links in a doc Windows 7 32bit word macro that reformats embedded links in a doc Office 2010 32bit
Novice
 
Join Date: Sep 2012
Posts: 2
Wasteful is on a distinguished road
Default

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?
Reply With Quote
  #6  
Old 09-12-2012, 09:53 AM
macropod's Avatar
macropod macropod is offline word macro that reformats embedded links in a doc Windows 7 64bit word macro that reformats embedded links in a doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Wasteful View Post
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?
Which version of the macro did you use? In any event, 'ClearFormatting' isn't applicable.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 09-12-2012, 09:59 AM
Wasteful Wasteful is offline word macro that reformats embedded links in a doc Windows 7 32bit word macro that reformats embedded links in a doc Office 2010 32bit
Novice
 
Join Date: Sep 2012
Posts: 2
Wasteful is on a distinguished road
Default

Code from post #4
Reply With Quote
  #8  
Old 09-12-2012, 04:13 PM
macropod's Avatar
macropod macropod is offline word macro that reformats embedded links in a doc Windows 7 64bit word macro that reformats embedded links in a doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Tags
link, macro, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
word macro that reformats embedded links in a doc Convert html links into embedded hyperlinks njcloud Mail Merge 23 02-04-2020 02:00 PM
word macro that reformats embedded links in a doc Can't open Office 2007 embedded as objects in Word DK8220 Word 5 11-02-2011 01:06 AM
word macro that reformats embedded links in a doc Macro to loop in subfolders, change links, export xml data Catalin.B Excel Programming 2 09-08-2011 11:37 PM
word macro that reformats embedded links in a doc 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:59 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