使用Range.InsertParagraphAfter()时出现异常
本文关键字:异常 Range InsertParagraphAfter 使用 | 更新日期: 2023-09-27 18:09:12
大家下午好(阿根廷时间),我收到了一个项目,系统根据客户的一些要求格式化一个word文档。我接收到的word文档是。doc,而我导出或创建的word文档在不同的格式或内部设计下是。docx。核心问题是,同样的代码用于Office12程序集,但是当我添加新的Office15程序集时,它崩溃了。
示例代码,我正在使用的项目,其中流崩溃的部分如下:
public void Prueba()
{
// Open word and a docx file
var wordApplication = new Application() { Visible = false };
string strPath=@"<Path>" ;
string strNombreArchivo = "<Name of the doc>.doc";
string strddd = "Foo.docx";
var docOriginal = wordApplication.Documents.Open(strPath+strNombreArchivo, ReadOnly: true);
//var docddd = wordApplication.Documents.Open(strPath + strddd, ReadOnly: false);
var docNuevo = wordApplication.Documents.Add(DocumentType: WdDocumentType.wdTypeDocument);
docOriginal.Content.Copy();
docNuevo.Content.Paste();
var docAUsar = docNuevo;
Range searchRange = docAUsar.Range();
searchRange.Find.ClearFormatting();
int nextSearchStart = searchRange.Start;
searchRange.Start = nextSearchStart;
Paragraph ParagraphParrafo = searchRange.Paragraphs.First;
nextSearchStart = ParagraphParrafo.Range.End;
String titleText = ParagraphParrafo.Range.Text;
try
{
// This is the point where the code crashes.
searchRange.InsertParagraphAfter();
docAUsar.Save();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
// Close word
wordApplication.Quit();
this.Close();
}
异常消息为:
System.Runtime.InteropServices.COMException was unhandled
HelpLink=wdmain11.chm#37373
HResult=-2146823683
Message=This method or property is not available because this command is not available for reading.
Source=Microsoft Word
ErrorCode=-2146823683
StackTrace:
en Microsoft.Office.Interop.Word.Range.InsertParagraphAfter()
en WindowsFormsApplication3.Form1.Prueba() en C:'Users'Dleekam'Google Drive'Proyectos'WindowsFormsApplication3'WindowsFormsApplication3'Form1.cs:línea 58
en WindowsFormsApplication3.Form1.Form1_Load(Object sender, EventArgs e) en C:'Users'Dleekam'Google Drive'Proyectos'WindowsFormsApplication3'WindowsFormsApplication3'Form1.cs:línea 25
en System.Windows.Forms.Form.OnLoad(EventArgs e)
en System.Windows.Forms.Form.OnCreateControl()
en System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
en System.Windows.Forms.Control.CreateControl()
en System.Windows.Forms.Control.WmShowWindow(Message& m)
en System.Windows.Forms.Control.WndProc(Message& m)
en System.Windows.Forms.ScrollableControl.WndProc(Message& m)
en System.Windows.Forms.Form.WmShowWindow(Message& m)
en System.Windows.Forms.Form.WndProc(Message& m)
en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
有趣的事情:
- 当我使用一个新文件时,它不会崩溃。
- 当我用手写内容时,它不会崩溃。
- 当我复制粘贴内容或部分内容时,它崩溃。
- 我删除了新文档中的内容,再试一次,崩溃了。
我认为这与文档中单词或段落的格式有关。因为它只发生在我使用原始文档的内容时。但我还不确定到底发生了什么,或者如何处理这个问题,或者如何解决这个问题。因为例外是不那么描述性的(我知道,我并不总是会收到我需要的具体信息或错误信息),但我已经搜索了网页,看看是否有人有相同的,相关的或不相关的错误。
任何帮助或指导将和高度赞赏,感谢您所有的时间阅读和理解代码。
亲切的问候道格拉斯Leekam
我正在VS2010上工作,但后来我试图在VS2013中打开并运行解决方案的那一部分,我工作了。所以我必须假设VS2013可以识别office 15版本中的元素,并且不会引发异常。
感谢您的阅读。