Hyperlinks

C

Clayton L. Wilson

This is almost like my last post. I use a macro to create
hyperlinks to other (Word) documents. How can I use a
macro to restore the link and sublink portion to text at
the same location? Thanks again.
BTW, I'm not a VB programmer. I tend to use the macro
recorder.
 
J

JGM

Hi Clayton,

Record the following key sequence if you want a macro, or just do it
manually... I do not think you need a macro as it is easy to do manually...

CTRL+A (Select All)
CTRL+SHIFT+F9 (Delete links in fields)

Note that this will delete all automatic links (Such as an auto-date field
or a page number) that are in the text body (Not those in headers or
footers).

So if you want to suppress only the hyperlinks and keep other field info,
then you are going to need a macro to selectively delete the field links.

HTH
Cheers!
 
C

Clayton L. Wilson

Not quite what I want. I don't want to delete the links,
just convert the link over to text. That is, I want to
take the link information (link and sublink) and insert it
into the text. For example:
link is "myfile.doc"
sublink is "myanchor"
text is "click here"
After my macro runs text will be "myfile.doc#myanchor
click here"
 
A

Astrid

Hi Clayton,

Maybe some code like this can do the trick?

----------------------------------------------------------------------------
Sub ReplaceHyperlinks()
Const csSep As String = "#"
Dim oRange As Range
Dim l As Long
Dim sAddress As String
Dim SubAddress As String
Dim sText As String

If ActiveDocument.Hyperlinks.Count > 0 Then
For l = ActiveDocument.Hyperlinks.Count To 1 Step -1
Set oRange = ActiveDocument.Hyperlinks(l).Range
sAddress = ActiveDocument.Hyperlinks(l).Address
SubAddress = ActiveDocument.Hyperlinks(l).SubAddress
sText = ActiveDocument.Hyperlinks(l).TextToDisplay
oRange.Text = sAddress & csSep & SubAddress & csSep & sText
Next l
End If

Set oRange = Nothing

End Sub
----------------------------------------------------------------------------

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/

"Clayton L. Wilson" <claytonwilson@ev1.net> schreef in bericht Not quite what I want. I don't want to delete the links,
just convert the link over to text. That is, I want to
take the link information (link and sublink) and insert it
into the text. For example:
link is "myfile.doc"
sublink is "myanchor"
text is "click here"
After my macro runs text will be "myfile.doc#myanchor
click here"
 
A

Astrid

Hi Clayton,

I read your message one more time and thought that you might want to keep the hyperlink but only want to change the display text. If this is the case, make one minor modification:

Sub ReplaceHyperlinks()
Const csSep As String = "#"
Dim oRange As Range
Dim l As Long
Dim sAddress As String
Dim SubAddress As String
Dim sText As String

If ActiveDocument.Hyperlinks.Count > 0 Then
For l = ActiveDocument.Hyperlinks.Count To 1 Step -1
Set oRange = ActiveDocument.Hyperlinks(l).Range
sAddress = ActiveDocument.Hyperlinks(l).Address
SubAddress = ActiveDocument.Hyperlinks(l).SubAddress
sText = ActiveDocument.Hyperlinks(l).TextToDisplay
ActiveDocument.Hyperlinks(l).TextToDisplay = sAddress & csSep & SubAddress & csSep & sText
Next l
End If
End Sub

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/

"Clayton L. Wilson" <claytonwilson@ev1.net> schreef in bericht Not quite what I want. I don't want to delete the links,
just convert the link over to text. That is, I want to
take the link information (link and sublink) and insert it
into the text. For example:
link is "myfile.doc"
sublink is "myanchor"
text is "click here"
After my macro runs text will be "myfile.doc#myanchor
click here"
 
C

Clayton L. Wilson

Just what I needed. Thanks.
I wonder if this will work for bookmarks too. I'll try.

Hi Clayton,

Maybe some code like this can do the trick?

----------------------------------------------------------------------------
Sub ReplaceHyperlinks()
Const csSep As String = "#"
Dim oRange As Range
Dim l As Long
Dim sAddress As String
Dim SubAddress As String
Dim sText As String

If ActiveDocument.Hyperlinks.Count > 0 Then
For l = ActiveDocument.Hyperlinks.Count To 1 Step -1
Set oRange = ActiveDocument.Hyperlinks(l).Range
sAddress = ActiveDocument.Hyperlinks(l).Address
SubAddress = ActiveDocument.Hyperlinks(l).SubAddress
sText = ActiveDocument.Hyperlinks(l).TextToDisplay
oRange.Text = sAddress & csSep & SubAddress & csSep & sText
Next l
End If

Set oRange = Nothing

End Sub
----------------------------------------------------------------------------

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to
the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/

"Clayton L. Wilson" <claytonwilson@ev1.net> schreef in bericht
Not quite what I want. I don't want to delete the links,
just convert the link over to text. That is, I want to
take the link information (link and sublink) and insert it
into the text. For example:
link is "myfile.doc"
sublink is "myanchor"
text is "click here"
After my macro runs text will be "myfile.doc#myanchor
click here"
 
D

Danny Shaw

I'm a newbie to the VBA and .Net world and this forum is very interesting and helpful.
Can anyone tell me how to validate all of the hyperlinks within a Word document
ie: detect any broken links

thanks,
Danny..
 
J

Jonathan West

Hi Danny,

For links within a document, you check that ther Address is blank, and that
the Subaddress property of the hyperlink is a bookmark that exists.

For links between documents, you have to check that the file pointed to by
the Address property exists, and if the Subaddress property isn't blank,
then open that document to check that the bookmark inside it exists.

it is possible to write a macro that does these checks automatically

For links to internet URLs, I know of no way of automatically checking.
 
D

Danny shaw

Jonathan

Thanks a bunch
Is there a way, programatically, to test a URL link
I'm assuming that if there is, I can simply wrap some error management
around it, trap the error and consider that a broken link.

Thanks,
Danny...
 
J

Jonathan West

Hi Danny

By this, do you mean test whether a link is to a URL rather than to a local
file? If so, yes - the Address property of the link will contain http:// or
ftp:// or mailto: at the start of the string. Just check for that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top