2
votes

Create a macro-enabled document that has a working macro first, but then stops working with the insertion of a Button. Instructions below.

The code:

Sub UpdateOptions()
  Dim bProtected As Boolean
  'Unprotect the file
  If ActiveDocument.ProtectionType <> wdNoProtection Then
    bProtected = True
    ActiveDocument.Unprotect Password:=""
  End If

  Select Case ActiveDocument.FormFields("Bookmark0").Result
    Case "Bookmark1"
        ActiveDocument.Bookmarks("Bookmark1").Select
        SendKeys "%{down}"  'Displays choices in drop-down field
    Case "Bookmark2"
        ActiveDocument.Bookmarks("Bookmark2").Select
        SendKeys "%{down}"  'Displays choices in drop-down field
    Case "Bookmark3"
        ActiveDocument.Bookmarks("Bookmark3").Select
        SendKeys "%{down}"  'Displays choices in drop-down field
  End Select
  If bProtected = True Then
    ActiveDocument.Protect _
        Type:=wdAllowOnlyFormFields, _
        NoReset:=True, _
        Password:=""
  End If
End Sub

Steps to set up the Word document:

  • Type into the document: Bookmark0
  • Insert a Legacy Drop down Form Field

    • List entries: Bookmark1, Bookmark2, Bookmark3
    • Name: Bookmark0
    • Run Macro on Exit: UpdateOptions (code above)
  • After the drop-down, type: Enter, Enter (insert two paragraphs)

  • Type: Bookmark1
  • Insert a second Legacy Drop down Form Field

    • List: 1, 2, 3.
    • Name: Bookmark1
  • After the drop-down, type: Enter, Enter (insert two paragraphs)

  • Type: Bookmark 2
  • Insert a third Legacy Drop down Form Field

    • List: 1, 2, 3.
    • Name: Bookmark2
  • After the drop-down, type: Enter, Enter (insert two paragraphs)

  • Type: Bookmark3
  • Insert a fourth Legacy Drop down Form Field
    • List: 1, 2, 3.
    • Name: Bookmark3

Your document should look like this at this point:

Bookmark0[dropdown]

Bookmark1[dropdown]

Bookmark2[dropdown]

Bookmark3[dropdown]

  • Save the document as a Macro Enabled document (docm).
  • Protect the document for filling in as a form

Testing:

  • Change the first drowpdown options only from the available listed options (Bookmark1, Bookmark2, Bookmark3)
  • Every time you change your selection you will be directed to the corresponding Bookmark Dropdown Form Field.
  • Should be working as expected.

Button conflict

  • Unlock document
  • Insert a Button at the end of the document below Bookmark3 [Button]

Your document should look like the same as above but with a button now:

Bookmark0[dropdown]

Bookmark1[dropdown]

Bookmark2[dropdown]

Bookmark3[dropdown]

[Button]

  • Protect the document for filling in as a form

Testing Button conflict:

  • Change Bookmak0 selection from (Bookmark1, Bookmark2, Bookmark3)
  • Every time you change your selection you will NOT be directed to the corresponding Bookmark Dropdown Form Field.
  • Every time you change your selection you will be directed to a field after the intended corresponding Bookmark Dropdown Form Field.
  • Macro is not working as expected with the presence of a Button.
  • Unprotect the document, delete the button, re-protect document, it will be working fine again.

I posted this questions on the links below too (Files available for download there.: http://www.vbaexpress.com/forum/showthread.php?65092-Odd-behavior-when-Command-Buttons-are-present

http://www.msofficeforums.com/word-vba/42422-odd-behavior-when-command-buttons-present.html

eileenslounge.com/viewtopic.php?f=26&t=32411

1
I can repro the issue, but know no reason for the behavior - it appears to be a bug of some sort. (Using Word 2010 BTW) Note that for me deleting the ActiveX control does NOT restore to the previous behavior! Could you use a MacroButton field instead of an ActiveX control? - Cindy Meister
Hi Cindy, thank you for looking into this. Unfortunately the department I'm creating the file for wants the Active X controls. Since the bug moves a few spaces ahead of the intended field, I'll be forced to code the macro to select a few fields prior to the intended one. This way, in conjunction with the odd behavior, I'll end up selecting the correct field. - RaudelJr
Agreed, just thought I'd present an alternative to having to do that as it makes the code less self-documenting. Be sure to comment it! If the bookmarks are named as in the repro steps it wouldn't be a big deal since the pattern in consistent. - Cindy Meister
I'll update the thread with the final. Thanks again Cindy, I really appreciate it. - RaudelJr

1 Answers

0
votes

The reason for the odd behavior was not found.

As a workaround I simply selected a Bookmark prior to the intended as shown in the code below.

Everything works as expected with Command Buttons.

Sub UpdateOptions()
Dim bProtected As Boolean
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
    bProtected = True
    ActiveDocument.Unprotect Password:=""
End If

Select Case ActiveDocument.FormFields("Bookmark0").Result
    Case "Bookmark1"
        ActiveDocument.Bookmarks("Bookmark0").Select
        SendKeys "%{down}"  'Displays choices in drop-down field
    Case "Bookmark2"
        ActiveDocument.Bookmarks("Bookmark1").Select
        SendKeys "%{down}"  'Displays choices in drop-down field
    Case "Bookmark3"
        ActiveDocument.Bookmarks("Bookmark2").Select
        SendKeys "%{down}"  'Displays choices in drop-down field
End Select
If bProtected = True Then
    ActiveDocument.Protect _
        Type:=wdAllowOnlyFormFields, _
        NoReset:=True, _
        Password:=""
End If
End Sub

Thanks you all that entertained this thread.

Raudel