Visual Basic - HRESULT: 0x800A03EC Error Trying to Save Excel Work

T

Tim Attaway

am porting a website from Server 2000 to Server 2008. A few of the pages on
the site create Excel spreadsheets. When those run, they are getting an
error when I try to do the "SaveAs" on the workbook. I have created a
skeleton version of one of those programs to strip out all of the
spplication-specific stuff. All it is doing is creating a workbook with a
single sheet with a value in a single cell and then trying to save it. To
eliminate any possibilities of access privileges i am running it for now with
Administrator privileges so there should be no access denied problems. I am
logging the progress to the event log so I can see just exactly how far the
code is getting. Here is the code.

=========================================================

Private Sub btnTest_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnTest.Click

Dim theEventLog As EventLog = New EventLog()
theEventLog.Source = "CCSInformation"
Dim myIdentity As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim myName As String = myIdentity.Name
Dim theFirstLog As String = "Starting Excel Test as UserName: " +
myName
theEventLog.WriteEntry(theFirstLog)
'
' Create the temporary file
'
Dim tempDirectory As String
Dim templateString As String
Dim GetRidOfFiles() As String
Dim FileToDelete As String
Dim theExcelFileName As String
tempDirectory =
HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath)
tempDirectory = Path.Combine(tempDirectory, "Excel\Temp\")
theEventLog.WriteEntry("Excel Test - About to purge old files from "
+ tempDirectory)

templateString = "TempExcel" & "*.*"
GetRidOfFiles = Directory.GetFiles(tempDirectory, templateString)
'
' Clean up any files in the temp directory that are at least an hour
old
'
For Each FileToDelete In GetRidOfFiles
If File.GetLastAccessTime(FileToDelete) <
DateTime.Now.AddMinutes(-60) Then
Try
theEventLog.WriteEntry("Excel Test - Deleting " +
FileToDelete)
File.Delete(FileToDelete)
Catch ex As Exception
theEventLog.WriteEntry("Excel Test - Error attempting to
delete " + FileToDelete)
End Try
End If
Next
theExcelFileName = tempDirectory & "TempExcel" &
Now.Month.ToString("D02") & _
Now.Day.ToString("D02") &
Now.Year.ToString("D04") & _
Now.Hour.ToString("D02") &
Now.Minute.ToString("D02") & _
Now.Second.ToString("D02") &
Now.Millisecond.ToString("D03") & ".xls"
theEventLog.WriteEntry("Excel Test - The Excel file will be " +
theExcelFileName)
If File.Exists(theExcelFileName) Then
Try
theEventLog.WriteEntry("Excel Test - Attempting to delete
Excel File")
File.Delete(theExcelFileName)
Catch ex As Exception
theEventLog.WriteEntry("Excel Test - Error attempting to
delete Excel File")
End Try
End If
'
' Generate the spreadsheet
'
theEventLog.WriteEntry("Excel Test - Starting Excel Application")
Dim xlsApp As New EXCEL9.Application()
xlsApp.Visible = False
xlsApp.SheetsInNewWorkbook = 1
'
' Create a workbook and a worksheet
'
theEventLog.WriteEntry("Excel Test - Adding workbook")
Dim newBook As EXCEL9.Workbook = xlsApp.Workbooks.Add()
theEventLog.WriteEntry("Excel Test - Adding worksheet")
Dim newSheet As EXCEL9.Worksheet = CType(newBook.Sheets(1),
EXCEL9.Worksheet)

newSheet.Select(True)
newSheet.Name = "Excel Test Sheet"

'
' Define a style
'
theEventLog.WriteEntry("Excel Test - Adding a style")
Dim styleNormalText As EXCEL9.Style
styleNormalText = newBook.Styles.Add("styleNormalText", Type.Missing)
styleNormalText.Font.Bold = False
styleNormalText.Font.Name = "Arial"
styleNormalText.Font.Size = 10
styleNormalText.NumberFormat = "@"
styleNormalText.WrapText = True
styleNormalText.HorizontalAlignment = XlHAlign.xlHAlignLeft
'
' Add a single cell to the spreadsheet
'
theEventLog.WriteEntry("Excel Test - Adding a value at cell(1,1)")
Dim theCell As EXCEL9.Range = Nothing
theCell = CType(newSheet.Cells(1, 1), Range)
theCell.Value2 = "test value"
theCell.Style = "styleNormalText"
'
' Save the workbook
'
theEventLog.WriteEntry("Excel Test - Saving the workbook")
newBook.SaveAs(theExcelFileName, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
False, _
False, _
XlSaveAsAccessMode.xlNoChange, _
XlSaveConflictResolution.xlLocalSessionChanges, _
Type.Missing, _
Type.Missing)
theEventLog.WriteEntry("Excel Test - Spreadsheet is saved. Now
close it.")

newBook.Close(True, theExcelFileName, Type.Missing)
theEventLog.WriteEntry("Excel Test - Spreadsheet is closed.
Quitting the application.")
xlsApp.Quit()
theEventLog.WriteEntry("Excel Test - Excel application is
terminated. About to download the file.")
'
' Download the file
'
Dim genFileName As String
genFileName = "attachment;filename=ExcelTest.xls"
Try
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.AddHeader("content-disposition",
genFileName)
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/ms-excel"
HttpContext.Current.Response.WriteFile(theExcelFileName)
HttpContext.Current.Response.End()
Catch exptn As Exception
Throw
End Try
theEventLog.WriteEntry("ExtractDataTrends - HTTP Response Sent")

End Sub

====================================================================

The eventlog shows that it is getting right up to point of trying to do the
"newBook.SaveAs". When it gets to that step, I get

===========================================================

Exception from HRESULT: 0x800A03EC
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Exception
from HRESULT: 0x800A03EC

Source Error:

An unhandled exception was generated during the execution of the current web
request. Information regarding the origin and location of the exception can
be identified using the exception stack trace below.

Stack Trace:


[COMException (0x800a03ec): Exception from HRESULT: 0x800A03EC]
EXCEL9.WorkbookClass.SaveAs(Object Filename, Object FileFormat, Object
Password, Object WriteResPassword, Object ReadOnlyRecommended, Object
CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution,
Object AddToMru, Object TextCodepage, Object TextVisualLayout) +0
CCS.ExcelTest.btnTest_Click(Object sender, EventArgs e) +2546
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
+140
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +29
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

============================================================

This runs fine when I test it on my local machine (which has Excel 2007
installed). It runs fine on the Windows 2000 server (which has Excel 2003).
It only fails on the Windows 2008 server (which has Excel 2007). Does anyone
have a clue what could be causing this error or how to correct it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top