问题描述
这是Word VSTO加载项。它具有将RTF文件插入当前打开的文档的功能。为了最大限度地提高性能,我不使用其他(不可见的)word实例打开RTF文件。相反,我使用Word的当前实例将RTF作为不可见的文档打开,读取其XML,然后将其插入到当前插入点的文档中。
我正在使用Opens the specified document and adds it to the Documents collection. Returns a Document object.的Documents.Open
方法。因此,我期望文档集合最初将最初打开Word doc。然后,当我的加载项打开RTF文件时。它将其作为doc#2添加到Documents集合中。
在大多数情况下,这种方法行之有效,即问题是断断续续的。外接程序会在调试器中正常运行,并且长时间在Word中使用发行版时也会运行,然后,有时,打开RTF文件时,外接程序会挂起。在调试器中,出于调试目的,我将RTF的打开更改为可见,并且在这种状态下,我可以看到问题是,当打开RTF文件时,打开的原始文档从Documents集合中消失了,RTF文件变成了文档集合中的第一个也是唯一的文档。因此,当然,当我打算将RTF XML加载到最初打开的文档中时,它会崩溃,因为原来打开的文档是从Documents集合中删除的。
序列:
我希望添加RTF文件,并使其成为集合中的第二个。相反,它似乎替代了最初打开的文档
打开RTF之前
打开RTF后
相关代码
object refMissing = System.Reflection.Missing.Value;
//deselect all text
direction = Word.WdCollapseDirection.wdCollapseStart;
Globals.ThisAddIn.Application.Selection.Collapse(ref direction);
// Insert template text at insertion point
Word.Selection objTargetSelectionInsertionPoint = Globals.ThisAddIn.Application.Selection;
if (Globals.ThisAddIn.Application.Options.Overtype)
{
Globals.ThisAddIn.Application.Options.Overtype = false;
}
// Test to see if selection is an insertion point.
if (objTargetSelectionInsertionPoint.Type == Word.WdSelectionType.wdSelectionIP)
{
Word.Document objTempWordDocument = Globals.ThisAddIn.Application.Documents.Open(Visible: true,FileName: fqTempFileName,ReadOnly: false);
objTempWordDocument.Content.Select();
Word.Range objSourceRange = objTempWordDocument.ActiveWindow.Selection.Range;
objTargetSelectionInsertionPoint.Range.InsertXML(objSourceRange.XML);
objTargetSelectionInsertionPoint.EndKey(WdUnits.wdStory);
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
objTempWordDocument.Close(ref doNotSaveChanges,ref refMissing,ref refMissing);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)