none
Unlock Form in VBA code - MS Word 2010 RRS feed

  • Question

  • I used the wonderful code at the link below to spell check form fields and it works great.  I did need the code to be in the document that is being opened (not a template) so I used the Custom UI Editor to add a new tab and ribbon button to call the macro.  I did have to add the control as IRibbonControl to the sub because I am working with ribbons and not toolbars.

    The button and code works fine until I put on a password.  I did add the password to the two areas in the code where I was told to add it to because we lock our forms with a password.

          oDoc.Unprotect Password:="form"

          oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="form"

    Unfortunately, once I protect my document with the Restrict Editing tab, editing restrictions to filling in forms, an then enforce the protection with the word "form", the Spellcheck button no longer does anything when I click on it.

    Is there something I am doing wrong? 


    http://word.mvps.org/FAQs/MacrosVBA/SpellcheckProtectDoc.htm

    Thanks,

    DebraAnn


    DebraAnn

    Tuesday, July 16, 2013 7:48 PM

Answers

  • Hi DebraAnn

    What's the purpose of using forms protection, in your case? Simply so that the user can TAB from control to control and not make inadvertent changes to the surrounding text?

    If that's the case, make a copy of your document and try something:

    Remove the protection. Select all the document content (Ctrl+A).

    Click the tab of this new control (top of the document) to select it, then click the "Properties" button. Activate the options to not delete the control and to not allow editing.

    In the Developer tab, go to the Controls group and click the button at the top-right (Richtext control). Then use the Group button to group the "form" controls into the one just inserted.

    This should protect all the document content, except for the "form" content controls. You should be able to TAB from control to control and type. You'll notice that the "red squiggles" work, as does the right-click spell check. F7 will also work, but only for each individual content control.

    To spell check the entire document, you can use something like the following macro

    Sub SpellContentControlForm()
        Dim doc As word.Document
        Dim cc As word.ContentControl
        Set doc = ActiveDocument
        For Each cc In doc.Contentcontrols
            cc.Range.Select
            On Error Resume Next
            Dialogs(wdDialogToolsSpellingAndGrammar).Show
            On Error GoTo 0
        Next
    End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 18, 2013 3:44 PM
    Moderator

All replies

  • Hi DebraAnn

    If I'm following the scenario correctly, you need a custom button in the Ribbon to call your macro. The macro would need to unprotect, run the spellcheck, then re-protect. Spellcheck will not work in a document protected as a form when protection is active. Since it's won't work, the spellcheck button is not available.

    Is there a reason you use form fields rather than content controls?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 17, 2013 12:50 PM
    Moderator
  • Hi Cindy,

    Are form fields and content controls the same thing?  If they aren't, I used the selections under the controls section of the ribbon.  I actually got the button to work, unprotect the document, and the spelling verify does start working on the fields but my new problem is if someone clicks the cancel button on the spelling verifier popup, it kills the macro and never resets the password.  Is there an error trap I can fix for this. Here is the code.  I took out a bunch of testing for password type because I always use the same one and only have one section.

    If you could tell me how I can get the password put back on if someone cancel's the spelling verifier, that would be great.  It seems to kill the macro so the code that puts back on the password never gets initiated.  It does relock if I do the spelling verifier.

    Sub RunSpellcheck(control As IRibbonControl)

    On Error GoTo Err_RunSpellcheck

    Dim oDoc As Document

    Set oDoc = ActiveDocument

    'unprotect the document
    oDoc.Unprotect Password:="form"
    Selection.HomeKey Unit:=wdStory
    oDoc.SpellingChecked = False

    'Run spelling verifier
            If Options.CheckGrammarWithSpelling Then
                oDoc.CheckGrammar
            Else
                oDoc.CheckSpelling
            End If
            Application.ScreenRefresh
            If oDoc.SpellingErrors.Count = 0 Then
                If Options.CheckGrammarWithSpelling Then
                    MsgBox "The spelling and grammar check is complete", vbInformation
                    oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="form"
                Else
                    MsgBox "The spelling check is complete", vbInformation
                    oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="form"
                End If
            End If
            System.Cursor = wdCursorNormal
            Exit Sub

    Application.ScreenRefresh
    If oDoc.Range.SpellingErrors.Count = 0 Then
        MsgBox "The spelling check is complete", vbInformation
    End If

    'Release variables from memory
    System.Cursor = wdCursorNormal
    Cancelled = False
    CorrectedError = vbNullString
    Set MyRange = Nothing

    Err_RunSpellcheck:
        oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="form"
        Exit Sub
    End Sub


    DebraAnn

    Thursday, July 18, 2013 1:16 AM
  • Hi DebraAnn

    No, form fields and content controls are definitely NOT the same thing. But they're in the same Group on the Ribbon. Content controls are the ones at the top; form fields are in the list of "legacy controls". So, which are you using?


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 18, 2013 8:46 AM
    Moderator
  • I am using content controls (I did nto go into legacy controls).

    Thanks.


    DebraAnn

    Thursday, July 18, 2013 2:28 PM
  • Hi DebraAnn

    What's the purpose of using forms protection, in your case? Simply so that the user can TAB from control to control and not make inadvertent changes to the surrounding text?

    If that's the case, make a copy of your document and try something:

    Remove the protection. Select all the document content (Ctrl+A).

    Click the tab of this new control (top of the document) to select it, then click the "Properties" button. Activate the options to not delete the control and to not allow editing.

    In the Developer tab, go to the Controls group and click the button at the top-right (Richtext control). Then use the Group button to group the "form" controls into the one just inserted.

    This should protect all the document content, except for the "form" content controls. You should be able to TAB from control to control and type. You'll notice that the "red squiggles" work, as does the right-click spell check. F7 will also work, but only for each individual content control.

    To spell check the entire document, you can use something like the following macro

    Sub SpellContentControlForm()
        Dim doc As word.Document
        Dim cc As word.ContentControl
        Set doc = ActiveDocument
        For Each cc In doc.Contentcontrols
            cc.Range.Select
            On Error Resume Next
            Dialogs(wdDialogToolsSpellingAndGrammar).Show
            On Error GoTo 0
        Next
    End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 18, 2013 3:44 PM
    Moderator
  • Hi Cindy,

    It has been a while.  I ended up working on a long-term project and am now getting back to this issue.  I have tried to do what you said above but it doesn't seem to lock the regular text so I must be doing something wrong.  When you said to do the following:

    • Click the tab of this new control (top of the document) to select it, then click the "Properties" button. Activate the options to not delete the control and to not allow editing.

    I don't know what new control you are talking about.  I had just did Ctrl-A to select the document.  I did not have a new control.  You do state after the above sentence:

    • In the Developer tab, go to the Controls group and click the button at the top-right (Richtext control).

    Was this second bullet instruction supposed to be done before the first bullet instruction? 

    Also, when I am grouping, how do I group all the other controls "into" the new control?  Do I group all the internal controls first and then group it with the whole document control?

    Thanks for any clarification you can provide.

    DebraAnn


    DebraAnn

    Monday, September 16, 2013 5:34 PM
  • You know, I got this to work if I added just text and control boxes; however, my form is in a table format.  Would that cause the issue for the text not locking down into the document control?

    DebraAnn

    Monday, September 16, 2013 5:38 PM
  • I also get a "Rich text controls cannot be inserted around multiple selections." when I try to group my controls together.

    DebraAnn

    Monday, September 16, 2013 5:47 PM
  • I know I'm about a year late but have you gotten your answer to this question?
    Tuesday, December 30, 2014 8:36 PM