Question : Excel Exception from HRESULT Error

I created a small application in vb.net that will export some data from sql database into an excel spreadsheet.  Whenever it ask me that the file already exists and do I want to overwrite, if I choose "No" or "Cancel" I get the following error.

Exception from HRESULT: 0x800A03EC

Here's the last line of code that throws off the error:

Try
With

strExcelFile = strAppPath & "exp_file"
.ActiveWorkbook().SaveAs(strExcelFile)  --> Here's where it errors out if I click "No" or "Cancel"......clicking "Yes" to overwrite has no issue.
.ActiveWorkbook.Close()
End With
Catch ex As Exception
            MsgBox(ex.Message)
 End Try

Answer : Excel Exception from HRESULT Error

This is a little bit of a cheat, but you shouldn't have any problems:

        strExcelFile = strAppPath & "exp_file"

        If My.Computer.FileSystem.FileExists(strExcelFile) Then

            Dim result As DialogResult = _
                MessageBox.Show("A file named '" & strExcelFile & "' already exists in this located.  " & _
                    "Do you want to replace it?", "Microsoft Excel", MessageBoxButtons.YesNoCancel, _
                     MessageBoxIcon.Question)

            If result = Windows.Forms.DialogResult.Yes Then
                ActiveWorkbook.SaveCopyAs(strExcelFile)

            End If

        Else
            ActiveWorkbook.SaveCopyAs(strExcelFile)

        End If

        ActiveWorkbook.Close()

Random Solutions  
 
programming4us programming4us