gpt4 book ai didi

c# - 使用 Range.InsertParagraphAfter() 时出现 COM 异常

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:09 26 4
gpt4 key购买 nike

大家好下午好(阿根廷时间),我收到了一个项目,系统根据客户的一些要求格式化了一个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. 我删除了新文档中的内容,然后重试,崩溃。

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

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

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

最佳答案

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

感谢阅读。

关于c# - 使用 Range.InsertParagraphAfter() 时出现 COM 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39755098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com