用户点击“否”时出错通过VB.NET替换现有的excel文件 [英] Error when user clicks on "NO" to replace existing excel file through VB.NET

查看:124
本文介绍了用户点击“否”时出错通过VB.NET替换现有的excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vb.net中有以下代码



Dim xlApp As New Excel.Application()

Dim xlWorkBook As Excel.Workbook

xlWorkBook = xlApp.Workbooks.Add

xlWorkBook.SaveAs(C:\ABC.xlsx)



当文件已经存在于C:\ ABC.xlsx时,它会提示文件已经存在。你要替换它吗?当我点击是然后它工作正常。但是当我点击NO按钮然后它会抛出错误来自HRESULT的异常:0x800A03EC。

我不想将DisplayAlerts属性设置为False,因为我希望用户知道该文件已经存在。

如何处理错误?



我尝试过:



仅当我设置DisplayAlerts = False时才有效。但我不想这样做。

解决方案

在使用Excel时,这个错误很常见 - 如果您尝试取消,则会出现相同的错误。其他人有使用Excel互操作执行其他操作的相同(无用)错误消息。



我建议你自己检查一下文件并显示您自己的错误消息。例如

 Dim xlApp As New Excel.Application()
Dim xlWorkBook As Excel.Workbook
xlWorkBook = xlApp.Workbooks.Add

Dim fileName As String = C:\Temp \ABC.xlsx
Dim vRes As DialogResult = DialogResult.Yes

If File.Exists(fileName)Then
vRes = MessageBox .Show( String .Format( 一个名为'的文件{0}'已存在于此位置。是否要替换它?,fileName),_
Microsoft Excel,MessageBoxButtons.YesNoCancel,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button2)
xlApp.DisplayAlerts = False
End if

If vRes = DialogResult.Yes然后
xlWorkBook.SaveAs(fileName)
结束如果
xlWorkBook.Close()
xlApp.Quit()



请注意,如果文件已经存在,我只关闭DisplayAlerts。


I have below code in vb.net

Dim xlApp As New Excel.Application()
Dim xlWorkBook As Excel.Workbook
xlWorkBook = xlApp.Workbooks.Add
xlWorkBook.SaveAs("C:\ABC.xlsx")

When the file already exists at "C:\ABC.xlsx" then it prompts "file already exists. Do you want to replace it ?" When I clicked on Yes then it works fine. But when i clicked on NO button then it throws error "Exception from HRESULT: 0x800A03EC".
I don't want to set DisplayAlerts property to False, as i want the user to aware that file already exists.
How to handle the error ??

What I have tried:

It works only when I set DisplayAlerts = False. But I don't want to do it.

解决方案

That error is unfortunately quite common when using Excel - you get the same error if you try to Cancel as well. Other people have had the same (unhelpful) error message doing other things with Excel interop.

Rather than trying to fix Excel I suggest that you do your own check for the file and display your own error message. For example

Dim xlApp As New Excel.Application()
Dim xlWorkBook As Excel.Workbook
xlWorkBook = xlApp.Workbooks.Add

Dim fileName As String = "C:\Temp\ABC.xlsx"
Dim vRes As DialogResult = DialogResult.Yes

If File.Exists(fileName) Then
    vRes = MessageBox.Show(String.Format("A file named '{0}' already exists in this location. Do you want to replace it?", fileName), _
                           "Microsoft Excel", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2)
    xlApp.DisplayAlerts = False
End If

If vRes = DialogResult.Yes Then
    xlWorkBook.SaveAs(fileName)
End If
xlWorkBook.Close()
xlApp.Quit()


Note that I only turn off DisplayAlerts if the file already exists.


这篇关于用户点击“否”时出错通过VB.NET替换现有的excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