• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3784
  • Last Modified:

Excel Interop - Problem with Autofit

I am trying to use the autofit function to fit the data into the excel columns from a vb.net application.

However, i keep getting the following error when running the Autofit function:-
{"Exception from HRESULT: 0x800A03EC"}

Many thanks
Dan
Private Sub cmdExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExport.Click
        exc = New Excel.Application()
        Dim r As Integer
        Dim c As Integer
        Dim CellsRange As Excel.Range
 
        If exc Is Nothing Then
            Console.WriteLine("ERROR: EXCEL couldn't be started")
            Exit Sub
        End If
 
        exc.Visible = True
 
        Dim workbook As Excel.Workbook = exc.Workbooks.Add
        Dim WorkSheet As Excel.Worksheet = workbook.Worksheets.Add
        For r = 0 To gridResults.Rows.Count - 1
            For c = 0 To gridResults.Columns.Count - 1
                WorkSheet.Cells(r + 2, c + 1) = gridResults.Item(c, r).Value
            Next
        Next
        For c = 0 To gridResults.Columns.Count - 1
            WorkSheet.Cells(1, c + 1) = gridResults.Columns(c).HeaderText
            WorkSheet.Cells(1, c + 1).font.bold = True
            WorkSheet.Columns(c).autofit()
 
 
        Next
 
    End Sub

Open in new window

0
DanJourno
Asked:
DanJourno
  • 2
  • 2
1 Solution
 
imitchieCommented:
Make sure the case is exactly the same when using InterOp
0
 
imitchieCommented:
In this case, it's AutoFit
0
 
DanJournoAuthor Commented:
Thanks for your reply.

I replaced the line:-
WorkSheet.Columns(c).autofit()
with
WorkSheet.Columns(c).AutoFit()

However, i get the same error.

Dan
0
 
DanJournoAuthor Commented:
Here is the solution I came to:-

I changed:
WorkSheet.Columns(c).AutoFit()
To
WorkSheet.Columns("A:Z").AutoFit()

It autofits all the columns instead of one at a time, but that doesnt both me.
Thanks for your help
Dan
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now