【问题标题】:COM Exception when using Range.InsertParagraphAfter()使用 Range.InsertParagraphAfter() 时出现 COM 异常
【发布时间】:2017-02-06 21:10:08
【问题描述】:

大家好,下午好(阿根廷时间),我收到了一个项目,系统根据客户的一些要求来格式化一个 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: 

有趣的事情:

  1. 当我使用新文件时它不会崩溃。
  2. 当我手动编写内容时,它不会崩溃。
  3. 当我复制并粘贴内容或部分内容时,它会崩溃。
  4. 我删除新文档中的内容再试一次,崩溃了。

我认为这与文档原始文档中的单词或段落格式有关。因为它只发生在我使用原始文档的内容时。但我还不确定发生了什么,或者如何处理这个,或者如何解决这个问题。由于该异常不是那么具有描述性(我知道,我并不总是会收到我需要的特定信息或错误消息),但我已经在网上搜索以查看是否有人有相同、相关或不相关的类型错误。

我们将非常感谢任何帮助或指导,感谢大家花时间阅读和理解代码。

最诚挚的问候 道格拉斯·利卡姆

【问题讨论】:

    标签: c# office-interop interopservices


    【解决方案1】:

    我在 VS2010 上工作,但后来我尝试在 VS2013 中打开并运行该解决方案的那部分并且我工作了。所以我必须假设VS2013能够识别15版office中的元素并且它不会引发异常。

    感谢您的阅读。

    【讨论】:

      猜你喜欢
      • 2011-02-22
      • 2020-08-19
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      相关资源
      最近更新 更多