Hello Guys こんにちは (アルゼンチン時間)。私は、システムがクライアントからの要件に従ってワード文書をフォーマットするプロジェクトを受け取りました。受け取った 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:
興味深いこと:
- 新しいファイルを使用してもクラッシュしません。
- 手でコンテンツを書いてもクラッシュしません。
- コンテンツまたはコンテンツの一部をコピーして貼り付けると、クラッシュします。
- 新しいドキュメントのコンテンツを削除して再試行すると、クラッシュします。
これは、元のドキュメントの単語または段落の書式設定に関連するものだと思います。元のドキュメントのコンテンツを使用した場合にのみ発生するためです。しかし、何が起こっているのか、これをどのように処理するのか、またはこれを回避する方法はまだわかりません. 例外はそれほど説明的ではないため(必要な特定の情報やエラーメッセージを常に受け取るとは限りません)、すでにWebを検索して、誰かが同じ、関連する、または無関係な種類を持っているかどうかを確認していますエラーの。
コードを読んで理解してくれてありがとう。
敬具 ダグラス・リーカム