COMException - Error creating Excel Workbook

Posted on 2011-05-05
Last Modified: 2012-05-11
I'm getting a COMException error when trying to create a workbook in 2005.

Any thoughts?

COMException was unhandled
Microsoft Office Excel cannot access the file 'C:\A750D100'. There are several possible reasons:

• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.

Error occurs at line: xlApp.ActiveWorkbook.SaveAs("C:\test.xlsx")

Private Sub Create_Workbook()

        Dim xlApp As New EXCEL.Application
        Dim xlWorkBook As EXCEL.Workbook = xlApp.Workbooks.Add
        Dim xlWorkSheet As EXCEL.Worksheet = CType(xlWorkBook.Worksheets.Add, EXCEL.Worksheet)
        Dim strFileName As String = ""

        Dim misValue As Object = System.Reflection.Missing.Value

        xlWorkSheet.Name = Me.txt_Slip_Number.Text
        xlWorkSheet.Cells(1, 1) = Me.DatePick_Slip_Date.Value.ToString
        xlWorkSheet.Cells(1, 2) = Me.txt_Location.Text
        xlWorkSheet.Cells(1, 3) = "Job No:"
        xlWorkSheet.Cells(1, 4) = Me.txt_Job_Number.Text
        xlWorkSheet.Cells(1, 5) = "Engineer: "
        xlWorkSheet.Cells(1, 6) = Me.txt_Engineer.Text
        xlWorkSheet.Cells(2, 1) = "Contractor: "
        xlWorkSheet.Cells(2, 2) = Me.txt_contractor.Text
        xlWorkSheet.Cells(2, 3) = "SWO#: "
        xlWorkSheet.Cells(2, 4) = Me.txt_SWO_ID.Text




        MsgBox("Excel file created , you can find the file c:\")
    End Sub

Open in new window

Question by:tobin46
    LVL 1

    Accepted Solution

    Got it....found MSDN article I did not see the first time around.  I like this approach better.

    Private Sub Create_Excel_WB()
            Dim oXL As EXCEL.Application
            Dim oWB As EXCEL.Workbook
            Dim oSheet As EXCEL.Worksheet
            Dim oRng As EXCEL.Range
            ' Start Excel and get Application object.
            oXL = CreateObject("Excel.Application")
            oXL.Visible = True
            ' Get a new workbook.
            oWB = oXL.Workbooks.Add
            oSheet = oWB.ActiveSheet
            ' Add table headers going cell by cell.
            oSheet.Cells(1, 1).Value = Me.DatePick_Slip_Date.Value.ToString
            oSheet.Cells(1, 2).Value = Me.txt_Location.Text
            oSheet.Cells(1, 3).Value = "Job No: "
            oSheet.Cells(1, 4).Value = Me.txt_Job_Number.Text
            oSheet.Cells(1, 5).value = "Engineer: "
            oSheet.Cells(1, 6).value = Me.txt_Engineer.Text
            oSheet.Cells(2, 1).value = "Contractor: "
            oSheet.Cells(2, 2).value = Me.txt_contractor.Text
            oSheet.Cells(2, 3).value = "SWO#: "
            oSheet.Cells(2, 4).value = Me.txt_SWO_ID.Text
            ' Format A1:D1 as bold, vertical alignment = center.
            With oSheet.Range("A1", "D1")
                .Font.Bold = True
                .VerticalAlignment = EXCEL.XlVAlign.xlVAlignCenter
            End With
            oSheet.Name = Me.txt_Slip_Number.Text
            ' AutoFit columns A:D.
            oRng = oSheet.Range("A1", "D1")
            ' Make sure Excel is visible and give the user control
            ' of Excel's lifetime.
            oXL.Visible = True
            oXL.UserControl = True
            ' Make sure that you release object references.
            oRng = Nothing
            oSheet = Nothing
            oWB = Nothing
            oXL = Nothing
            Exit Sub
            MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
        End Sub
    End Class

    Open in new window

    LVL 12

    Expert Comment

    Hello, tobin46,

    Your code worked fine for me (although I'm using the ancient version of XL2K).  

    I'm assuming that there is nothing obviously "funny" that is preventing you from accessing the root of the C: drive.

    As a suggestion, you might try adding the line:

                xlApp.Visible = True

    just before your "SaveAs" statement.  The set a breakpoint on the SaveAs statement and when the program stops there, execute the SaveAs command from Excel's file menu instead of from the program.  Perhaps you will get a more informative error message that way.

    LVL 1

    Author Comment

    @Randy:  I'm sorry, looks like our comments crossed streams...I found a good article on MSDN.

    I think the xlApp.Visible = True may have made the difference though...

    Thanks for your time.
    LVL 12

    Expert Comment

    Hi, tobin46,

    Glad to know that you're on your way.



    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    732 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

    24 Experts available now in Live!

    Get 1:1 Help Now