Hide command button on word doc - vba

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)

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.

If you really wanted to "hide" the button, you could set the Height and Width to 0.75 and it's virtually gone. Then resize back to "show". I've also seen people put them inside tags and hide the tag. Hope this helps

Related

Using VBA to allow a checkbox to hide/show content in Microsoft Word

I've gone through a number of tutorials and instructional videos trying to achieve my intended result of simply allowing a checkbox in my form to hide content when selected, or re-show it when being de-selected, but nothing seems to be working.
Currently, I've created a bookmark for the content I want hidden, and try to call his this in VBA with the following statement - which a number of resources indicated as the solution:
Private Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
But despite doing this, selecting the checkbox has no affect on the content.
Is there something additional I'm missing (I'm using Microsoft Word 2013).
Your code worked fine when I tested it, but I ran into a few issues since I've never messed with userforms/checkboxs in Word VBA and I suspect you may have the same.
For instance, the first thing I did was create Insert --> Module. This is incorrect. What you want to do is Insert --> Userform then drag a checkbox over from the ToolBox
https://smallbusiness.chron.com/use-check-boxes-word-54673.html
Then double click the checkbox to bring up the "module" it would run, notice there is no module in the side pane! Edit the module, then go back to the userform and press F5. You should be presented with a checkbox that will hide/unhide your text.
Here is my module:
Public Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
Here is an image:
Note: I didn't test how to insert the checkbox into the word doc, I'll leave you some of the learning!
Update:
This sub will make the CheckBox appear when run, but I'm not sure the logic you would use to call it, perhaps an event like opening of document?
Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub
This could be called via a keyboard shortcut or event, but this will cause a "pop-up window". If you want an inform checkbox you may need to look into using this Legacy Form/Checkbox. I was following the URL from above but I think it's dated.
Update:
You could also add this easily to a toolbar, but that isn't likely what you want. I found the best way is to use a "field code" see --> https://word.tips.net/T001571_Assigning_a_Macro_to_a_Button_in_Your_Text.html
I was able to get this to work by pressing CTRL + F9 then typing { MacroButton Loadform ClickMe} then clicking this text. This may be the best bet as using an image seems not to be a great idea.. (https://answers.microsoft.com/en-us/msoffice/forum/all/using-graphic-with-macrobutton/a9c1ef3b-cf1f-48ba-92a8-c44bffcdc131) & (http://www.addbalance.com/usersguide/parts/macrobutton_fields.htm#Different_behavior)
Gif Example:

UserForm Option Button Controls Crash Excel After Toggling at Runtime

I'm having a problem with radio option buttons on a UserForm I have created in excel. When the UserForm is loaded it is defaulted to OptionButton1. If I toggle to OptionButton2 and then toggle back to OptionButton1 excel will be unresponsive and crash when I close the form. If I just toggle to OptionButton2 and close the form or don't toggle them at all, everything will be fine. It only happens if I toggle more than once in a given runtime. Also, the option buttons are on a frame on the userform if that makes any difference.
I have commented out ALL code that is triggered by events attached to the controls and it still happens. Also, the code works perfectly fine in the case that I only toggle the options once. It will crash simply by changing the option to 2 and back to 1 without executing any other code. I have another option button group on the form and I don't have any issues with that group. I can toggle them as many times as I want.
I'm not able to share my actual workbook and have been unsuccessful recreating the problem in an example workbook. I'm not sure what else I can provide that would be of use. If anyone has any suggestions based on my description of the problem, it would be greatly appreciated. I have run out of ideas.
Things I've tried:
Removing all code associated with the controls
Changing and removing the group name for the set of option buttons
deleting the option buttons and remaking them
changing the names of the option buttons
copying and pasting the existing working option buttons and renaming them
EDIT: I was able to save as on my original workbook and delete everything out except the working and broken option controls. Here is a link to the sample book which is a bare skeleton of my actual workbook:
Link to workbook example.
Even with everything gone, the problem still happens. If you open the userform, you can toggle between the "Option A" and "Option B" buttons, but if you try to toggle between "Option 1" and "Option 2" excel will crash when you close the form.
Your User-Form and /or controls are corrupt. In line with what Commitern suggested, instead of full export and recreation, try this:
1.Delete your faulty user form. in this case `Test`.
2.Save and close Excel.
3. Go to C:\Users\<<yourusername>>\AppData\Local\Temp\VBE
4. Here delete all the exd files.
5. Re-open workbook add new form, name it `Test` and all option buttons.
All works fine.
Remember if you try to add a new user form and rename it as the old one Test without saving the file, you will get error.
https://support.microsoft.com/en-us/kb/244238

Form Option Button Reset Macro

I am looking for a simple macro I could activate with a form control button that would clear all of the form control option buttons on my sheet.
I have lists of options for industrial part specifications, of which only one may be selected per section. However, once one is selected, the form control option button stays filled in. I previously circumvented this by using checkboxes, where if you click the box again it will remove the check mark. However once I learned that I was to create the form in such a way that only one option could be selected per section, (for ease of use in case a less computer minded person were to use it) it became clear that option buttons were the right way to go.
So I need a simple macro that I can activate with a button that will clear these option buttons all to blank, as if none were selected. I have looked and tried some strings of code, but none have worked so far. Perhaps I am missing something obvious or looking for the wrong thing, but I dont think I am.
I have checked the following pages and tried their code:
http://www.mrexcel.com/forum/excel-questions/689865-how-clear-all-checkboxes-option-buttons-list-boxes-form-3.html
Re-setting all option buttons at once
http://www.excel-easy.com/vba/examples/option-buttons.html
I feel like this should be simple. A VBA macro code that will reset the FORM CONTROL Option Buttons to blank (which I believe is the false state?). No need to worry about having specific ranges to clear; one button to reset the sheet will do perfectly.
Thanks in advance for any help.
Cycle through the Shapes collection:
Sub Reset()
For Each vCtrl In ActiveSheet.Shapes
vCtrl.DrawingObject.Value = False
Next
End Sub

How do you add a help button to Excel userform

I have a few different userforms in Excel 2007 right now and was wondering if I could add a "?" button next to the close symbol in the userform.
Alternatively, is there a way to display some text when I hover over a specific label
The form property "WhatsThisButton" displays the question mark icon next to the close button, but this does nothing without creating an actual help file and assigning it to your form, this is not an easy thing to do. Far easier is to display text as you have described, each control has a "controlTipText" property that will display whatever text you enter in there, when your user hovers their mouse over the control

How can I trigger a click event of an invisible control?

I have a background for my VBA project which contains pictures. These pictures I would like to make clickable, in the way that the click triggers a click event. I've tried adding pictureboxes, buttons and labels but I couldn't manage to make them invisible without making the click event not work (setting the Visible property to false causes this). Googling the problem, most solutions made the button look transparent by changing the color and borderstyle so the control appeared to be a part of the mono color background. I however have a specific picture that I would like to see beneath the button.
To add a macro manually
Right-click on the picture you want to assign the macro to.
Choose Assign Macro from the options.
Assign the macro that you want to run when the picture is clicked.
Click OK to finish.
To add a macro programmatically
If the macro is in the same workbook:
ActiveSheet.Shapes("Picture 1").OnAction = "ThisWorkbook.test"
If the macro is in a different workbook:
ActiveSheet.Shapes("Picture 1").OnAction = "'Book1'!test"
For more examples, look at this and this.