Friday 14 December 2012

VB.NET - Excel - How to fix error - Selected method of range class failed

Error message is produced while trying to freeze panels

Watch on youtube
Replace

       'excel
        Dim oexcel As Object
        oexcel = CreateObject("Excel.Application")
        Dim obook As Excel.Workbook
        Dim osheet As Excel.Worksheet
        Dim fnExc As String = "C:\Temp\Test.xlsx"
        Dim curLine As Integer = 0

        ' this should be used only once
        obook = oexcel.Workbooks.Add

        'first
        If oexcel.Application.Sheets.Count() < 1 Then
            osheet = CType(obook.Worksheets.Add(), Excel.Worksheet)
        Else
            osheet = oexcel.Worksheets(1)
        End If
        osheet.Name = "Payroll Detail Report"
        osheet.Range("A1").Value = "Header"
        osheet.Range("A1", "A1").Select()
        oexcel.ActiveWindow.FreezePanes = True


        'second
        If oexcel.Application.Sheets.Count() < 2 Then
            osheet = CType(obook.Worksheets.Add(), Excel.Worksheet)
        Else
            osheet = oexcel.Worksheets(2)
        End If
        osheet.Name = "Payroll Detail Report (real)"
        osheet.Range("A1").Value = "Header"
        osheet.Range("A1", "A1").Select()
        oexcel.ActiveWindow.FreezePanes = True

        'third
        If oexcel.Application.Sheets.Count() < 3 Then
            osheet = CType(obook.Worksheets.Add(), Excel.Worksheet)
        Else
            osheet = oexcel.Worksheets(3)
        End If
        osheet.Name = "Payroll Summary Report"

        'next
        If oexcel.Application.Sheets.Count() < 4 Then
            osheet = CType(obook.Worksheets.Add(), Excel.Worksheet)
        Else
            osheet = oexcel.Worksheets(4)
        End If
        osheet.Move(After:=obook.Worksheets(obook.Worksheets.Count))
        osheet.Name = "Weekly Payroll Report - Week 1"


        obook.SaveAs(fnExc)
        obook.Close()
        obook = Nothing

With 
       'excel
        Dim oexcel As Object
        oexcel = CreateObject("Excel.Application")
        Dim obook As Excel.Workbook
        Dim osheet As Excel.Worksheet
        Dim fnExc As String = "C:\Temp\Test.xlsx"
        Dim curLine As Integer = 0

        ' this should be used only once
        obook = oexcel.Workbooks.Add

        'first
        If oexcel.Application.Sheets.Count() < 1 Then
            osheet = CType(obook.Worksheets.Add(), Excel.Worksheet)
        Else
            osheet = oexcel.Worksheets(1)
        End If
        osheet.Name = "Payroll Detail Report"
        osheet.Range("A1").Value = "Header"
        osheet.Select()
        osheet.Range("A1", "A1").Select()
        oexcel.ActiveWindow.FreezePanes = True


        'second
        If oexcel.Application.Sheets.Count() < 2 Then
            osheet = CType(obook.Worksheets.Add(), Excel.Worksheet)
        Else
            osheet = oexcel.Worksheets(2)
        End If
        osheet.Name = "Payroll Detail Report (real)"
        osheet.Range("A1").Value = "Header"
        osheet.Select()
        osheet.Range("A1", "A1").Select()
        oexcel.ActiveWindow.FreezePanes = True

        'third
        If oexcel.Application.Sheets.Count() < 3 Then
            osheet = CType(obook.Worksheets.Add(), Excel.Worksheet)
        Else
            osheet = oexcel.Worksheets(3)
        End If
        osheet.Name = "Payroll Summary Report"

        'next
        If oexcel.Application.Sheets.Count() < 4 Then
            osheet = CType(obook.Worksheets.Add(), Excel.Worksheet)
        Else
            osheet = oexcel.Worksheets(4)
        End If
        osheet.Move(After:=obook.Worksheets(obook.Worksheets.Count))
        osheet.Name = "Weekly Payroll Report - Week 1"


        obook.SaveAs(fnExc)
        obook.Close()
        obook = Nothing

No comments:

Post a Comment