Word - document object code reference - vba

I have a word document on which I have created a checkbox from the Control Toolkit. I have added a text box to the word document that I want visible only when the check box is clicked. I have the code I need to make the text box visible based on the checkmark from another similar piece of code.
What I need is the name of the text box that I put on the document so I can refer to it in my code...the other text box is 27, do I just need to try incrementing numbers until I get lucky or is there some method to the madness where I can look to see what the number is?
Private Sub OtherChk_Click()
If OtherChk = False Then
ActiveDocument.Shapes("Text Box ??").Select
Selection.ShapeRange.Visible = msoTrue
Else
ActiveDocument.Shapes("Text Box ??").Select
Selection.ShapeRange.Visible = msoFalse
End If
End Sub

Since with word you can be talking about Content controls, Form fields or Older style activex controls, each of which have their own quirks, it's tough to answer a question like this directly.
For instance, I created a new word doc (word 2010), clicked the developer tab and dropped down the "control toolbar" button in the ribbon, there's "Legacy forms" and "activex controls" listed,
I clicked the DESIGN MODE button, then chose the Textbox activex control, dropped on on the form, right clicked it and selected "Properties"
that pops up the VERY old school looking properties browser (I don't think that code has been touched in word in years), but, at the top, you have the controlname, defaulted to TextBox1
You can change that name to whatever you want, and then reference the control via that name.
You MAY need to do a for-next through all the controls checking the NAME property (I'm not sure off hand if the indexed collection will index on name or just on the index number).

Related

Word document text boxes "remember" what was in them previously

I am using a VBA userform to randomly generate values to be sent into a Word document to provide random questions on various math topics (for my students).
This seems to work well except that when I exit the userform (me.hide) and the values are sent and focus is back in the document, the previous values are still briefly visible every time I move the mouse.
This only happens for an instant. The correctly sent values appear if there is no mouse (or window slider) activity taking place. If I view another application and do some activity there and then return to the Word document the display is fine as well.
The flipping of values is visually annoying. Has anyone out there encountered a similar issue?
To reproduce:
Create a Word document with an ActiveX Textbox and button. The button activates a userform which also contains a Textbox and a button to send the Textbox value back to the Textbox in the document.
In my case, when the button in the document is clicked the form opens and the value does get returned to the Word document, but when rolling the mouse or moving the slider at the edge of the window the previous value in the text box briefly flickers into view. Here is the code from the document button:
Private Sub cmdOk_Click()
ThisDocument.TextBox1.Value = TextBox1.Value
Me.Hide
End Sub
I am using Word 2016 and Windows 10.
I tried to use the "new" Word textboxes but I do not know how to communicate with them from a userform. It is so simple using the "old" text boxes.
ActiveX controls were designed for VBA UserForms. It's possible to insert them on the surface of a Word document or Excel spreadsheet, but they don't always behave optimally. Content controls (and the legacy form fields), on the other hand are designed for the surface of a Word document.
In order to write to a content control you can use code like this:
ActiveDocument.SelectContentControlsByTitle("name").Item(1).Range.Text
Closer to what you're trying to do with identifying the content controls could be:
ActiveDocument.SelectContentControlsByTitle(TextBox1.Name).Item(1).Range.Text = TextBox1.Value
Why it's so complicated: The team that designed content controls wanted to avoid the problems people ran into with bookmarks and form fields, that a name could not be duplicated in a document. It's possible to give the same name to multiple content controls. So it's not possible to identify a content control using Document.ContentControls("name").
Instead, Document.SelectContentControlByTitle needs to be used, which returns an array of content controls. If there's only one (or if you want only one), then you can add on .Item([index]) to get the single content control directly.
Rather than type in a static name, you can query the UserForm's textbox control for its name, if that makes things easier for you.

Modify Context menus

I have the following code to add a menu item on the Word 2013 right click menu:
Sub CreateMenuItem()
Dim MenuButton As CommandBarButton
With CommandBars("Text")
Set MenuButton = .Controls.Add(msoControlButton)
With MenuButton
.Caption = ChrW(&H2714) '"Correct"
.Style = msoButtonCaption
.OnAction = "InsertMark"
End With
End With
End Sub
The issue is that the new menu item is not displayed when I right click on a Text box or a table.
How do I remove a menu item that is not used by me, for example "Translate"
Please Assist
Thinus
Word provides a completely different CommandBar object for each context-sensitive menu, rather than dynamically changing the contents of a single menu. That means you need to make the change for each context in which your command should appear.
You can generate a list of CommandBars by looping the collection and writing the various properties out (for example, in a new document). Then you have to inspect that list, pick out the likely candidates, run your code then see if you guessed correctly.
You want to be very careful where these changes are SAVED in Word. If you don't specify it specifically, Word could save the change in one place and delete it (assuming you "undo" your changes) in a different one. This can really muck up the Word configuration and make users extremely unhappy. So always use Application.CustomizationContext at the beginning of code that changes the CommandBars. The CustomizationContext can be any Document or Template object.
NOTE: The CommandBars object is actually deprecated as of Word 2010. From Word 2013 onwards the context menus should be customized using Ribbon XML. Just saying... It's possible that in a future version your code will no longer work.

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 select (or copy) text from a Word 2010 ActiveX label?

A Word 2010 document has an ActiveX label that displays some text. Is there any way to make the text selectable, or otherwise copy-able, from a user's point of view, so he can paste it somewhere else?
Use case: I give the form to someone, they fill it out and return it to me. The element in question is a Label which, when double clicked, produces UserForm1 which has a ListBox on it. Once one or more selections are made and the user presses OK on UserForm1, the Label in the Word doc gets updated. I then receive the form back, and want to right click the label, copy the text, and paste it into an email.
You can't, at least from an end-user's point of view. Let me explain.
I started out wanting to achieve this with a Label, but soon found I couldn't copy the text that was displayed there using conventional ctrl-c or right-click > copy.
So, I switched it up to a TextBox. This worked somewhat, and the data was displayed, though with one flaw: Word 2010 seems to put a bunch of unselectable space between the last line in the TextBox and the bottom of the TextBox, making most the contents hidden until you scrolled back up to the top of it.
Here's what it looked like:
Notice all the empty, unselectable space below the last item in the list?
The solution to my problem of 100% of the text not being displayed in the box was to use this line of code, which places the cursor at the top of the text after the values are placed in the TextBox:
ThisDocument.functionalComponentsTextBox.SelStart = 0
This basically simulates the user manually clicking in the TextBox and pressing the Up key until he reaches the top of the TextBox. With that, the selections from the ListBox are now stored in the TextBox, the contents of which can be copied and pasted wherever as part of our business processes.
Here's what it looks like after: a perfect match when compared to the properly-displayed Label approach! Added bonus: the text is selectable, and the TextBox is customizable so I removed the border from it... can't tell the difference!

Word add-in, custom layout

Is it possible to create a custom layout, existing ones are:
Print layout
Full Screen reading
Web layout
Outline
Draft
These can be found in the View Ribbon under the group Document Views.
My aim is to get my own layout button in either the existing View Ribbon (if it is possible to modify it) or add a new layout to my custom Ribbon.
Thanks in advance!
This answer is going to provide information on how to change standard settings of any view type control and associate these changes with certain document. This will not work with all documents and will not change the control action for whole Word Application but for one document. Operation could be repeated for few document and almost all Word button.
Important! I'm not using English version of Office application therefore some description will not match exactly to what you have. Tried and tested for Word 2010.
There are following steps to go:
Open new document- one where control should work according to your private expectations.
Go to View >> Macros >> Show list of macros
In the combo-box below middle of the Macro window choose something like Word application commands (or Word macros or similar). As a result you get list of lots of macros names.
You need to guess which of the macro is associated with ribbon control you are going to change. Use common sense and logic to find it. Sometimes two or three seems to match and possibly you will need to make a try.
A) let's try to change behaviour of draft/pending/working view ribbon control. one rounded red below:
B) find macro ViewNormal (but not ViewDraft)
C) select this macro on the list
Change back on the combo-box list to your document (while keeping your chosen macro selected)
Press Create button on the right in the macro window. You will be moved to VBA Editor to the following code:
Sub ViewNormal()
'
' ViewNormal Makro
' Zmienia widok edycji na normalny
'
If ActiveWindow.View.SplitSpecial = wdPaneNone Then
ActiveWindow.ActivePane.View.Type = wdNormalView
Else
ActiveWindow.View.Type = wdNormalView
End If
End Sub
This code is responsible for working of chosen ribbon control.
First, let's check if we can take control of ribbon button- add MsgBox "Control taken" at the end of the code, before End sub. Back to Word App and press button on the ribbon which result should be- setting of chosen view and our message box.
Now you need to change your code accordingly to set your view as you need. Use VBA for that.
Save document as .Docm and all the changes will be applied to the document each time you press chosen ribbon button.