none
All Command Buttons Getting Disabled / Deactivated After One is Clicked RRS feed

  • Question

  • Hello.  Below is the code in question:

    Private Sub CommandButton1_Click()

    Dim oSec As Word.Section

        With Application.Dialogs(wdDialogFileSaveAs)
            .Name = "c:\My Documents\Document 1.doc"
            .Format = wdFormatDocument
            .Show
        End With
       
    End Sub

    Private Sub CommandButton2_Click()

    Dim oSec As Word.Section

        ' Delete Section 4 prior to saving
        Set oSec = ActiveDocument.Sections(4)
            oSec.Range.Delete

        With Application.Dialogs(wdDialogFileSaveAs)
            .Name = "c:\My Documents\Document 2.doc"
            .Format = wdFormatDocument

            If .Show = False Then
                MsgBox "You did not save the document.  Please be aware that the sections you wanted to keep private have not been removed!"
                ActiveDocument.Undo
                Exit Sub
            End If
        End With

    End Sub

    Command Button 1 saves the document as is; command button 2 deletes Section 4 of the document prior to saving, and both buttons worked fine until I inserted the "If" command code set under the code for command button 2.  I inserted the "If" command code set for cases where the user inadvertently pressed command button 2, so that Section 4 of the document can be re-inserted via the "ActiveDocument.Undo" command.  The problem is that if a user has inadvertently clicked command button 3 and consequently clicks "Cancel", Section 4 does get reinserted into the document successfully but then neither command button 1 or command button 2 work any more.  They do work again if the document is closed and re-opened.

    Any assistance would be appreciated!

    Thank-you.


    • Edited by R.Shaw Tuesday, October 6, 2015 3:20 AM
    Tuesday, October 6, 2015 3:19 AM

Answers

  • Hi R. Shaw

    Interesting, the changes I proposed stopped the renaming of the CommandButton (at least for the duration of the Word session).

    If you're getting an error it would help for you to give the message text.

    However, I just noticed that you don't have any code for unprotecting/reprotecting the document, but it is protected as a form. In order to perform a deletion and/or Undo document protection needs to be removed, then reinstated afterwards.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 9, 2015 5:37 PM
    Moderator

