Unprotect Word Document via C# and .NET

  • Thread starter Thomas Roessler via OfficeKB.com
  • Start date
T

Thomas Roessler via OfficeKB.com

I am trying to unprotect a Word2003-document using .NET-libraries and C#
(btw: the document protection in my case does not require a password).

So, the .NET-library provides me with the

_Document.Unprotect(ref object)

method, but it does not work. Even when I call this method, the forms
contained in my document are still protected and the application which I
have developed is not able to retrieve the content of the document.

Does anybody have an idea how to unprotect this kind of word-forms using C#?

Thanks!

Thomas
 
C

Cindy M -WordMVP-

Hi Thomas,

HOW does it not work? An error, I presume; what is the exact message?

How, exactly, was document protection enabled? Also, as I recall, even though
your document doesn't have a password for the protection, you need to pass
even optional arguments when working with C#. And, in this case, it would be
an "object missing" type of object. Did you define that for "object" in your
code snippet, below, or did you just type object in there without declaring it
anywhere?
I am trying to unprotect a Word2003-document using .NET-libraries and C#
(btw: the document protection in my case does not require a password).

So, the .NET-library provides me with the

_Document.Unprotect(ref object)

method, but it does not work. Even when I call this method, the forms
contained in my document are still protected and the application which I
have developed is not able to retrieve the content of the document.

Does anybody have an idea how to unprotect this kind of word-forms using C#?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
T

Thomas Roessler via OfficeKB.com

Hi Cindy,

currently I am using the following lines of code in order to unprotect an
eventually protected Word-Form:

this.actualDocument_ = thisApplication.ActiveDocument;
object oMissing = System.Reflection.Missing.Value;
this.actualDocument_.Protect(Word.WdProtectionType.wdNoProtection,
ref oMissing,ref oMissing, ref oMissing,ref oMissing);


Alternatively, I also have tried this approach (since I have not to deal
with a passwort-protection, I use even here the oMissing-reference):

this.actualDocument_.Unprotect(ref oMissing);

In both cases I get no error but the result is the same: my Word-Addin does
not come up. If the form-protection is removed manually by using the
command bar, my Addin starts normally and is able to gather the current
document.

BTW, my Word-Addin does the following: it takes the current word document;
stores it as a temporary Word-XML file and processes the so created XML-
file.

Best regards,

Thomas
 
C

Cindy M -WordMVP-

Hi Thomas,
Alternatively, I also have tried this approach (since I have not to deal
with a passwort-protection, I use even here the oMissing-reference):

this.actualDocument_.Unprotect(ref oMissing);
This would be more the way to go, but...
In both cases I get no error but the result is the same: my Word-Addin does
not come up. If the form-protection is removed manually by using the
command bar, my Addin starts normally and is able to gather the current
document.
Is this being called from your Addin, or from something else? Are you sure
the code is even executing at all (like, put a MessageBox in there or
something)?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
T

Thomas Roessler via OfficeKB.com

Hi Cindy,

thanks for your help - I could handle the problem. I have forgotten to
wrap the unprotect-method in a try-catch-block. Therefore I have not seen
the exception which was thrown by the method. With the help of the
exception it was easy to fix the problem.

Now I have a new problem: after uninstalling the Word-Addin using the setup-
routine created by visual-studio, the addin-button still remains on the
command bar. I remove the button every time within the OnDisconnect-Method,
but this does not help. Do you have any idea? I have read a lot of postings
regarding this issue but nobody could give a clear solution.

Best regards,
Thomas
 
R

robin

When someone posts a problem and then solves it themself (with or
without getting help from others) I think it is nice (polite,
considerate, only decent) to share your solution for all of us.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top