ClipboardGetLink


Description:
This function allows you to get at more information in the clipboard than Visual Basic's intrinsic Clipboard object. When you copy a hyperlink in Internet Explorer, IE actually stores quite a bit of data in the clipboard for other applications to access, not just the URL of the hyperlink itself. This demonstrates how to use the Windows' API to get at the name of the hyperlink itself, which, if the hyperlink is copied from a webpage, is the text of the hyperlink. This can be useful to provide the user with a prettier name of the hyperlink.
 
Code:
Option Explicit

Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) _
        As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Function EnumClipboardFormats Lib "user32" (ByVal wFormat As _
        Long) As Long
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GetClipboardData Lib "user32" (ByVal wFormat As _
        Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
        As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (Destination As Any, Source As Any, ByVal Length As Long)
Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal _
        wFormat As Long) As Long

Public Sub ClipboardGetLink(sLinkName As String, sLinkURL As String)
'Retreives the contents of the clipboard, sLinkName is the name
' as stored by IE, and and sLinkURL is the text of the URL itself

   Dim nSize As Long
   Dim sData As String
   Dim lpVoid As Long
   Dim hMem As Long
   
   'Attempt to open the clipboard
   If OpenClipboard(0) Then
      
      'If there's a text format available, copy it out
      If IsClipboardFormatAvailable(1) Then
         hMem = GetClipboardData(1)
         nSize = GlobalSize(hMem)
         lpVoid = GlobalLock(hMem)
         sData = Space(nSize)
         If lpVoid <> 0 Then
            CopyMemory ByVal sData, ByVal lpVoid, nSize
            GlobalUnlock hMem
            sLinkURL = Trim(Mid(sData, 1, InStr(1, sData, _
                vbNullChar) - 1))
         End If
      End If
      
      'This is the ASCII version of the name format, if it's
      ' there, copy it out
      If IsClipboardFormatAvailable(49269) Then
         hMem = GetClipboardData(49269)
         nSize = GlobalSize(hMem)
         lpVoid = GlobalLock(hMem)
         sData = Space(nSize)
         If lpVoid <> 0 Then
            CopyMemory ByVal sData, ByVal lpVoid, nSize
            GlobalUnlock hMem
            sData = Mid(sData, 77)
            sLinkName = Trim(Mid(sData, 1, InStr(1, sData, _
              vbNullChar) - 1))
              
            'Chop off the URL extension to the name
            If Right(sLinkName, 4) = ".url" Then
               sLinkName = Mid(sLinkName, 1, Len(sLinkName) - 4)
            End If
         End If
      End If
      
      CloseClipboard
   End If

End Sub

 
Sample Usage:
 
ClipboardGetLink sLinkName, sLinkURL
Debug.Print sLinkName, "=", sLinkURL