none
Interop.Word - IIS, server side execution, SaveAs pdf RRS feed

  • Question

  • Hi,
    I'm using Interop.Word for saving docx file as pdf file. When I'm using my solution locally everything works fine. But when I try to run it on IIS variable doc is always null.

    This is my code:

     
    Word.Application word = new Word.ApplicationClass();
    Word.Document doc;
    word.Visible = true;
    string fullPath = Document;
    string fullDocPath = newDocument;
    object o = (object)fullPath;
    object missing = Type.Missing;
    object newObj = (object)fullDocPath;
    object format = (object)Word.WdSaveFormat.wdFormatPDF;
    
    doc = word.Documents.Open(ref o, 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);
    
    try
    {
    doc.SaveAs(ref newObj, ref format, 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);
    doc.Close(ref missing, ref missing, ref missing);
    }
    catch { }
    
    word.Quit(ref missing, ref missing, ref missing);
    word = null;


    I'll go through it in sections, so I can explain what's the problem.

    Word.Application word = new Word.ApplicationClass();
    Word.Document doc;
    word.Visible = true;
    string fullPath = Document;
    string fullDocPath = newDocument;
    object o = (object)fullPath;
    object missing = Type.Missing;
    object newObj = (object)fullDocPath;
    object format = (object)Word.WdSaveFormat.wdFormatPDF; 

    This part is pretty straight-forward. I create instance of word application class, and set object that will be used for opening document.

    This is where problems start:

     
     
    doc = word.Documents.Open(ref o, 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);
    
    try
    {
    doc.SaveAs(ref newObj, ref format, 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);
    doc.Close(ref missing, ref missing, ref missing);
    }
    catch { }



    If I run this on wcf service, that's hosted on IIS 7.0, variable doc is always null, so nothing is done in try block. This works perfectly in local mode.

    It's not security issue, since service is run under mu account, and I'm the administrator. I have also tried adding Everyone to starting\executing section in Component Services (that's accessible through DCOMCNFG). Nothing happened.
    I don't even get an exception when I try to load file, so I don't really know the reason why it's not loading.


    Also:
    word.Quit(ref missing, ref missing, ref missing);
    word = null;



    This part, combined with doc.Close() (from previous segment), closes the runing instance of WINWORD.exe, that has been started, by
    Word.Application word = new
     Word.ApplicationClass();

    BUT!
    it only closes word executable when it's run locally. In my "server-side scenario" document isn't open, so call to doc.Close() cannot be made, and call to word.Quit() by itself doesn't stop WINWORD.exe. So it keeps running in background.

    Any ideas?
    A had similar problem with server side execution of InfoPath, but I found a way around it, but it's not working with word
    • Edited by Milan Erić Thursday, June 18, 2009 2:20 PM Code formatting
    Thursday, June 18, 2009 2:15 PM

All replies

  • Hi Milaneric,

    I too have same problem, please let me know if you find any solution.
    Advanced thanks.

    Murali
    Friday, June 19, 2009 1:41 PM
  • Hello, 

    Word (all Office client applications like Excel, PowerPoint) is not designed to be run from Windows service (and other such un-attended environments). Refer to following KB for the reasons and it's recommended that you don't do it:
    http://support.microsoft.com/?id=257757

    -Ankush

    Friday, June 19, 2009 10:45 PM
  • Hi Milan,

    Office automation works well, but getting it to run in an unattended fashion from a service (IIS) account is a major pain.

    I have worked on a product that actually does this. We have overcome all problems and released it as a commercial solution that you can invoke from a webservice.

    I hope it will be of use to you, it is quite cheap.

    Jeroen


    Thursday, December 3, 2009 4:14 PM