RaudelJr
RaudelJr

Reputation: 25

Odd behavior when Command Buttons are present. Word VBA

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:

Your document should look like this at this point:

Bookmark0[dropdown]

Bookmark1[dropdown]

Bookmark2[dropdown]

Bookmark3[dropdown]

Testing:


Button conflict

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

Bookmark0[dropdown]

Bookmark1[dropdown]

Bookmark2[dropdown]

Bookmark3[dropdown]

[Button]

Testing Button conflict:


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

Upvotes: 2

Views: 101

Answers (1)

RaudelJr
RaudelJr

Reputation: 25

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

Upvotes: 0

Related Questions