![]() |
|
|
|
#1
|
|||
|
|||
|
I am trying to copy the address (not the display text) of the currently selected hyperlinked text in a word document. However, I am sort of new at this and all my research hasn't led to a definitive method.
I see this question has been asked before at https://stackoverflow.com/questions/...-links-address but the answer offered there is not working for me, and in any case, I would prefer to just use a more current method for Word 2016, and I'm assuming what I want can be done without adding a legacy form library and just using the Hyperlink.Address Property detailed at https://msdn.microsoft.com/en-us/vba...-property-word Any help would be very appreciated! |
|
#2
|
||||
|
||||
|
Try:
Code:
Sub CopyHyperlink() Dim Rng As Range Set Rng = Selection.Hyperlinks(1).Range.Fields(1).Code With Rng .MoveStartUntil Chr(34), wdForward .Start = .Start + 1 .Collapse wdCollapseStart .MoveEndUntil Chr(34), wdForward .Copy End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Thank you for your help macropod. Because your version only copied the name of the bookmark instead of the full file path, I used a simplified version of your suggestion as follows:
Code:
Sub CopyHyperlink() Dim Rng As Range Set Rng = Selection.Hyperlinks(1).Range.Fields(1).Code Rng.Copy End Sub Code:
HYPERLINK "file:///C:\\Users\\Username\\Dropbox\\Doc.docx" \l "Bookmark Name" Code:
C:\Users\Username\Dropbox\Doc.docx#Bookmark Name |
|
#4
|
||||
|
||||
|
Quote:
Code:
Sub CopyHyperlink()
Dim Rng As Range
With Selection.Hyperlinks(1).Range.Fields(1)
Set Rng = .Code
With .Code
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
.Text = "\l"
.Replacement.Text = "#"
.Execute Replace:=wdReplaceAll
.Text = "HYPERLINK"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
.Text = "^w"
.Execute Replace:=wdReplaceAll
.Text = Chr(34)
.Execute Replace:=wdReplaceAll
End With
End With
End With
Rng.Copy
While InStr(Rng.Text, "HYPERLINK") = 0
Undo
Wend
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
||||
|
||||
|
Unless I am missing something here (it wouldn't be the first time) why not use the hyperlink address property? e.g.
Code:
Sub GetLinkAddress()
Dim dFname As DataObject
Dim hLink As Hyperlink
Set hLink = Selection.Hyperlinks(1)
Set dFname = New DataObject
dFname.SetText hLink.Address
dFname.PutInClipboard
MsgBox hLink.Address & vbCr & "copied to clipboard"
lbl_Exit:
Set hLink = Nothing
Set dFname = Nothing
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#6
|
||||
|
||||
|
I was trying to avoid setting a reference to the MS Forms 2.0 Object Library - which your code requires - plus getting both the hyperlink address and the hyperlink sub-address (if present) as a single, correctly-parsed, clipboard object - which I think your code also doesn't do.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#7
|
||||
|
||||
|
OK. I see your point, but the issues you raise are not insurmountable - how about
Code:
Sub GetLinkAddress2()
Dim hLink As Hyperlink
Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.Collapse 0
Set hLink = Selection.Hyperlinks(1)
oRng.Text = hLink.Address
If Not hLink.SubAddress = "" Then oRng.Text = oRng.Text & "#" & hLink.SubAddress
MsgBox oRng.Text & vbCr & "copied to clipboard"
oRng.Cut
lbl_Exit:
Set hLink = Nothing
Set oRng = Nothing
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#8
|
||||
|
||||
|
Yes, that's a god alternative (better in some ways, as my code didn't address the presence of other field switches), but one still needed use the MS Forms 2.0 Object Library:
Code:
Sub CopyHyperlink()
Dim StrTxt As String
With Selection.Hyperlinks(1)
StrTxt = .Address
If .SubAddress <> "" Then StrTxt = StrTxt & "#" & .SubAddress
With .Range.Fields(1).Code
.Text = StrTxt
.Copy
End With
ActiveDocument.Undo
End With
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#9
|
|||
|
|||
|
Thank you both for your help. In the end I used Macropod's code:
Code:
Sub CopyHyperlink()
Dim StrTxt As String
With Selection.Hyperlinks(1)
StrTxt = .Address
If .SubAddress <> "" Then StrTxt = StrTxt & "#" & .SubAddress
With .Range.Fields(1).Code
.Text = StrTxt
.Copy
End With
End With
ActiveDocument.Undo
End Sub
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Extract name and address. | donlincolnmsof | Word VBA | 1 | 11-10-2017 12:59 PM |
| Add Hyperlink in Visio that when clicked copies something to your clipboard | MoHyB | Visio | 0 | 11-10-2017 08:55 AM |
Find & selected paras with highlighted text & copy to clipboard
|
marceepoo | Word VBA | 1 | 09-14-2012 08:20 PM |
| How to extract selected boilerplate into a new doc | mturner296 | Word | 0 | 03-29-2010 07:35 AM |
Extract email address from field
|
zssteen | Excel | 1 | 06-19-2009 02:32 AM |