我们拥有使用模板创建的文档。在我们的应用程序中,可以查看文档,其中包含单词自动化Open方法,如下所示。
当用户关闭单词时,将始终显示“保存”对话框("想要将更改保存到.dotx?"),即使未对主文档进行任何更改,也会发生这种情况。永远不会触及模板部分。
我们如何才能停止显示此对话框?
代码:
public class WordHelper
{
private Word._Application _app = null;
public WordHelper(bool visible = false, bool maximize = false)
{
try
{
_app = new Word.Application();
_app.Application.Visible = visible;
if (maximize)
_app.Application.WindowState = Word.WdWindowState.wdWindowStateMaximize;
else
_app.Application.WindowState = Word.WdWindowState.wdWindowStateNormal;
_app.Application.Caption = Guid.NewGuid().ToString().ToLowerInvariant().Trim();
_pid = UtilityHelper.GetPidByWindowTitle(_app.Application.Caption);
}
catch (Exception ex)
{
ScreenHelper.DisplayException(ex, "Error on launch of Word document generation", this.GetType().Name, new StackFrame().GetMethod().Name);
}
}
public void OpenDocument(string fileName, bool readOnly, string readPassword, string writePassword)
{
try
{
if (!string.IsNullOrWhiteSpace(fileName))
{
_app.Documents.Open(
fileName,
Type.Missing,
readOnly,
false,
readPassword,
Type.Missing,
Type.Missing,
writePassword,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing);
_app.Application.Caption = fileName;
}
}
catch (Exception ex)
{
throw new Exception("Error in OpenDocument" + ex.Message);
}
}
}