Hello Guys good afternoon (argentina time), i had recieved a project where the system formats a word document according to some requirements from the client. The word document that i recieve is on .doc and and the word document that i export or create under a different format or internal design is .docx. The core thing is that this same code used to work with Office12 assemblies, but when i added the new assemblies which are Office15, It crashed.
The sample code that i am using for the portion of the proyect where the flow crashes is the following:
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();
}
The exception message is:
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:
Interesting things:
- when i use a new file it doesnt crash.
- when i write the content by hand it doesnt crash.
- when i copy and paste the content or part of the content it crashes.
- I delete the content in the new document and try it again, crashes.
I believe it is something related with the formatting of the words or of the paragraphs in the document original document. Since it only happens when i use the content of the original document. But i am not sure yet what is going on, or how to handle this, or how to do a workaround of this. Since that the exception is not so descriptive (i know, i am not always going to recieve the specific information or error message that i need) but i already have search the web looking to see if someone have had the same, related or unrelated kind of error.
Any help or orientation will and is highly appreciated, thank you all for your time reading and understanding the code.
Kindest Regards Douglas Leekam