.NET Framework generates strange DCOM error
- by Anders Oestergaard Jensen
Hello, 
I am creating a simple application that enables merging of key-value pairs fields in a Word and/or Excel document. Until this day, the application has worked out just fine. I am using the latest version of .NET Framework 4.0 (since it provides a nice wrapper API for Interop). My sample merging method looks like this: 
public byte[] ProcessWordDocument(string path, List<KeyValuePair<string, string>> kvs)
{
  logger.InfoFormat("ProcessWordDocument: path = {0}", path);
  var localWordapp = new Word.Application();
  localWordapp.Visible = false;
  Word.Document doc = null;
  try
  {
    doc = localWordapp.Documents.Open(path, ReadOnly: false);
    logger.Debug("Executing Find->Replace...");
    foreach (Word.Range r in doc.StoryRanges)
    {
      foreach (KeyValuePair<string, string> kv in kvs)
      {
        r.Find.Execute(Replace: Word.WdReplace.wdReplaceAll,
            FindText: kv.Key,
            ReplaceWith: kv.Value, Wrap: Word.WdFindWrap.wdFindContinue);
      }
    }
    logger.Debug("Done! Saving document and cleaning up");
    doc.Save();
    doc.Close();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
    localWordapp.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(localWordapp);
    logger.Debug("Done.");
    return System.IO.File.ReadAllBytes(path);
  }
  catch (Exception ex)
  {
    // Logging...
    // doc.Close();
    if (doc != null)
    {
      doc.Close();
      System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
    }
    localWordapp.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(localWordapp);
    throw;
  }
}
The above C# snippet has worked all fine (compiled and deployed unto a Windows Server 2008 x64) with latest updates installed. But now, suddenly, I get the following strange error: 
System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
   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)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at Meeho.Integration.OfficeHelper.ProcessWordDocument(String path, List`1 kvs) in C:\meeho\src\webservices\Meeho.Integration\OfficeHelper.cs:line 30
   at Meeho.IntegrationService.ConvertDocument(Byte[] template, String ext, String[] fields, String[] values) in C:\meeho\src\webservices\MeehoService\IntegrationService.asmx.cs:line 49
--
I googled the COM error, but it returns nothing of particular value. I even gave the right permissions for the COM dll's using mmc -32, where I allocated the Word and Excel documents respectively and set the execution rights to the Administrator. I could not, however, locate the dll's by the exact COM CLSID given above. Very frustrating. 
Please, please, please help me as the application is currently pulled out of production. 
Anders
EDIT: output from the Windows event log: 
Faulting application name: WINWORD.EXE, version: 12.0.6514.5000, time stamp: 0x4a89d533
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x00000000
Faulting process id: 0x720
Faulting application start time: 0x01cac571c4f82a7b
Faulting application path: C:\Program Files (x86)\Microsoft Office\Office12\WINWORD.EXE
Faulting module path: unknown
Report Id: 041dd5f9-3165-11df-b96a-0025643cefe6
- 
   
  1000 
  2 
  100 
  0x80000000000000 
   
  2963 
  Application 
  meeho3 
   
  
- 
  WINWORD.EXE 
  12.0.6514.5000 
  4a89d533 
  unknown 
  0.0.0.0 
  00000000 
  c0000005 
  00000000 
  720 
  01cac571c4f82a7b 
  C:\Program Files (x86)\Microsoft Office\Office12\WINWORD.EXE 
  unknown 
  041dd5f9-3165-11df-b96a-0025643cefe6