none
making word document readonly RRS feed

  • Question

  • how to make a word document readonly in c#

    i have used

    private Word.Document D;

    D.Protect( Word.WdProtectionType.wdAllowOnlyReading, ref MISSING, ref MISSING, ref MISSING, ref MISSING);

    how to make that document readonly by giving password protection.

    the code should open the protect Document dialog automatically and from there the user has to give the password so that no other user can modify the content.

    please suggest me the code for this sir.

     

    Friday, January 6, 2012 4:47 AM

Answers

  • Here's the method with the parameter for the password marked by "password"

    ActiveDocument.Protect wdAllowOnlyReading, , "password", ,

    You need to show some kind of input form to the user. The result the user types will be a string. Create an object from that string and pass it by ref as a parameter.

    object oPassword = stringValueTypedByUser;
    D.Protect( Word.WdProtectionType.wdAllowOnlyReading, ref MISSING, ref oPassword, ref MISSING, ref MISSING);


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Wednesday, January 18, 2012 6:10 AM
    Friday, January 6, 2012 1:57 PM
    Moderator

All replies

  • Hi Shyamaldas

    And in what way is the Protect method, as you show, not working for you? One of those parameters to which you're passing ref MISSING is for the password protection. Assign a password and the document will be protected.


    Cindy Meister, VSTO/Word MVP
    Friday, January 6, 2012 7:56 AM
    Moderator
  • sir which parameter should be assigned a password value. i tried to search but i didn't get the answer.

    u please tell me how to write that code line with password protection. the person who is coding need not to know the password. only the password belongs to the user. so i just want to open the protect document dialog using code.

    thank you sir.

    Friday, January 6, 2012 1:44 PM
  • Here's the method with the parameter for the password marked by "password"

    ActiveDocument.Protect wdAllowOnlyReading, , "password", ,

    You need to show some kind of input form to the user. The result the user types will be a string. Create an object from that string and pass it by ref as a parameter.

    object oPassword = stringValueTypedByUser;
    D.Protect( Word.WdProtectionType.wdAllowOnlyReading, ref MISSING, ref oPassword, ref MISSING, ref MISSING);


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Wednesday, January 18, 2012 6:10 AM
    Friday, January 6, 2012 1:57 PM
    Moderator
  • thank you so much sir. i got it.
    Friday, January 6, 2012 5:31 PM
  • sir i have created a windows form to accept the password.. but how to access that variable in another class after retrieved from the text box. making public is not working.. how can i access that variable before

    object oPassword = stringValueTypedByUser;
    D.Protect( Word.WdProtectionType.wdAllowOnlyReading, ref MISSING, ref oPassword, ref MISSING, ref MISSING);

    this code..

    Friday, January 6, 2012 6:26 PM
  • We need to understand more about how your solution is constructed. Perhaps if you show us the code that displays the form it would help...
    Cindy Meister, VSTO/Word MVP
    Saturday, January 7, 2012 7:47 AM
    Moderator
  • in windows form i have created two text box

    1) one showing "Enter new password"

    2)another showing "Reenter to confirm password"

    i get the input from user and stored it in a variable like

     

    Word.Application wordapp;

    if(textbox1.text == textbox2.text)

    {

                 string password=textbox2.text;

                 Wordauth wa=new Wordauth( wordapp);

                  wa.AssignPassword(password);

    }

    In Wordauth class:

    object passwordToProtect;

    public void AssignPassword(string p)

    {

        passwordToProtect=p;

    }

    and in another function which possess the function of applying protection to the word document i used

    new GetPasswordForm().ShowDialog();

    D.Protect( Word.WdProtectionType.wdAllowOnlyReading, ref MISSING, ref passwordToProtect, ref MISSING, ref MISSING);

     

    AT THE TIME OF BUILDING

    i got the warning like Wordapp variable in windows form will always take a null value.

    But after running the code , i tried to display the content of the variable passwordToProtect like

    public void AssignPassword(string p)

    {

        passwordToProtect=p;

        MessageBox.show(passwordToProtect.ToString());

    }

    It showed the string value of the variable correctly.But after that when i try to close the GetPasswordForm .I got an exception "TypeMismatch"

    Please find what might be the error sir. Thank you sir.

    Saturday, January 7, 2012 8:39 AM
  • sir,

    can we make to display the text of input box as dots or astrisk as for password input..

    or any other way to display like that without creating windows form

    Sunday, January 8, 2012 6:43 PM
  • Hi Shyamaldas

    <<AT THE TIME OF BUILDING

    i got the warning like Wordapp variable in windows form will always take a null value.>>

    If you don't need wordapp, comment it out.

    What line of code is triggering TypeMismatch?

    It seems to me that one problem is certainly this line of code:
      Wordauth wa=new Wordauth( wordapp);

    You're instantiating a new instance of Wordauth, which has no connection to the instance that called the form for getting the password. You need to pass that instance of the class to the form OR leave the Wordapp declaration in and pass the instance of the Word.Application used by Wordauth OR pass in a string "by ref" to the form.

    Basically, what this means is that you need to change the form class's constructor to accept the object/value you're passing to it. So instead of this:

            public GetPasswordForm()
            {
                InitializeComponent();
            }
    
    

    You'd have something like this:

             Word.Application Wordapp = null;<br/>         public GetPasswordForm(Word.Application app)
            {
                InitializeComponent();<br/>            Wordapp = app;
            }
    
    

    Y


    Cindy Meister, VSTO/Word MVP
    Monday, January 9, 2012 7:43 AM
    Moderator
  • thank  you sir.i will try what you have said..

    can you explain me this case

     

    can we make to display the text of input box as dots or astrisk as default  for password input..

    or is there any other way to display like that without creating windows form

    Monday, January 9, 2012 8:25 AM
  • can we make to display the text of input box as dots or astrisk as default  for password input..

    or is there any other way to display like that without creating windows form


    I don't know of any way to do this, no. Theoretically, I suppose it would be possible using the Windows API, but I have no idea of how to actually do it.
    Cindy Meister, VSTO/Word MVP
    Monday, January 9, 2012 9:38 AM
    Moderator
  • thank you sir.
    Monday, January 9, 2012 10:32 AM
  • sir,

    i have changed the code as you said.. The warning is cleared but still ..if i try to display the variable passwordToProtect

    like MessageBox.Show(passwordToProtect.ToString()); after assigning the value .. it is showing the exception

    new GetPassword(wordApp).ShowDialog();
    MessageBox.Show(passwordToProtect.ToString());
                
                  D.UndoClear();
                  D.Protect(
                      Word.WdProtectionType.wdAllowOnlyReading,
                  ref MISSING, ref passwordToProtect, ref MISSING, ref MISSING);
                
    
    private string password;
            private Word.Application wordApp;
            public GetPassword(Word.Application app)
            {
                InitializeComponent();
                wordApp = app;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (textBox1.Text == textBox2.Text)
                {
                    password = textBox2.Text;
                    MessageBox.Show(password);
                    WordQA wordQAObject = new WordQA(wordApp);
                    wordQAObject.AssignPassword(password);
                    this.Hide();
                }
                else
                    MessageBox.Show("Please check both of the passwords");
            }
    

      public WordQA(Word.Application wordApp)
            {
               this.wordApp = wordApp;
            }
    
             public void AssignPassword(string password)
             {
                 passwordToProtect = (Object)password;
                 MessageBox.Show(passwordToProtect.ToString());
             }
    


    this is my code sir.

    it is displaying the messagebox with the password value in AssignPassword and button1_Click function.

    but it is not displaying the password after the calling statement of GetPassword(wordApp).ShowDialog().

    It is either showing the exception like TypeMismatch at the Messagebox.Show(passwordToProtect.ToString())

    or showing object reference not set to instance

    please find what would be the error sir.

    Monday, January 9, 2012 1:29 PM
  • As I said before, don't do this:

                    WordQA wordQAObject = new WordQA(wordApp);
                    wordQAObject.AssignPassword(password);

    You need to protect the document in the form's code.

    Perhaps it would be better if you pass the document object to the form (D) instead of the Word.Application object.

     


    Cindy Meister, VSTO/Word MVP
    Monday, January 9, 2012 1:40 PM
    Moderator
  • ok sir.. thank you so much.. i will do that
    Monday, January 9, 2012 5:31 PM
  • i have created addins for MS Word in c# whether there is a possiblitiy to create similar addins  which possess cryptographical functions for open office.org in Linux environment.
    Thursday, January 12, 2012 9:23 AM
  • You need to ask this in a venue that supports open office and Linus. The forums on MSDN target Microsoft products and the products you mention are definitely not Microsoft.
    Cindy Meister, VSTO/Word MVP
    Thursday, January 12, 2012 3:27 PM
    Moderator