public void MergeDoc(string outPutFilePath, params string[] filePath) { object oMissing = System.Reflection.Missing.Value; object oObject = System.Reflection.Missing.Value; Word.ApplicationClass oWord = new Word.ApplicationClass(); Word._Document oDoc = null; oWord.Visible = false; Word.Documents oDocs = oWord.Documents; try { oDoc = oDocs.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); foreach (string fs in filePath) { if (File.Exists(fs)) { oWord.Selection.InsertFile(fs, ref oObject, ref oObject, ref oObject, ref oObject); object obj = Word.WdBreakType.wdPageBreak; //oWord.Selection.InsertBreak(ref obj); } } object objFinalFile = (object)outPutFilePath; oDoc.SaveAs(ref objFinalFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); } catch (Exception e) { } finally { oDoc.Close(ref oMissing, ref oMissing, ref oMissing); } }
// Show explorer form public void showExplorerForm(Word.Documents documents) { try { if (explorerForm == null) { explorerForm = new ExplorerForm(documents, OKMDocumentType.TYPE_WORD, configXML, docXML); } explorerForm.Show(); explorerForm.startUp(); } catch (Exception e) { throw e; } }
private string GetDocumentText(object path, int tryCount) { if (tryCount++ > 1) { return(string.Empty); } Word.Documents documents = null; try { if (_MSWord == null) { if (!LoadWordInstance()) { return(string.Empty); } } documents = _MSWord.Documents; } catch (InvalidCastException exception) { _tracer.TraceException(exception); return(GetDocumentTextWithReloadingWord(path, tryCount)); } catch (System.Runtime.InteropServices.COMException exception) { _tracer.TraceException(exception); if (IsReloadingPossible(exception)) { return(GetDocumentTextWithReloadingWord(path, tryCount)); } return(string.Empty); } catch (Exception exception) { _tracer.TraceException(exception); return(string.Empty); } Word.Document doc = null; string bodyText = string.Empty; object pass = "******"; try { DisableAutomationSecurity(); doc = documents.Open(ref path, ref FALSE, ref TRUE, ref FALSE, ref pass, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue); Word.Range range = doc.Content; bodyText = range.Text; } catch (System.Runtime.InteropServices.COMException exception) { _tracer.TraceException(exception); if (IsReloadingPossible(exception)) { _tracer.Trace("Try to load new instance of MS Word"); return(GetDocumentTextWithReloadingWord(path, tryCount)); } if (COM_Error.CouldNotOpenMacroStorage(exception)) //This exception leads to not closed documents { LoadWordInstance(); return(string.Empty); } } catch (InvalidCastException exception) { _tracer.TraceException(exception); return(GetDocumentTextWithReloadingWord(path, tryCount)); } catch (Exception exception) { _tracer.TraceException(exception); return(string.Empty); } RestoreAutomationSecurity(); try { if (doc != null) { doc.Close(ref FALSE, ref MissingValue, ref MissingValue); } return(bodyText); } catch (System.Runtime.InteropServices.COMException exception) { if (IsReloadingPossible(exception)) { return(GetDocumentTextWithReloadingWord(path, tryCount)); } } catch (InvalidCastException exception) { _tracer.TraceException(exception); return(GetDocumentTextWithReloadingWord(path, tryCount)); } catch (Exception exception) { _tracer.TraceException(exception); } return(string.Empty); }