Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Programatically locking fields in Word 2010

    I played with VBA many years ago and I don't remember much so I need your help. I have an end-user who developed a form in Word 2010 that is full of text fields and checkboxes. It is currently "protected" from any changes and the issue we are having is that the end-user wants the form to "lock" (not allowing any changes to that file) after we fill in the fields and save it.

    I did some research and I understand that we could set the document's code to "fire" after an event (OnSave) so that when the user saves the document, the entire document would be locked from further changes (we want to permanently archive it) and we did consider saving as PDF but we hire blind employees and PDF are not "accessible" to our blind employees, so we are forced to save it as a Word file.

    Thanks!

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Assuming your "text fields and checkboxes" are formfields, you could use a macro like the following, in the form or it's template:
    Code:
    Private Sub Document_Close()
    Dim FmFld As FormField, bComp As Boolean, bSaved As Boolean
    With ActiveDocument
      bSaved = .Saved
      bComp = True
      For Each FmFld In .FormFields
        With FmFld
          If .Type = wdFieldFormTextInput Then
            If Trim(.Result) = vbNullString Then
              bComp = False
              MsgBox "Please complete all the items"
              ActiveDocument.Reload
              Exit Sub
            End If
          ElseIf .Type = wdFieldFormDropDown Then
            If .DropDown.Value = .DropDown.Default Then
              bComp = False
              MsgBox "Please complete all the items"
              ActiveDocument.Reload
              Exit Sub
            End If
          End If
        End With
      Next
      If bComp = True Then
        For Each FmFld In .FormFields
          FmFld.Enabled = False
        Next
      End If
      If bSaved <> .Saved Then .Save
    End With
    End Sub
    The test for Dropdown formfields assumes the default value (which might be a blank or text asking for a selection to be made) is not valid.

    The problem you'll have with both checkboxes and Dropdown formfields that have valid default values is that there is no reliable way of telling whether those values are 'correct'
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    New Lounger
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the code. I'll give it a shot.

    Brent

  4. #4
    New Lounger
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Paul -

    Thanks so much for the code - As it turns out, the document goes through several people, so the code fires too early. We have determined a better course of action to deal with this.

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    In that case, you might prefer a less intrusive version. This one gives no prompts and, instead, simply checks whether all the textboxes & dropdowns have been updated. If so, the form is locked.
    Code:
    Private Sub Document_Close()
    Dim FmFld As FormField, bSaved As Boolean
    With ActiveDocument
      bSaved = .Saved
      For Each FmFld In .FormFields
        With FmFld
          If .Type = wdFieldFormTextInput Then
            If Trim(.Result) = vbNullString Then Exit Sub
          ElseIf .Type = wdFieldFormDropDown Then
            If .DropDown.Value = .DropDown.Default Then Exit Sub
          End If
        End With
      Next
      For Each FmFld In .FormFields
        FmFld.Enabled = False
      Next
      If bSaved <> .Saved Then .Save
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •