In Loki We Trust The many projects of Lokkju, Inc


Using Office Automation on IIS7

Though there are a lot of articles out there on office automation in dotnet (most of them telling you not to do it), there are very few covering how to get office automation up and running under IIS7 on a 64bit machine - and it is possible.

I needed to do this recently, and found one hint on how to get it working at . They key is to use Process to launch the application you need, then attach to it.

Sample code in C#:

Microsoft.Office.Interop.Word.Application app = null;
Process proc = null;
Document doc = null;
ProcessStartInfo procinfo = new ProcessStartInfo(WORD_PATH, "");
procinfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
procinfo.CreateNoWindow = true;
procinfo.WindowStyle = ProcessWindowStyle.Hidden;
proc = Process.Start(procinfo);
app = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
if (app == null) { throw new Exception("Word not found"); }
app.Visible = false;
app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
#region Declare Params
object fileName = filename;
object ConfirmConversions = false;
object ReadOnly = true;
object AddToRecentFiles = Type.Missing;
object PasswordDocument = Type.Missing;
object PasswordTemplate = Type.Missing;
object Revert = Type.Missing;
object WritePasswordDocument = Type.Missing;
object WritePasswordTemplate = Type.Missing;
object Format = Type.Missing;
object Encoding = Type.Missing;
object Visible = false;
object OpenAndRepair = Type.Missing;
object DocumentDirection = Type.Missing;
object NoEncodingDialog = Type.Missing;
object XMLTransform = Type.Missing;
doc = app.Documents.Open(ref fileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform);
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
object OriginalFormat = Type.Missing;
object RouteDocument = Type.Missing;
((_Document)doc).Close(ref saveChanges, ref OriginalFormat, ref RouteDocument);
doc = null;
catch { }
((Microsoft.Office.Interop.Word._Application)app).Quit(ref saveChanges, ref OriginalFormat, ref RouteDocument);
app = null;
catch { }
catch { }

Comments (3) Trackbacks (0)
  1. I am trying to get a web service to save word docs as PDFs. My code works fine in my unit tests, but not when I call it directly from IIS. Did you need to do any further configuration of DCOM, IIS or file system security to get your code to work? I have tried your code, but it doesn’t work. The Word object is created just fine, but the call to app.Documents.Open always returns null.

    Any help is appreciated. Thanks.

  2. A few things:
    – Make sure to do a full install of office.
    – create a user for the service, and make sure to open word at least once with that user
    – configure the service to use the specific user you created
    there are major differences between what is needed for IIS6 and for IIS7 as well – which are you on?

  3. Hi
    Iam facing same issue with IIS7. I tried your code but still IAm getting as null.
    I have MS office 2010 Intalled on my server. COde works in debugging mode but the same does not work when runing from IIS
    Please help.

Leave a comment

No trackbacks yet.