All replies

  • Unfortunately, you don't give us any details about what Command button 3 does?

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 6, 2015 5:43 PM
    Moderator
  • Sorry, where you see "command button 3" in my narrative above, that should read "command button 2".  I don't have a third command button.

    Thanks for your reply.

    Tuesday, October 6, 2015 5:47 PM
  • What happens if you comment out Exit Sub? Do things work, then?

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 6, 2015 5:53 PM
    Moderator
  • No, command buttons are inactive even after commenting-out "Exit Sub".
    Tuesday, October 6, 2015 5:57 PM
  • Hi R.Shaw,

    I am trying to reproduce this issue in Word 2013 however failed. Here are the test steps:
    1. Create a document with 5 sections

    2. Create a userform with two command button with code above

    3. Show the userform and click the button2 and cancel the file save dialog

    The command buttons still works well for me. Please feel free to let me know if I misunderstood. Would you mind sharing a demo document to help us reproduce this issue? You can upload it via OneDrive and please remove the sensitive information before you uploading.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 7, 2015 1:43 AM
    Moderator
  • Thanks for your reply.  Yes, you understood the issue perfectly.  Unfortunately, I cannot upload due to its content but since you were able to have it work successfully in 2013, I am wondering if the code I am using is too advanced for the version of Word we are using.  We are using 2003 . . . yes, 2003.

    Do you think that is the reason for it not working for me and if so, can you recommend how I would change the code?

    Thanks for your time and effort!

    Wednesday, October 7, 2015 4:29 AM
  • Hi R.Shaw

    The problem may not have anything to do with the version of Word. The particular document, or the Normal.dot on the system, or pretty much anything could be the cause. That's why Fei requested a document that exhibits the behavior.

    Are you able to create a new document with a bit of text plus the two controls with the macro code - IOW doesn't contain any confidential or proprietary information - that you can share?

    Or is it possible that the behavior is limited to the one document?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, October 7, 2015 2:54 PM
    Moderator
  • Hi R.Shaw,

    I am also share the demo document via link below, you can download it and open the document and VBE run the userform1 to check whether it works for you.

    CommandButtonSave.docm

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 8, 2015 7:36 AM
    Moderator
  • Sorry for the delay and getting back to you both, and thanks for your help.  I will download the document you provided and get back to you soon.
    Thursday, October 8, 2015 2:55 PM
  • Both command buttons work in the document you provided me.  I will attempt to change my document and upload to OneDrive.

    Thank-you!

    Thursday, October 8, 2015 3:07 PM
  • Hello Fei.  Here is a link to the document: http://1drv.ms/1LBQ7mJ .

    Please note that for reasons I can't explain, the "undo" command for the second command button is no longer "undoing".  I did have to unprotect and reprotect the document using VBA, but the undo command line doesn't do its job whether the unprotect/protect code is there or not.

    Thursday, October 8, 2015 5:12 PM
  • I'm pretty certain now that this has everything to do with document protection.  "ActiveDocument.Unprotect" and  "ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True" inserted at the beginning and the end of the code for both buttons are not doing the trick however.
    Thursday, October 8, 2015 5:56 PM
  • Hi R.Shaw

    CommandButton_2 is being renamed on my machine to CommandButton_21, then _211, etc. Thus, there is no code for that control name and nothing is happening. 

    If I comment out the following two lines in the If, things work correctly

                'ActiveDocument.Protect _
                    Type:=wdAllowOnlyFormFields, NoReset:=True ' Added!
                'Exit Sub

    Since you protect the document in any case, no need to have that line in the If, and then the Exit is also not required.

    Why this is happening I can only guess and that would be that Word is hitting a "race" situation with Undo, protection and exiting and renaming the control is a result of Word recovering the conflict without crashing.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 8, 2015 6:17 PM
    Moderator
  • I see what is happening now . . . it's not an issue of protection but the command buttons keep getting renamed.

    Whenever I choose "Cancel" after clicking the second command button, it renames the second command button (by adding a "1" to the end of the name).  I have the "undo" working again from your suggestion - thank-you.  Should there be an "else if" in the "If .Show = False Then" routine that would prevent the re-naming of the command button?

    • Edited by R.Shaw Thursday, October 8, 2015 7:03 PM
    Thursday, October 8, 2015 6:43 PM
  • Cindy or Fei . . . will this make it easier?  I changed my code for the second command button so that the section is not deleted unless ".Show = True", and incorporated Cindy's recommendations.  The problem is I now have a run-time error but I was hoping this code would be more manageable.  It appears that the issue with the name being changed automatically for the command button may be resolved with this code (?).

    The error occurs at "oSec.Range.Delete".  The problem may be with its placement because previously, it was near the top and there were no issues with it.

    Private Sub CommandButton2_Click()

    Dim oSec As Word.Section
        ' Identify Section for deleting
        Set oSec = ActiveDocument.Sections(4)

        With Application.Dialogs(wdDialogFileSaveAs)
             .Name = "c:\My Documents\Document 2.doc"
             .Format = wdFormatDocument

             If .Show = True Then
                ' Delete Section 4 prior to saving
                oSec.Range.Delete
                Else
                    MsgBox "You did not save the document.  Please be aware that the sections you wanted to keep private have not been removed!"
             End If
        End With

    End Sub

    Thanks.  Hope you can help.  Feels like I'm getting closer.

    Friday, October 9, 2015 2:26 AM
  • Hi R. Shaw

    Interesting, the changes I proposed stopped the renaming of the CommandButton (at least for the duration of the Word session).

    If you're getting an error it would help for you to give the message text.

    However, I just noticed that you don't have any code for unprotecting/reprotecting the document, but it is protected as a form. In order to perform a deletion and/or Undo document protection needs to be removed, then reinstated afterwards.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 9, 2015 5:37 PM
    Moderator
  • I re-inserted the protection code.  I think that did it!  What a relief . . . thanks for your assistance.
    • Edited by R.Shaw Friday, October 9, 2015 5:50 PM
    Friday, October 9, 2015 5:49 PM