How to Copy to Clipboard in Microsoft Visual Basic

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Copy to Clipboard in Microsoft Visual Basic
You can copy data to the system clipboard using Visual Basic.NET. (digital city. binary tunnel and city at night image by Stasys Eidiejus from

It's useful to know how to use the system clipboard to copy information from one application to another using Microsoft Visual Basic.NET (VB.NET). The clipboard is an environment that is shared among programs in Windows, making it easier to share information. Programmers often need to export information from a VB.NET application to a Microsoft Excel worksheet. However, you can write VB.NET code to copy data to the system clipboard and paste it in an Excel worksheet.

Skill level:


  1. 1

    Type the following code in a new subroutine in your VB.NET project to declare your Excel variables:

    Dim XLApp As Microsoft.Office.Interop.Excel.Application = Nothing

    Dim XLBooks As Microsoft.Office.Interop.Excel.Workbooks = Nothing

    Dim XLBook As Microsoft.Office.Interop.Excel.Workbook = Nothing

    Dim XLSheets As Microsoft.Office.Interop.Excel.Sheets = Nothing

    Dim XLSheet As Microsoft.Office.Interop.Excel.Worksheet = Nothing

  2. 2

    Type the following to add a string of text to the clipboard and get the text from the clipboard:

    Dim process Object As New Process

    Clipboard.SetDataObject("This is copied to clipboard and added to Excel.")

    Dim clipboard Object As IDataObject = Clipboard.GetDataObject()

  3. 3

    Type the following to open Excel and add a new worksheet:

    XLApp = New Microsoft.Office.Interop.Excel.Application

        XLApp.Visible = True
        XLApp.DisplayAlerts = False
        XLBook = CType(XLApp.Workbooks.Add(), Microsoft.Office.Interop.Excel.Workbook)
        XLBooks = XLApp.Workbooks
        XLSheet = CType(XLBooks(1).Sheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
        XLSheets = XLBook.Worksheets
  4. 4

    Type the following to add the string from the clipboard to Excel:

    With clipboard Object

       If .GetDataPresent(DataFormats.Text) Then
           XLSheet.Cells(1, 1) = .GetData(DataFormats.Text)
       End If

    End With

  5. 5

    Run your program.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.