2

I do not wish to get into a debate about the merits of using Microsoft Office to perform this task. I have argued endlessly that this is NOT the way to accomplish this but the powers that be have overridden me on numerous occasions. I understand too that Microsoft does NOT recommend using this method and does NOT support it. But here I am….

Issue: I have created a console application that processes files by encrypting them with a password so they can be emailed. I have no issues processing images and PDF’s, both using iTextSharp, but there is a strange issue processing office documents. When I step through the studio they process without any issues no matter what active directory account I am logged in with. When deployed however office documents will not process unless it is running under my AD account. I know exactly where the error is generated. Here is the code below.

private void PasswordProtectWord(Datapassedin pa)
{
   int num = 0;
   try
   {
      Microsoft.Office.Interop.Word._Application objWord = null;
      _Document objDoc = null;
      objWord = (Microsoft.Office.Interop.Word.Application)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("000209FF-0000-0000-C000-000000000046")));

      objWord.Visible = false;
      objWord.DisplayAlerts = WdAlertLevel.wdAlertsNone;
      objWord.DisplayAutoCompleteTips = false;
      objWord.DisplayDocumentInformationPanel = false;
      objWord.DisplayRecentFiles = false;
      objWord.ScreenUpdating = false;
      object objValue = Missing.Value;
      object objMissing = Type.Missing;
      object docName = pa.DocName;
      object password = pa.Password;
      object passwordProtectedDocName = pa.Password_Protected_DocName;
      object objTrue = true;
      object objFalse = false;
      object objReadOnly = configvalue.ObjReadOnly;
      // open statement below does not open
      objDoc = objWord.Documents.Open(ref docName, ref objValue, ref objReadOnly, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objFalse, ref objValue, ref objValue, ref objValue, ref objValue);
      // null object error is thrown here when we attempt to activate.  I discovered this by placing flags in the code line
      objDoc.Activate();
      objDoc.SaveAs2(ref passwordProtectedDocName, ref objValue, ref objValue, ref password, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue, ref objValue);
      objDoc.Close(objTrue, Type.Missing, Type.Missing);
      Marshal.ReleaseComObject(objDoc);
      objWord.Quit(objFalse, Type.Missing, Type.Missing);
      Marshal.ReleaseComObject(objWord);
  }
  catch (Exception ex)
  {
      UpdateErrorLogAndSendEmail(string.Format("{0} And {1} - {2} - {3}", ex.Message, ex.InnerException.Message,  num.ToString(), pa.Password_Protected_DocName), ex.Source, "", configvalue.Bcc);
  }
}

Okay here are some of the steps I have taken, albeit the extreme steps, to resolve the issue.

We attempted to rebuild the entire project using the service ID rather than my own account. Then logged into the server using the service ID deploying the code and creating the console job.

Not only did this not work it also repeated the behavior by allowing the console application to run when my ID was used as the AD authority.

Tried to duplicate all permissions from my AD account to the service ID. Same results as above.

Items to consider:

  1. Scheduled task processes all image and PDF files no matter what AD account is used.
  2. No AD account works on office documents when not ran under my account. I only mentioned the service account above but we have also tried system administration accounts to run the scheduled task as well.
  3. All accounts attempting to run the scheduled task logged into the deployment servers and opened word/excel documents to ensure we have temp space on the server.
  4. Office settings for all accounts were compared and matched to my settings.

One of the issues with this problem is I'm not even sure what to search for in searching for an answer.

Kara
  • 6,115
  • 16
  • 50
  • 57
Mike
  • 21
  • 1
  • "however office documents will not process": in what way? Error or what? Need to describe the symptoms here. PS. What does a process dump at that point show when loading in a debugger? – Richard Jan 08 '14 at 13:47
  • Is this relevant? http://stackoverflow.com/a/11359745/352101 – Bolu Jan 08 '14 at 14:06
  • Richard, if you read the post the code line has comments to tell you exactly where it fails and the error generated. It appears to not be able to open the document and throws a "object null value" exception. The file in question is definitely there and the Service AD account has access to that directory because it is able to process other file types from that network share. – Mike Jan 09 '14 at 13:24
  • Bolu - Thanks I am looking at that link now and will let you know. I can tell you I do not have the Microsoft Word 97-2003 Documents in "Dconfig" but I may have found some other settings that need corrected. – Mike Jan 09 '14 at 13:43
  • Richard, I meant to add as well that in my original description clearly states that the process works fine when stepping through the studio in debug mode. – Mike Jan 09 '14 at 13:57
  • Bolu - I will continue to look at this BUT the only DCom setting I found that may be relevant is the "Launch and Activation Permission" setting on "Office Licensing COM Server 14 properties" giving my AD service account direct full permissions. It did not work but I will continue to look into the possibility that dcom permissions could hold a key. It is apparent to me that this is some type of permissions issue related to Office. – Mike Jan 09 '14 at 14:01
  • Sorry Bolu I was not running mmc under 32 bit. I'm revisiting this solution. – Mike Jan 09 '14 at 14:47
  • Tried to use the DCOM settings but I'm still having the same issue. It did give me something to look at though. – Mike Jan 09 '14 at 16:08
  • Update and correction, I went back to the article you supplied Bolu and my original read of the link you supplied was flawd. After reviewing it again and following the solution provided by NaNNy it indeed resolved my issue. Rather than set the "Identity tab" to "interactive" I had to set it to the AD account running the service. I am still curious, and I'm not one of those guys that HAVE to know why something works, as to why my AD account would work running the service. – Mike Jan 09 '14 at 19:59
  • This is working now though and I appreciate you remembering this article. Once I saw the article I could not believe I did not use the same subject line or used it in my google but alas I did not. Thanks for you help. – Mike Jan 09 '14 at 20:00
  • 1
    Glad you have solved the problem, you may post the answer with all the steps for your own question. – Bolu Jan 10 '14 at 09:25

0 Answers0