writting to Excel spreadsheet using vb.net

I am trying to open and write to an existing Excel worksheet but I get the error "ComExecption was unhandled, Exception from HRESULT: 0x800A03EC"  when I try to write to a cell.
I haven't been able to find out what this means.
 Imports Excel = Microsoft.Office.Interop.Excel

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim DGV1 As DataGridView = DataGridView1
        Dim app As New Excel.Application
        Dim wb As Excel.Workbook = app.ActiveWorkbook
        'Dim sht As Excel.Worksheet
        Dim NumCols As Integer = DGV1.Columns.Count
        Dim filePath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Master.xls")

        wb = app.Workbooks.Open(filePath)
        app.Visible = True

        'copy header names into spreadsheet.
        For i As Integer = 0 To NumCols
            For Each col As DataGridViewColumn In DataGridView1.Columns
                wb.Sheets(1).Cells(i + 6, 0) = col.HeaderText
            Next
        Next
    End Sub

Open in new window

All the code examples that I have found seem fairly similar to my code.  I'm guessing I made a simple mistake but I can't figure out where.  Any and all help is welcome.
NevSoFlyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NevSoFlyAuthor Commented:
Thanks that was a stupid oversight on my part, but it didn't solve my problem.  I am using the nested loops to copy data from a datagridview into the appropriate cells in the worksheet.
0
NevSoFlyAuthor Commented:
To be more accurate I'm trying to transfer over the column names from the datagrid to the spreadsheet and I needed to make sure that any changes to the datagrid was reflected in the spreadsheet.
0
anarki_jimbelSenior DeveloperCommented:
Disregard my previous comment. I've deleted it.

Do not use zero index. Start from "1". I tried - it works,.

        Dim worksheet As Excel.Worksheet = wb.Sheets(1)

        'copy header names into spreadsheet.
        For i As Integer = 1 To NumCols
            For Each col As DataGridViewColumn In DataGridView1.Columns

                worksheet.Cells(i + 6, 1) = col.HeaderText
            Next
        Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

NevSoFlyAuthor Commented:
I still get the same error.  I commented out the loops and just used:

wb.Sheets(1).Cells(6, 0) = "xxxxx"

and I still get the same error.
0
anarki_jimbelSenior DeveloperCommented:
Please read carefully: "Do not use zero index."

In my code above there are no '0' indexes. Not for sheets, not for cells. '0' index causes the error.
0
NevSoFlyAuthor Commented:
thank you very much I did misread your post.
0
anarki_jimbelSenior DeveloperCommented:
Glad to help! :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.