none
Cast Excel.Worksheet to Excel.Worksheet in VB.Net RRS feed

  • Question

  • In VB.Net, how do you set a reference to a worksheet to an Excel worksheet variable?

    I know I can do like this for workbooks:

    Dim wkb as Excel.Workbook = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)

    but when it comes to sheets, I don't get it to work. The sheet I want to refer to is Globals.Sheet1 and the variable I want is targetSheet. Something like this gives you the idea of what I'm trying:

    Dim targetSheet as Excel.Worksheet = CType(Globals.Sheet1, Excel.Worksheet)

    I can't be very far from a working code, can I?

    Error I get right now is "Runtime errors might occur when converting 'ExcelWorkbook4.Sheet1' to 'Microsoft.Office.Interop.Excel.Worksheet'"

    Friday, July 19, 2013 7:19 AM

Answers

  •         Dim ws As Excel.Worksheet = Globals.Sheet1.InnerObject
            System.Diagnostics.Debug.Print(ws.UsedRange.Address.ToString())
    
    It's so straight-forward that no code sample is really necessary...

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by KilpAr Tuesday, July 23, 2013 12:49 PM
    Tuesday, July 23, 2013 12:39 PM
    Moderator

All replies