How to Copy a String From One Application & Paste It in Another With Visual Basic

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Copy a String From One Application & Paste It in Another With Visual Basic
It's simple to copy a string to clipboard and paste it in another application using visual basic. (binary world image by Attila Toro from Fotolia.com)

Having the knowledge on how to copy data from one application and pasting it in another can save you time when working with different applications. You can use the clipboard to copy data and paste it in another application using Visual Basic.NET. The clipboard is a set of functions that all applications have access to, making it simple to transfer data between them. Visual Basic is a Microsoft programming language that is relatively easy to use.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Start Visual Basic.NET and create a new "Windows Forms Application" project. Add a "TextBox" and two "Buttons" to the form.

  2. 2

    Click "Project" in the "File" menu and select "<project> Properties". Select "References" on the left pane and click "Add...". Click the "COM" tab, select "Microsoft Excel 12.0 Object Library" and click "OK".

  3. 3

    Double-click the form to open the "Form1.vb" module. Under "Form1_Load" Type the following to add a text string to the text box:

    Me.TextBox1.Text = "I copied this string from a VB application and pasted in Microsoft Excel."

  4. 4

    Click "Form1.vb [Design]" tab and double-click "Button1". Type the following under "Button1_Click" to copy the string to clipboard:

    Dim obj As New Process

    Clipboard.SetDataObject(Me.TextBox1.Text)

  5. 5

    Type the following to the very top of your "Form1.vb" module:

    Imports Microsoft.Office.Interop.Excel

  6. 6

    Click "Form1.vb [Design]" tab and double-click "Button2". Type the following to create a new spreadsheet, open it and paste the string copied to clipboard from your VB application:

    Dim objClipbrd As IDataObject = Clipboard.GetDataObject()

        Dim objXL As Microsoft.Office.Interop.Excel.Application = Nothing
    
        Dim objBks As Microsoft.Office.Interop.Excel.Workbooks = Nothing
    
        Dim objBk As Microsoft.Office.Interop.Excel.Workbook = Nothing
    
        Dim objShts As Microsoft.Office.Interop.Excel.Sheets = Nothing
    
        Dim objSht As Microsoft.Office.Interop.Excel.Worksheet = Nothing
    
    
    
        objXL = New Microsoft.Office.Interop.Excel.Application
    
        objXL.Visible = True
    
        objXL.DisplayAlerts = False
    
        objBk = CType(objXL.Workbooks.Add(), Microsoft.Office.Interop.Excel.Workbook)
    
        objBks = objXL.Workbooks
    
        objSht = CType(objBks(1).Sheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
    
        objShts = objBk.Worksheets
    
    
    
        With objClipbrd
    
            If .GetDataPresent(DataFormats.Text) Then
    
    
    
                objSht.Cells(1, 1) = .GetData(DataFormats.Text)
    
            End If
    
        End With
    
  7. 7

    Run your program and click "Button1" to copy the string displayed in the text box. Click on "Button2" to open a new Excel spreadsheet and paste the string.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

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