Late Binding e.g. to Office, Excel, Word etc

 
Option Strict Off
‘Early binding would require COM reference to Excel which brings in
‘   Microsoft.Office.Core
‘   Microsoft.Office.Interop.Excel
‘Imports Microsoft.Office.Interop
‘However late binding requires Option Strict Off
 
Public Class cls
    Private Sub CopyClipboardToExcel(Optional ByVal MyNewFileName As String = "")
        ‘Late Binding or distribute reference
        ‘http://msdn.microsoft.com/en-us/library/0tcf61s1.aspx
        Try
            ‘Dim xlApp As Excel.Application
            Dim xlApp As Object = CreateObject("Excel.Application")
            Dim xlBook As Object = xlApp.Workbooks.Add
            Dim xlSheet As Object = xlBook.Worksheets(1)
            xlSheet.Range("A1").Select()
            xlSheet.Paste()
            Call FormatExcelSheet(xlApp)
            ‘xlSheet.Cells(2, 2) = "This is column B row 2"
            xlSheet.Application.Visible = True
            If MyNewFileName <> "" Then
                xlSheet.SaveAs(MyNewFileName)
                ‘ Optionally, you can call xlApp.Quit to close the workbook.
            End If
        Catch ex As Exception
            If IsT1 Then
                Throw New ApplicationException(ex.Message, ex)
            End If
        End Try
    End Sub
End Class
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: