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:
- Scheduled task processes all image and PDF files no matter what AD account is used.
- 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.
- 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.
- 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.