Frage

I've been assigned to create a TOC (Table of contents) for Word document which it's filled with HTML code dynamically base on several information obtained from database and with a specific design, and finally this operation has to be done in a ASP. NET server side using 3.5 .NET Framework programmed in C# and it has to be sent to the client side with the TOC generated and in Word format.

I can tell you too that the OS of the server is Windows 2008 Server, and it has Microsoft Office 2010 installed.

So, firstly I try using Microsoft.Interoffice.Word integrating it in the server side code, but afterwards I realised that Microsoft didn't support this sort of things because of security matters. Then I try to create a console application which make this function (using Microsoft.Office.Interop.Word) and exporting the HTML code to doc document, both of them located in the same folder, and IIS_USERS group have full access to this location. But, when I try to launch the console application which creates the table of contents in the website code, it shows me an error related to the console application.

I have tested the console application with doc generated filled with HTML code and it runs smooth, so I don't understand what's going on...does IIS detects that the console application uses Microsoft.Office.Interop.Word, and it doesn't allow it to execute? The idea was that when the console application finish, I would bring back the whole document again to website and generate an HTTP response to the client side so it would show the typical Open/Save popup dialog.

I have been trying to use external libraries too without results like GemBox (FileFormat problem with HTML code), NPOI (its HWPF library it's in alpha version), OpenSDK (cannot evalute paging so it can't create a TOC wih the doc filled with info), AsPosed Words (quite expensive library, i can't try this kind of functions in free license)

I show you know the console application code:

    ArrayList arrValoresIndices = new ArrayList();
Application wordApp = new Application();
object missing = System.Type.Missing;
    
try {
    Document wordDocument = wordApp.Documents.Open(docFileSource);
    //Applying style to headers
    System.Drawing.Color colorNecesario = System.Drawing.ColorTranslator.FromHtml("#1F497D");
    WdColor coloraplicado = (Microsoft.Office.Interop.Word.WdColor)(colorNecesario.R + 0x100 * colorNecesario.G + 0x10000 * colorNecesario.B);
    wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Name = "Calibri";
    wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Size = 14;
    wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Bold = -1;
    wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Color = coloraplicado;
    
    int numPalabras = wordDocument.Words.Count;

    for (int i = 1; i < numPalabras; i++)
    {

        string texto = wordDocument.Words[i].Text;

        if (texto.Equals("") == false && wordDocument.Words[i].Font.Size == 14 &&
             wordDocument.Words[i].Font.Name == "Calibri" &&
             wordDocument.Words[i].Font.Bold == -1 &&
             wordDocument.Words[i].Font.Color == coloraplicado)
        {
            wordDocument.Words[i].set_Style(WdBuiltinStyle.wdStyleHeading1);
        }

    }

    object gotoPage = WdGoToItem.wdGoToPage;
    object gotoNext = WdGoToDirection.wdGoToNext;
    object gotoCount = null;
    object gotoName = "2";
    wordApp.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName);
    wordApp.Selection.InsertBreak(WdBreakType.wdPageBreak);
    gotoName = "2";
    Range indexPage= wordApp.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName);
    object oTrue = true;
    TableOfContents toc = wordDocument.TablesOfContents.Add(indexPage, ref oTrue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref oTrue);

    wordDocument.TablesOfContents.Format = WdTocFormat.wdTOCModern;
    toc.Update();
    wordDocument.SaveAs2(docFileSource,WdSaveFormat.wdFormatFilteredHTML);
    wordDocument.Close();
    wordApp.Quit();
}
catch (COMException ce)
{
    wordApp.Application.Quit(ref missing, ref missing, ref missing);
    throw new COMException("Process has failed ...\n");
    }
}

Web site code which launches the code:

String nombreDoc = "C:\\tempDocs\\JGA.doc"; //i will store the doc in this folder, in this folder IIS_USRS have full access
File.WriteAllText(nombreDoc, strCabecera.ToString()); //strCabecera contains the whole html code
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = @"C:\\tempDocs\\TOCIndexer\\TOCIndexer.exe -nombreDoc";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Verb = "runas";

using (Process exeProcess = Process.Start(p))
{
     exeProcess.WaitForExit();
}

//When the process has finished I import againg the content to a new string builder
StringBuilder strCabeceraVolcado = new StringBuilder();
using (var sr = new StreamReader(nombreDoc))
{
    strCabeceraVolcado.Append(sr.ReadToEnd());
}
if (File.Exists(@nombreDoc))
{
    File.Delete(@nombreDoc);
}
strCabecera = strCabeceraVolcado;

HttpContext.Current.Response.Write(strCabecera);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();

Could you give me some advice?

Thanks

War es hilfreich?

Lösung

I have realised after researching, that the whole application was right and the problem resides in server settings.

This solution only works for Windows Server 2008 64 bits, you have to follow the next steps if you encounter problems with Microsoft.Office.Interop.Word:

1º You have to install Microsoft.Office.Word in the server.

2º You have to assigned "Full Control" permissions in the folder in which the documents are generated to the "IIS group of users", and "Anonymous Logon" user.

3º You have to modify Component Services (C:\Windows\System32\comexp.msc) and set Identity the value "The Interactive User" to "Office Licensing COM Server 14 Properties" (can be 12 instead of 14 depending on the version of Office you install)

4º Create folder "Desktop" in the path located at C:\Windows\SysWOW64\config\systemprofile\

If you follow this steps, ASP.NET will allow you modify documents using Microsof.Office.Interop.Word library.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top