VS 2010 [RESOLVED] Copy File Link to Clipboard-VBForums
Results 1 to 4 of 4

Thread: [RESOLVED] Copy File Link to Clipboard

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    Resolved [RESOLVED] Copy File Link to Clipboard

    Hi all,

    I almost feel silly asking as I feel like the answer is staring me in the face, but I haven't been able to find anything on Google that actually helps my specific issue.

    The origin of the issue is moot, but I have an Excel Addin with a button that I would like to use to copy a link to the workbook that is currently active. I am able to get the text to copy to the clipboard, however when I paste it in Word or an Email, it pastes as plain text, but I want a link. Is this possible? I've played with different DataFormats, but I don't think I quite grasp how to use that. Below is what I currently have:

    Code:
        Private Sub btnCopyLink_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnCopyLink.Click
            Dim objBook As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
    
            If MsgBox("Copy a link to " & objBook.FullName & " to the clipboard?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Copy Link?") = MsgBoxResult.No Then Exit Sub
    
            My.Computer.Clipboard.Clear()
            My.Computer.Clipboard.SetText("file:///" & objBook.FullName)
        End Sub

  2. #2
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Copy File Link to Clipboard

    vb Code:
    1. 'Demonstrates SetText, ContainsText, and GetText.
    2. Public Function SwapClipboardHtmlText( _
    3.     ByVal replacementHtmlText As String) As String
    4.  
    5.     Dim returnHtmlText As String = Nothing
    6.  
    7.     If (Clipboard.ContainsText(TextDataFormat.Html)) Then
    8.         returnHtmlText = Clipboard.GetText(TextDataFormat.Html)
    9.         Clipboard.SetText(replacementHtmlText, TextDataFormat.Html)
    10.     End If
    11.  
    12.     Return returnHtmlText
    13.  
    14. End Function

    from MSDN

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    Re: Copy File Link to Clipboard

    Quote Originally Posted by proneal View Post
    vb Code:
    1. 'Demonstrates SetText, ContainsText, and GetText.
    2. Public Function SwapClipboardHtmlText( _
    3.     ByVal replacementHtmlText As String) As String
    4.  
    5.     Dim returnHtmlText As String = Nothing
    6.  
    7.     If (Clipboard.ContainsText(TextDataFormat.Html)) Then
    8.         returnHtmlText = Clipboard.GetText(TextDataFormat.Html)
    9.         Clipboard.SetText(replacementHtmlText, TextDataFormat.Html)
    10.     End If
    11.  
    12.     Return returnHtmlText
    13.  
    14. End Function

    from MSDN
    Thank you for your response. I did see that on MSDN, however I guess I'm not sure how that helps me. I did try setting the text as HTML but I couldn't paste anything after.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    Re: Copy File Link to Clipboard

    So after playing with this I realized that apparently I'm just picky. I wanted the link to paste without the "file:///" before it, but from what I can tell there is no way to do that so I can suck it up.

    Just in case anyone can find it useful, I have modified my sub to replace the Drive letter with the network UNC path:

    Code:
        Private Sub btnCopyLink_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnCopyLink.Click
            Dim objBook As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
            Dim sPath As String
            Dim oDrive As System.IO.DriveInfo
    
            sPath = objBook.FullName
    
            If Mid(sPath, 1, 1).ToUpper = "C" Then
                MsgBox("Links to spreadsheets that are not located in a network folder cannot be copied.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Cannot Copy Link")
                Exit Sub
            End If
    
            For Each oDrive In My.Computer.FileSystem.Drives
                If InStr(sPath, oDrive.Name) <> 0 Then
                    sPath = sPath.Replace(oDrive.Name, Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\Network\" & Mid(oDrive.Name, 1, 1), "RemotePath", Mid(oDrive.Name, 1, 2)) & "\")
                    Exit For
                End If
            Next
    
            If MsgBox("Copy a link to " & sPath & " to the clipboard?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Copy Link?") = MsgBoxResult.No Then Exit Sub
    
            My.Computer.Clipboard.Clear()
            My.Computer.Clipboard.SetText("file:///" & sPath)
        End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.