I'm doing a web application that makes me a merge of several word documents but I need to join them without the Line break or ENTER , I'm developing it in c # I already have the merge of documents but I just need delete the line breaks , I managed to work with the library interop.word.dll works as I want but at the time of publishing it gives me an error with the interoperability of the word and I have not been able to solve that problem in my server windows 2012, for that reason I am seeing another option, Thanks in advance
This is the method that makes the merge of several word documents:
public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate, string nsec_aop, DataTable tblRequisitos)
{
object defaultTemplate = documentTemplate;
object missing = System.Type.Missing;
object pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
object outputFile = outputFilename;
object oTrue = true;
object oAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
// Create a new Word application
Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application();
try
{
// Create a new file based on our template
Microsoft.Office.Interop.Word.Document wordDocument = wordApplication.Documents.Add(
ref missing
, ref missing
, ref missing
, ref missing);
// Make a Word selection object.
Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection;
//Count the number of documents to insert;
int documentCount = filesToMerge.Length;
//A counter that signals that we shoudn't insert a page break at the end of document.
int breakStop = 0;
// Loop thru each of the Word documents
foreach (string file in filesToMerge)
{
breakStop++;
// Insert the files to our template
selection.InsertFile(
file
, ref missing
, ref missing
, ref missing
, ref missing);
selection.Find.Replacement.ClearFormatting();
selection.Find.ClearFormatting();
selection.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
object oFindText = "\r";
object oReplaceText = " ";
selection.Find.Execute(ref oFindText, ref missing, ref missing, ref missing, ref missing, ref missing, ref oTrue, ref missing, ref oTrue, ref oReplaceText, ref oAll, ref missing, ref missing, ref missing, ref missing);
oFindText = " ";
oReplaceText = " ";
selection.Find.Execute(ref oFindText, ref missing, ref missing, ref missing, ref missing, ref missing, ref oTrue, ref missing, ref oTrue, ref oReplaceText, ref oAll, ref missing, ref missing, ref missing, ref missing);
oFindText = "\n";
oReplaceText = " ";
selection.Find.Execute(ref oFindText, ref missing, ref missing, ref missing, ref missing, ref missing, ref oTrue, ref missing, ref oTrue, ref oReplaceText, ref oAll, ref missing, ref missing, ref missing, ref missing);
oFindText = "\t";
oReplaceText = " ";
selection.Find.Execute(ref oFindText, ref missing, ref missing, ref missing, ref missing, ref missing, ref oTrue, ref missing, ref oTrue, ref oReplaceText, ref oAll, ref missing, ref missing, ref missing, ref missing);
oFindText = "\v";
oReplaceText = " ";
selection.Find.Execute(ref oFindText, ref missing, ref missing, ref missing, ref missing, ref missing, ref oTrue, ref missing, ref oTrue, ref oReplaceText, ref oAll, ref missing, ref missing, ref missing, ref missing);
oFindText = "\r\n";
oReplaceText = " ";
selection.Find.Execute(ref oFindText, ref missing, ref missing, ref missing, ref missing, ref missing, ref oTrue, ref missing, ref oTrue, ref oReplaceText, ref oAll, ref missing, ref missing, ref missing, ref missing);
/*
//Do we want page breaks added after each documents?
if (insertPageBreaks && breakStop != documentCount)
{
selection.InsertBreak(ref pageBreak);
}*/
}
// Save the document to it's output file.
wordDocument.SaveAs(
ref outputFile
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing);
// Clean up!
wordDocument.Close();
wordDocument = null;
}
catch (Exception ex)
{
//I didn't include a default error handler so i'm just throwing the error
throw ex;
}
finally
{
// Finally, Close our Word application
wordApplication.Quit(ref missing, ref missing, ref missing);
}
}
And this is the error that throws me when it is published on the server windows server 2012 and is also installed the office 2013
[UnauthorizedAccessException]: Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at SGPJ.DescargarArchivo.Merge(String[] filesToMerge, String outputFilename, Boolean insertPageBreaks, String documentTemplate, String nsec_aop, DataTable tblRequisitos)
at SGPJ.DescargarArchivo.GenerarProtocolo3(String nsecAop)
at SGPJ.DescargarArchivo.Page_Load(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
[HttpUnhandledException]: Exception of type 'System.Web.HttpUnhandledException' was thrown.
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
--><!--
This error page might contain sensitive information because ASP.NET is configured to show verbose error messages using <customErrors mode="Off"/>. Consider using <customErrors mode="On"/> or <customErrors mode="RemoteOnly"/> in production environments.-->