- 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). Type the following to add a string of text to the clipboard and get the text from the clipboard:
Dim processObject As New Process
Clipboard.SetDataObject("This is copied to clipboard and added to Excel.")
Dim clipboardObject As IDataObject = Clipboard.GetDataObject() - 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). Type the following to add the string from the clipboard to Excel:
With clipboardObject
If .GetDataPresent(DataFormats.Text) Then
XLSheet.Cells(1, 1) = .GetData(DataFormats.Text)
End If
End With - 5). Run your program.
previous post