Search code examples
vbams-wordms-office

Hide command button on word doc


I have a .doc with a command button (cmdStart) which opens a form. After populating the form I click a button to close the form and populate the .doc.

I want to hide the initial cmdStart on the .doc as well when the form closes, Ive tries document.shapes(1).visible=false and cmdStart.visible=false but none seems to work.

Any ideas?

thanks (ps I cant just opne the form from the autonew, I need the cmdStart visible to begin with)


Solution

  • You have several options to deal with that. However, you won't be able to hide your command button but you will be able to remove it.

    Removing a command button can be done by the following code:

    Private Sub CommandButton1_Click()
        CommandButton1.Select
        Selection.Delete
    End Sub
    

    (Note that usually you would be able to hide text in Word by setting the font hidden, e.g. by calling Selection.Font.Hidden. However, this does not affect controls.)

    If you deleted the button and you need it later on again you will have to re-create it. In that case it might be a good idea to mark the position of the button with a bookmark.

    Another option would be to use a MACRO button field. Such a field can be inserted from the Insert Field dialog and can be used to launch a macro.