none
password is incorrect error on document with forms protection RRS feed

  • Question

  • Hi,

    We have a Word 2007 Template (a .dotm file) that we use to customize the authoring experience for our documents.  The Template code creates certain sections that we don't want the users to modify using the Section Break Continuous break type.  We then password protect those sections.  The password is held in a constant variable.

    In order for some of our code to run, we have to unprotect the document first.  So, we unprotect the document, run the code that needs to run, then we re-protect the document again.  We have ProtectDocument and UnProtectDocument subroutines to do this.

    From a user's perspective, the document is always protected.  They cannot unprotect it because they don't know the password.

    We have one user who can create new documents based off our Template, but when they close and then re-open the document, our code throws an error when it tries to unprotect the document (before it has to do some work that requires the document to be unprotected).

    If anyone else who has our Template opens the document, it works fine.  It's just this one user who is getting the error.

    Any ideas of what could possibly cause this very strange behavior?

    Thanks in advance for the help!

    • Moved by Max Meng Friday, December 7, 2012 6:03 AM (From:Word IT Pro Discussions)
    Wednesday, December 5, 2012 7:35 PM

Answers

  • Just FYI: The local IT person rebuilt the user's PC and everything is working correctly now.

    Thanks for the help!

    • Marked as answer by theLuggage Wednesday, December 19, 2012 2:31 PM
    Wednesday, December 19, 2012 2:31 PM

All replies

  • Hi,

    Is that means the user with the issue could open the document without problem on the first time?

    And the other users can open and close the document many times without the problem?

    Can you share the code you used so we can look into it?

    .

    BTW, just check if the user's Preview pane has been enabled, disable it if so, and check if the problem persists.

    Or, check the Task Manager, make sure the WinWord.exe has exited when the document is closed.


    Max Meng
    TechNet Community Support

    If you have any feedback on our support, please click here

    • Edited by Max Meng Thursday, December 6, 2012 5:53 AM
    Thursday, December 6, 2012 5:24 AM
  • The user can create a new document based off the template.  But they can never open it again.  (Our code throws an exception when it fails to unprotect the document with the "incorrect password" error.  And our code closes the document.)  Any other user can open the document.

    We're using Word 2007.  I don't know what the Preview pane is.  But she didn't have any custom task panes of any kind open.  To open the file, she's choosing the file from the Word recently used file list.  Or she's just double-clicking the file in Win Explorer.

    Here's our code to protect and unprotect our documents:

    Public Const DOC_PSWD               As String = "TheDocPassword"
    Public Const TMPLT_NAME               As String = "Co Research.dotm"
    
    Public Function ProtectDocument(ByRef intError As Integer) As Boolean
        Dim i           As Integer
        
        If ActiveDocument.AttachedTemplate <> TMPLT_NAME Then
            ProtectDocument = True
            intError = 0
            Exit Function
        End If
        
        On Error GoTo ErrorHandler
        
        ' Protect the doc, if it isn't already
        If ActiveDocument.ProtectionType = wdNoProtection Then
            ' Can't protect the doc if the cursor is not in the main story
            If Selection.StoryType <> wdMainTextStory Then
                Selection.GoTo what:=wdGoToSection, which:=wdGoToAbsolute, Count:=2
            End If
        
            With ActiveDocument
                .Sections(1).ProtectedForForms = True
                .Sections(2).ProtectedForForms = False
                .Sections(3).ProtectedForForms = True
                For i = 4 To .Sections.Count
                    .Sections(i).ProtectedForForms = False
                Next i
                .Sections.Last.ProtectedForForms = True
                    
                .Protect wdAllowOnlyFormFields, Password:=DOC_PSWD
            End With
        End If
    
        ProtectDocument = True
        intError = 0
    
    ExitFunction:
        Exit Function
        
    ErrorHandler:
        ProtectDocument = False
        intError = 2
        GoTo ExitFunction
        
    End Function
    
    Public Function UnprotectDocument(ByRef intError As Integer) As Boolean
        
        If ActiveDocument.AttachedTemplate <> TMPLT_NAME Then
            UnprotectDocument = True
            intError = 0
            Exit Function
        End If
        
        On Error GoTo ErrorHandler
        
        With ActiveDocument
            ' Unprotect the doc, if it isn't already
            If (.ProtectionType = wdNoProtection) = False Then
                .Unprotect Password:=DOC_PSWD
            End If
        End With
        
        UnprotectDocument = True
        intError = 0
    
    ExitFunction:
        Exit Function
        
    ErrorHandler:
        UnprotectDocument = False
        intError = 2
        GoTo ExitFunction
        
    End Function

    I did check, and the user only has the one instance of WindWord.exe running and it does close when they close the document and quit Word.

    Thanks again for the help!

    • Edited by theLuggage Thursday, December 6, 2012 3:10 PM fix typo
    Thursday, December 6, 2012 2:58 PM
  • Hi theLuggage,

    Sorry for late response,

    Would you please run your macro in docm file? It's based on my experience that dotm file doesn't exit from the process until the document have been saved.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 17, 2012 7:47 AM
    Moderator
  • Hi "Luggage"

    Very odd... The one thing that occurs to me, after thinking about this for a few minutes, is that this particular user might have more than one version of the the *.dotm template installed? An earlier version that had a different password? And Word is picking up that template as the attached template, rather than the one used to create the document, thus the wrong password?


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, December 17, 2012 8:49 AM
    Moderator
  • Hi Cindy,

    I did a search on the user's PC and there weren't any old templates.  Even if there were, we've used the same password for at least 8 years!

    I wondered if maybe they had some add-in that was using the same constant variable name as we were and so I renamed the constant to something else and that didn't help either.

    I've been doing MS Office development in VBA and VBA.NET and now C# for more than 12 years and this is by far the weirdest thing I've ever seen!

    I've asked the user's local IT group to check for bad memory, bad hard drive sectors, and to wipe the hard drive and re-install Windows and Office.  Hopefully that takes care of it.

    Monday, December 17, 2012 3:24 PM
  • Hi "Luggage"

    It would be interesting to see if the protection could be removed manually, but as that would end up revealing the password to this user (unless you can Remote Desktop?)... That would at least tell you if the problem is at the code or at the Word level.

    If it's at the Word level, something is niggling at the back of my mind. A vague recollection of some discussion - not dev-related. I think it had something to do with saving Word 2010 or docx files, generally, back to another file format screwing up forms protection passwords (what you're seeing). But if what I'm remembering is correct, then others' machines shouldn't be able to unprotect this document either. Might be worthwhile doing a quick Internet search, though, and checking in what the default Save file format is on that machine.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, December 17, 2012 3:52 PM
    Moderator
  • Hi Cindy,

    I did try typing the password and that didn't work--I get the same "incorrect password" error. (This user is in a different country than I am, so I've been doing all my trouble-shooting via remote connection.)

    The really weird thing is that when I had her email the .docx file to me, I could open it just fine and could type the password in just fine.  So, it's definitely something with this one specific user's PC.

    The user has Win XP and Office 2007. Her default save format is the usual .docx format.

    Monday, December 17, 2012 4:18 PM
  • Just FYI: The local IT person rebuilt the user's PC and everything is working correctly now.

    Thanks for the help!

    • Marked as answer by theLuggage Wednesday, December 19, 2012 2:31 PM
    Wednesday, December 19, 2012 2:31 PM
  • Glad you got it solved!

    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, December 19, 2012 7:39 PM
    Moderator