【VSTO】Office开发中遇到的兼容性检查问题

来源:互联网 发布:brew install node 6 编辑:程序博客网 时间:2024/06/02 16:56

事情是这样的:

 

我们的一个应用程序需要通过VSTO代码方式操作本地的Excel,包括打开工作簿,生成新的工作簿,设置数据,并且保存为新文件等等。

该程序在Excel 2003的环境下没有任何问题,但是在Excel 2007或者是Excel 2010中就老是遇到中途被卡住的状况。

究其原因,是因为我们想将文件继续保存为Excel 2003格式,但Excel 2007或者Excel 2010默认会有一个兼容性检查的功能。如下

image

如果将“Check compatiblity when saving this workbook”的选项关闭,则问题可以解决。

 

但是,如果你无法确保用户那边关闭了该选项,也可以通过程序的方式临时关闭检查

有两种方式实现这样的需求

第一种:在Application级别关闭警告

        If (app Is Nothing) Then            app = New Excel.Application            app.Interactive = False            app.UserControl = False            app.DisplayAlerts = False            app.Visible = False            app.ScreenUpdating = False        End If
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

 

第二种:在Workbook级别关闭警告

wb.CheckCompatibility=False
 
 
还有一个情况就是,如果你关闭了警告(不管是手工地,还是代码关闭),却又想恢复该选项。要按照下面这样做
 
image
在上面的对话框中,点击“Check Compatibility”,你将可以继续勾选上这个选项。
image
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }