How to add button to word 2007 document, and handle click event with VBA? - vba

How do I:
add button to my ms word 2007 document (i would say this would be trivial task, but can't even figure THIS out)
handle click event of this button in my VBA script
set text of my text control (with tag "text1")

I don't know if it is what you intended, but to add a button in the document, open the Developer tab. Click the Legacy tools folder (in the Controls group) and click the Command Button (Active X Control). This will put a button in the document and make Word go into design mode.
Double-click the new button and you will enter the VBA editor in the event handler for the button.
Enter code to manipulate the text in TextBox1:
TextBox1.Text = "Hi there, VBA master!"
Click the design mode button to exit design mode, and you're good to go; click the new button to set the text.
If you meant to add a button in the ribbon? A little more complicated, but not that hard. Leave a comment, and I'm sure we can work it out as well.

Related

Under Excel O365, the custom ribbon tab does not always populate

Using an .xlam project with a fully working custom ribbon, I have found that the new tab will sometimes show nothing when clicked on. It is intermittent. The global RibbonUI object is available and running an .invalidate will then show the ribbon. However, the .invlalidate attempts prior to the tab being viewed do not seem to change this behavior. I have to view the tab and then run the .invalidate manually (with a macro button) to see the ribbon appear.
I am scratching my head trying to find a slick way of making this new custom tab populate 100% of the time.
For the benefit of those that come across this issue, I have found the root cause and a solution. This is Excel O365 specific.
If you have a custom ribbon tab set up and working, try this. Open Excel, create a blank workbook, then double-click on a cell to put it in "edit mode". Click on your custom ribbon tab. Blank, right?
This "cell in edit mode" is what was causing Excel O365 to fail to show my custom ribbon. Excel O365 starts up on the "File" screen. Prior versions always opened to a blank sheet. If you click on "Blank workbook", the ribbon displays fine. If you DOUBLE-CLICK on "Blank workbook", the first click opens the workbook and the second click puts a cell into ""edit mode". In this state, clicking on your custom ribbon tab shows nothing.
The work-around
Private Sub WorkbookActivate(ByVal Wb As Workbook)
Application.SendKeys "{ESC}"
...
You may see a cell go into edit mode for one blink but it then exits edit mode. Click on your custom ribbon tab. It will now show the ribbon controls as designed.

How do I make a userform appear on demand in Microsoft Word?

I can make it popup on opening the document, but what if I close the userform and want to it to show up again in the same document? I know in excel you can add a button onto a worksheet directly and make it show the userform on click, but this button is unavailable for microsoft Word. Is there a solution besides initiating the script by hand?
To display a VBA userform, you can trigger it from a macrobutton field, from a form field, from a shape, from a QAT button or from an ActiveX button. There are probably a couple of other methods I'm not remembering at the moment. Each is a little different in the exact steps, but all will run the command:
UserFormName.Show

Add a button to Word 2010 for a macro at document level, not installation level

I created a macro for a document. I want a button so that anyone can run the macro easily.
I only want the button to appear on that document, and when I share the document the button appears.
I tried using the built-in File -> Options method to add the button, but the result is that
a) the button isn't shown when I send the document to others
b) it appears when I open any Word document
With the code in "ThisDocument" you can add a "button" to the document rather than the ribbon.
http://wordribbon.tips.net/T008658_Assigning_a_Macro_to_a_Button_in_Your_Text.html
"This is all instituted through the use of a field code.
It may be a bit of a misnomer to refer to the result of this field code as a "button," because no graphics are involved whatsoever, although you can create your own graphic and embed it into the field.
The syntax for the field code is: MacroButton MacroName Display"
If the final document should not have a button, you could revise the code to first generate a new document without the button.

VBA button action issue on cell edit

I have an excel toolbar created by VBA macros. When cell is in edit mode, I'm unable to perform any toolbar actions, the event itself is not getting triggered. Button events are getting triggered when we come out of that editing cell.
Is there any way to handle this? I want to allow users to click on buttons even when cell is in editing mode.
Thanks.
VBA will never run if a cell is in edit mode. If you are typing inside a cell, VBA is halted. Infact, if you press ALT + F11 and bring up the VBA editor window, and then go to edit a cell in Excel, you'll find you cant ALT + TAB back to the editor, it's gone. You cant even get back to the editor whilst you are in edit mode. (Obviously when you click outside Excel, the cell is no longer in edit mode, so you can select it from the windows tray)
You may have to use a form to allow users to edit cells, this way VBA will still run whilst the form text box is being typed in. Then submit it to the cell when the text box is complete

How do I launch a Word VBA Application from a Form (not a document)?

I created a Word Application using VBA. At present, I launch it from the IDE associated with a particular document. When my app launches, I click a Command Button on a Form to begin processing my data.
Question: How do I launch this application directly from the Form? Perhaps I can fashion my Word document to look like my Form.
If you don't need a form to collect information from the user, you can run macros directly from Word. Go to the View Ribbon tab and click the Macros button on the far right (or press Alt-F8).