Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-14-2018, 06:09 PM
poetofpiano poetofpiano is offline How to extract selected hyperlink address to clipboard? Windows 8 How to extract selected hyperlink address to clipboard? Office 2013
Novice
How to extract selected hyperlink address to clipboard?
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default How to extract selected hyperlink address to clipboard?

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!
Reply With Quote
  #2  
Old 02-14-2018, 06:57 PM
macropod's Avatar
macropod macropod is offline How to extract selected hyperlink address to clipboard? Windows 7 64bit How to extract selected hyperlink address to clipboard? 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

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]
Reply With Quote
  #3  
Old 02-16-2018, 11:27 AM
poetofpiano poetofpiano is offline How to extract selected hyperlink address to clipboard? Windows 10 How to extract selected hyperlink address to clipboard? Office 2016
Novice
How to extract selected hyperlink address to clipboard?
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

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
This copied the following to the clipboard:

Code:
HYPERLINK "file:///C:\\Users\\Username\\Dropbox\\Doc.docx" \l "Bookmark Name"
Then using string and regex functions in Autohotkey, I was able to pare down the clipboard to just the following:

Code:
C:\Users\Username\Dropbox\Doc.docx#Bookmark Name
This is exactly what I was looking for, so thank you so much!
Reply With Quote
  #4  
Old 02-16-2018, 02:07 PM
macropod's Avatar
macropod macropod is offline How to extract selected hyperlink address to clipboard? Windows 7 64bit How to extract selected hyperlink address to clipboard? 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 poetofpiano View Post
your version only copied the name of the bookmark instead of the full file path
That suggests the actual hyperlink lacked the enclosing double quotes. You also hadn't mentioned wanting to get the hyperlink sub-address (i.e. the bookmark). In that case, try:
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]
Reply With Quote
  #5  
Old 02-16-2018, 09:48 PM
gmayor's Avatar
gmayor gmayor is offline How to extract selected hyperlink address to clipboard? Windows 10 How to extract selected hyperlink address to clipboard? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #6  
Old 02-16-2018, 11:18 PM
macropod's Avatar
macropod macropod is offline How to extract selected hyperlink address to clipboard? Windows 7 64bit How to extract selected hyperlink address to clipboard? 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

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]
Reply With Quote
  #7  
Old 02-17-2018, 03:01 AM
gmayor's Avatar
gmayor gmayor is offline How to extract selected hyperlink address to clipboard? Windows 10 How to extract selected hyperlink address to clipboard? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #8  
Old 02-17-2018, 03:28 PM
macropod's Avatar
macropod macropod is offline How to extract selected hyperlink address to clipboard? Windows 7 64bit How to extract selected hyperlink address to clipboard? 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

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]
Reply With Quote
  #9  
Old 02-18-2018, 07:17 PM
poetofpiano poetofpiano is offline How to extract selected hyperlink address to clipboard? Windows 10 How to extract selected hyperlink address to clipboard? Office 2016
Novice
How to extract selected hyperlink address to clipboard?
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

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

Thread Tools
Display Modes


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
How to extract selected hyperlink address to clipboard? 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
How to extract selected hyperlink address to clipboard? Extract email address from field zssteen Excel 1 06-19-2009 02:32 AM

Other Forums: Access Forums

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