SaveCopyAs 方法不起作用
(我的英语水平很差,所以首先请原谅我的解释不好:D)
我通过我的应用程序打开一个 Excel 文件。
我在 Excel 中有一个加载项,在功能区中有一个按钮用于保存(正是“保存”按钮执行的保存操作)按钮的单击事件代码如下:
Globals.ThisAddIn.Application.ActiveWorkbook.Save();
<在我的应用程序中,我将一个方法(称为 WorkbookBeforeSave)分配给工作簿的“BeforeSave”事件处理程序,该处理程序将工作簿手动保存在我的自定义目录中。
private void WorkbookBeforeSave(bool saveasui, ref bool cancel)
{
_excelApp.EnableEvents = false;//_excelApp is my Excel Application
if (!_excelWorkbook.Saved)//_excelWorkbook is Active Excel Workbook
{
_excelWorkbook.SaveCopyAs(_savedFilePath);//_savedFilePath is my custom directory
_excelWorkbook.Saved = true;
}
cancel = true;
_excelApp.EnableEvents = true;
}
问题是当我单击原始 Excel 保存按钮“SaveCopyAs”方法正常工作,但当单击我的自定义保存按钮“SaveCopyAs”方法不起作用。 (没有抛出异常,所有代码均已编译和调试)
(I'm week in English language, so at first excuse me for bad explaining :D )
I open an excel file through my application.
I have an Addd-In in Excel and a button in ribbon for save (exactly such a save action that Save button do) code of Click event of button is here:
Globals.ThisAddIn.Application.ActiveWorkbook.Save();
In my application I assign a method (called WorkbookBeforeSave) to "BeforeSave" event handler of workbook that save workbook manually in my custom directory.
private void WorkbookBeforeSave(bool saveasui, ref bool cancel)
{
_excelApp.EnableEvents = false;//_excelApp is my Excel Application
if (!_excelWorkbook.Saved)//_excelWorkbook is Active Excel Workbook
{
_excelWorkbook.SaveCopyAs(_savedFilePath);//_savedFilePath is my custom directory
_excelWorkbook.Saved = true;
}
cancel = true;
_excelApp.EnableEvents = true;
}
problem is when I click Original Excel Save Button "SaveCopyAs" method works correctly but when click on my custom Save Button "SaveCopyAs" method does not work.
(no exception has thrown and all of codes compiled and debugged)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
try-catch
进行调试,它应该对你有帮助。如果没有线程/任务,office 2007 excel - 有时 - 在加载和保存方法时会比正常情况滞后或慢。
简单的
saveAs()
方法有很多参数(搜索它,您也可以在 msdn 和此处看到它),并且它需要对路径进行一些限制,因为最新的保存路径更改为C#使用的保存路径。我更喜欢 SaveCopyAs 解决方案,因为只有一个参数并且速度快。狂怒。 :D
Try to debug with
try-catch
, it should be helpful for you.Without threading/tasking the office 2007 excel - sometime - lagging or slower than normal at loading and at the save method.
The simple
saveAs()
method have lot of parameters (searh for it, you see it on msdn & here too) and its needs some hoak at the path, because the latest save-path change to the C# used save path.I prefer the
SaveCopyAs
solution, because just one parameter and fast & furious. :D