Simant
Simant

Reputation: 4242

Microsoft.Office.Interop is showing a login dialog for a file having a private template but after closing the dialog convert without issue

I have an application that utilizes Microsoft.Office.Interop.Word to access .docx files and convert them into .txt format. The application functions smoothly with most .docx files, except for one particular document. By analysing the document, I learned that it contains a private template for the file which is in SharePoint and while the attached c# code tries to open the document, it tries to fetch the template and a login dialog appears. When I turn off the internet or block HTTP ports 80 and 443 in the firewall outbound rule, the application converts the document to .txt.

When I analysed the Task Manager, I also came to know that the Word document opened with AI.EXE for the particular document as shown below:

enter image description here

Whenever the application attempts to open this specific .docx file, it consistently prompts a login screen. I must either dismiss the credentials dialog to proceed with the request or supply the correct credentials. However, the next time the problematic file is processed, the same dialog reappears which prevents automation of the file conversion.

Is there any way to ignore the private template and use the default word template Normal.dotm without prompting the login screen?

The code that I used to convert .docx to .txt is below and I tried both Open and OpenNoRepairDialog methods as shown in the following code sample:

 Application app = new Application
 {
     DisplayAlerts = WdAlertLevel.wdAlertsNone
 };
 WdSaveFormat saveFormat = WdSaveFormat.wdFormatText;

 string sourceFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "oddFile.docx");
 var targetFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "oddFile.txt");

 try
 { 
     //app.Documents.Open(sourceFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);

     app.Documents.OpenNoRepairDialog(sourceFile, false, false, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
         Type.Missing, WdOpenFormat.wdOpenFormatAuto, Type.Missing, Type.Missing, true, Type.Missing, false);

     app.ActiveDocument.SaveAs(targetFile, saveFormat);
 }
 catch (Exception)
 {

 }
 finally
 {         
     app.ActiveDocument.Close();
     app.Quit();
     app = null;
 }

The dialog that appears when opening the odd file in the c# code: enter image description here

Upvotes: 0

Views: 17

Answers (0)

Related Questions