How to open a password protected .ppt file with the Microsoft.Office.Interop.PowerPoint

I am trying to open a powerpoint file that has a password protection on it...

powerPoint = new Microsoft.Office.Interop.PowerPoint.Application();                
                presentations = powerPoint.Presentations;                          
                ps = presentations.Open(request.FilePath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

This just hangs... I'm assuming it's waiting for user input.  Is there anyway around this with the powerpoint interop??
How about a way to find out if the powerpoint file has a password ahead of time, so i can just skip it?

thanks!                                              


thatsricci
Answer
Answer

Okay that became part of my solution... since my goal is simply to 'skip' password protected files I was able to attempt to open them with a dummy password and catch the exception and simply report that back to the user.

On the other hand, some of these files are read-only capable and require a password for 'modifications'.   When you open these files by hand you get the dialog that let's you click 'open as read-only' and continue to open and export the document by hand...  I can't figure a way to handle this situation via the interop either... I've tried opening with ReadOnly (goes for office/excel documents too) but it still pops up the dialog, so I have to spawn the process in a Thread and give it a timeout, assuming if it doesn't open then it's probably asking for a password.   I'm ok with this, but wouldn't mind a more robust solution. 

 

Guess we're stuck without a smooth interop into office, we'll have to live with the one we got for now! 

 

long story short  here's the rough outline of my solution:

Application powerPoint = new PowerPoint.Application();

Presentations presentations = powerPoint.Presentations;

Thread openPPT = new Thread(delegate() {

presentations.Open("filepath", MsoTriState.msoTrue, MsoTriState.msoFalse);

});

openPPT.start();

if (!openPPT.Join(20000)) // 20 seconds to open

throw new Exception("Can't open, took too long, probably requires password...etc...etc.");

else

// processfile.... 

 

 

try/catch around the open to catch the 'invalid password' issue if you want...   use  powerPoint.ProtectedViewWindows.Open with a fake password and it will throw it there...

other options include:

 

ps = powerPoint.ProtectedViewWindows.Open(request.FilePath, "fakePassword", Microsoft.Office.Core.MsoTriState.msoFalse).Presentation;                            

                            if (!string.IsNullOrEmpty(ps.PasswordEncryptionAlgorithm))

                            {

                                throw new Exception("PowerPoint file requires a password, this cannot convert.");

                            }

 

Although that seems like a hack, it' been solid so far with the hundreds of sample files I have problems with. 

 

Thanks,

 

Justin. 


thatsricci
thatsricci

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated July 17, 2022 Views 10,517 Applies to: