Solved

Excel Interop - Problem with Autofit

Posted on 2007-12-02
4
3,738 Views
Last Modified: 2013-11-26
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
Comment
Question by:DanJourno
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20392430
Make sure the case is exactly the same when using InterOp
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20392434
In this case, it's AutoFit
0
 
LVL 5

Author Comment

by:DanJourno
ID: 20392441
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
 
LVL 5

Author Comment

by:DanJourno
ID: 20392536
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now