Microsoft.Office.Interop.Word.Document不会提示将更改保存到没有_Application.Quit()的单个文档中 [英] Microsoft.Office.Interop.Word.Document won't prompt to save changes to single document without _Application.Quit()

查看:1659
本文介绍了Microsoft.Office.Interop.Word.Document不会提示将更改保存到没有_Application.Quit()的单个文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开始之前,我已经把它转移到死亡,还有很多关于如何防止保存提示的帖子。我有一个显示保存提示的问题。



我正在C#中构建文档生成系统的模板编辑部分。系统将编辑点和点文件。在概述问题之前,我用于开发的环境正在运行Visual Studio 2010和Word 2010.它将最终在其他版本上运行,但我希望先获得这些版本。



要设置场景,我有一个表单打开,该表单具有从存储过程(数据源)返回的列的列表,以将该文档添加为书签。我拥有所有的书签和拖放功能。当我关闭应用程序时,我抓住ApplicationEvents4_DocumentBeforeCloseEventHandler事件关闭窗体。



当我关闭窗体时,我检查了许多文档是打开的。如果只打开一个文档,我关闭提示用户保存更改的应用程序。然而,如果有多个文档打开(大多数人都有许多不同的文字文档同时打开),我找到正确的文档,并将其设置为关闭,以提示用户保存更改。



这是问题发生的地方。在这一点上,保存更改对话框永远不会显示出来,所有的Visual Studio都会冻结。如果我在Visual Studio 2010上停止调试,文档将无限期地闪烁到任务栏中,如果您专注于它,它会消失,并保存更改而不提示。



是处理表单关闭事件的代码:

  private void Form2_FormClosing(object sender,FormClosingEventArgs e)
{
if(app!= null)
{
if(app.Documents.Count< 2)
{
this.TopMost = false;
((Word._Application)app).Quit();
app = null;
}
else
{
foreach(app.Documents中的Word.Document文档)
{
if(document.FullName.Equals(wordDoc.FullName ))
{
对象saveChanges = Word.WdSaveOptions.wdPromptToSaveChanges;
((Word._Document)wordDoc).Close(ref saveChanges);
break;
}
}
}
}
}

问题是这一行应该显示一个保存更改对话框:

 ((Word._Document)wordDoc).Close (ref saveChanges); 

我已经尝试调试这个没有太多的运气。在这条线上和

 断点处放置一个断点; 

行允许程序在关闭行停止,但是当您前进或继续的单词变得没有反应,下一行的表单和断点也不会被击中。



任何帮助将非常感谢,因为这个简单的东西是如此烦人

解决方案

为避免提示或获取提示您必须设置分别保存属性为true或false:

  var doco = WordApp.Documents.Add() ; 
doco.Saved = true;
doco.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges,Type.Missing,Type.Missing);

当您尝试关闭文档时,如果Word挂在代码行上,会发生什么事情。我建议妥善处理所有资源。以下是关于使用VSTO Contrib帮助提供此功能的好文章:



http://jake.ginnivan.net/vsto-com-interop



更新:



通过在系统环境变量中添加以下内容来启用VSTO日志文件:



NAME:VSTO_LOGALERTS
VALUE:1
可能是异常错误,这就是为什么您的加载项未加载。



您可以查看此源了解更多信息VSTO记录和警报,但实质上您根据需要做的更改两个环境变量值:



显示VSTO警报提示 VSTO_SUPPRESSDISPLAYALERTS变量设置为0(零),以显示消息框中的每个错误。您可以通过将变量设置为1(一)来抑制
消息。


将VSTO警报记录到日志文件


要将错误写入日志文件,请将VSTO_LOGALERTS变量设置为
1(一) 。


Visual Studio Tools for Office在包含应用程序清单的文件夹中创建日志文件。默认名称为.manifest.log。要停止记录错误,请将变量设置为0(零)。


Before I begin, I have googled this to death and there are many posts about how to prevent the save prompt. I am having an issue showing the save prompt.

I am building the template editing portion of a document generation system in C#. The system will edit 'dot' and 'dotx' files. Before outlining the problem, the environment I am using for development is running Visual Studio 2010 and Word 2010. It will eventually run on other versions, but i would like to get these versions functional first.

To set the scene I have a form open that has a list of columns returned from a stored procedure(Data Source) to add to the document as bookmarks. I have all of the bookmark and drag/drop operations functional. When I close the application, I catch the 'ApplicationEvents4_DocumentBeforeCloseEventHandler' event to close the form.

When I close the form, i check haw many documents there are open. If only one document is open, I close the application which prompts the user to save changes. If however there are multiple documents open (Most people have a number of different word documents open concurrently), I locate the correct document and close it with the flag set to prompt the user to save changes.

This is where the problem occurs. At this point, the save changes dialog never shows up and everything freezes up in Visual Studio. If I stop the debugging on Visual Studio 2010 the document flashes in the task bar indefinitely, and if you focus on it, it disappears and saves changes without a prompt.

This is the code to handle the form closing event:

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (app != null)
        {
            if (app.Documents.Count < 2)
            {
                this.TopMost = false;
                ((Word._Application)app).Quit();
                app = null;
            }
            else
            {
                foreach (Word.Document document in app.Documents)
                {
                    if (document.FullName.Equals(wordDoc.FullName))
                    {
                        object saveChanges = Word.WdSaveOptions.wdPromptToSaveChanges;
                        ((Word._Document)wordDoc).Close(ref saveChanges);
                        break;
                    }
                }
            }
        }
    }

The problem is this line should show a save changes dialog:

((Word._Document)wordDoc).Close(ref saveChanges);

I have tried debugging this without much luck. Putting a breakpoint on this line and on the

break;

line allows the program to stop at the 'Close' line but when you 'step' forward or 'continue' word becomes unresponsive, so does the form and the breakpoint on the very next line never gets hit.

Any help would be greatly appreciated as something this simple is so annoying to get stuck on.

解决方案

To avoid the Prompt or to get the Prompt to you have to set the Saved property to true or false respectively:

var doco = WordApp.Documents.Add();
doco.Saved = true;
doco.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges, Type.Missing, Type.Missing);

Something fishy is going on if Word hangs on the line of code when you try to close the document. I recommend disposing of all the resources properly. Here is a great article on using VSTO Contrib that helps provide this functionality:

http://jake.ginnivan.net/vsto-com-interop

Update:

Enable your VSTO log file by adding the following on your system environment variables:

NAME: VSTO_LOGALERTS VALUE: 1 There might be an exception error that is why your add-in is not loading.

You can check this source for more info on VSTO logging and alerts, but in essence you change two environment variable values depending on what you need to do:

Displaying VSTO Alert Prompts

To display each error in a message box, set the VSTO_SUPPRESSDISPLAYALERTS variable to 0 (zero). You can suppress the messages by setting the variable to 1 (one).

Logging VSTO Alerts to a Log file

To write the errors to a log file, set the VSTO_LOGALERTS variable to 1 (one).

Visual Studio Tools for Office creates the log file in the folder that contains the application manifest. The default name is .manifest.log. To stop logging errors, set the variable to 0 (zero).

这篇关于Microsoft.Office.Interop.Word.Document不会提示将更改保存到没有_Application.Quit()的单个文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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