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:
Other People Are Reading
Start Visual Basic.NET and create a new "Windows Forms Application" project. Add a "TextBox" and two "Buttons" to the form.
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".
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."
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
Type the following to the very top of your "Form1.vb" module:
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
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for