简体   繁体   中英

VB.NET Late Binding

For two days, I haven't been able to change the VB-Code to EARLY binding:

Dim Excel02 = New Microsoft.Office.Interop.Excel.Application()
Excel02.Application.WindowState = Excel02.XlWindowState.xlMaximized
Dim Excel02Workbook As Microsoft.Office.Interop.Excel.Workbook
Excel02Workbook = Excel02.Workbooks.Open("C:\Eigene-D\DS-GM\DS-GM-2016.xlsm")
Dim Excel02WorkSheet01 As Microsoft.Office.Interop.Excel.Worksheet
Excel02WorkSheet01 = CType(Excel02Workbook.Sheets("eBay"), Microsoft.Office.Interop.Excel.Worksheet)
Excel02WorkSheet01.Activate()

Excel02.Visible = True

Dim RowEinfuegen1 As Microsoft.Office.Interop.Excel.Worksheet = CType(Excel02WorkSheet01.Rows(10).Resize(5).Insert, Microsoft.Office.Interop.Excel.Worksheet)    

The last row is the problem, please help.

I agree, the last line is a problem. But what are you trying to accomplish?

Dim RowEinfuegen1 As Microsoft.Office.Interop.Excel.Worksheet =
    CType(Excel02WorkSheet01.Rows(10).Resize(5).Insert, 
        Microsoft.Office.Interop.Excel.Worksheet)

The object you're trying to cast

Excel02WorkSheet01.Rows(10).Resize(5).Insert

appears to be a bool

调试器屏幕

But you are casting it as a Worksheet. A cast is probably not what you want. I wrote some code to create a new sheet in Excel02 , and insert the range. Maybe this is what you want to do.

Dim Excel02 = New Microsoft.Office.Interop.Excel.Application()
Excel02.Application.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized
Dim Excel02Workbook As Microsoft.Office.Interop.Excel.Workbook
Excel02Workbook = Excel02.Workbooks.Open("C:\Temp\DS-GM-2016.xlsx")
Dim Excel02WorkSheet01 As Microsoft.Office.Interop.Excel.Worksheet
Excel02WorkSheet01 = CType(Excel02Workbook.Sheets("eBay"), Microsoft.Office.Interop.Excel.Worksheet)
Excel02WorkSheet01.Activate()

Excel02.Visible = True

Dim rowsRange As Microsoft.Office.Interop.Excel.Range = Excel02WorkSheet01.Rows(10)
Dim resizedRange As Microsoft.Office.Interop.Excel.Range = rowsRange.Resize(5)

Dim newBook As Microsoft.Office.Interop.Excel.Workbook = Excel02.Workbooks.Add()
Dim newSheet As Microsoft.Office.Interop.Excel.Worksheet = newBook.Sheets.Add()
Dim newRange As Microsoft.Office.Interop.Excel.Range = newSheet.Rows(1)
newRange.Insert(0, resizedRange)

Remember, you will need to get rid of the unmanaged objects in memory (C# GC won't (promptly) do this automatically). See how

Marshal.ReleaseComObject(newRange)
Marshal.ReleaseComObject(newSheet)
Marshal.ReleaseComObject(newBook)
Marshal.ReleaseComObject(resizedRange)
Marshal.ReleaseComObject(rowsRange)
Marshal.ReleaseComObject(Excel02WorkSheet01)
Marshal.ReleaseComObject(Excel02Workbook)
Marshal.ReleaseComObject(Excel02)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM