We help IT Professionals succeed at work.
Get Started

insert a blank row into an excel workbook which has been genereated in vb.net

821 Views
Last Modified: 2012-06-27
I've a vb app which generates a number of excel spreadsheets, however I'm trying to insert a number of blank rows
at certain per-defined points using

         .Rows("5:5").Select()
                .Selection.insert(Excel.XlDirection.xlDown)

 onto one of the sheets but I'm getting the following message

Select method of Range Class Failed

I'm not going to send all the code it, it a couple of thousand lines but the following works

-------------------------------------------------------------------------------------------------
Imports System.Configuration
Imports System.Xml
Imports Excel = Microsoft.Office.Interop.Excel
________________________________________________________________

 Public Sub ExportToExcel()

        'New - load the dataset data
        InitialiseDataSets()

        Dim excelApp As Object
        excelApp = CreateObject("Excel.Application")

        Try

            Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add
            Dim oSheet As Excel.Worksheet

            oSheet = excelBook.Worksheets.Add

          oSheet = excelBook.Worksheets.Add
          oSheet = excelBook.Worksheets.Add            

            oSheet = excelBook.Worksheets("Sheet6")
            'rename
            oSheet.Name = ("Summary")


            Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
            excelApp.Visible = True

            With excelWorksheet
'Code to formate this sheet
end with
__________________________________________________________________________

however  when I add the two lines below


Public Sub ExportToExcel()

        'New - load the dataset data
        InitialiseDataSets()

        Dim excelApp As Object
        excelApp = CreateObject("Excel.Application")

        Try

            Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add
            Dim oSheet As Excel.Worksheet

            oSheet = excelBook.Worksheets.Add

          oSheet = excelBook.Worksheets.Add
          oSheet = excelBook.Worksheets.Add            

            oSheet = excelBook.Worksheets("Sheet6")
            'rename
            oSheet.Name = ("Summary")


            Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
            excelApp.Visible = True

            With excelWorksheet
'Code to formate this sheet

'Two new lines here
         .Rows("5:5").Select()
                .Selection.insert(Excel.XlDirection.xlDown)

end with
 ____________________________________________________________

I'm getting the error message.

Any help would be much appreciated.
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE