MS Excel VBA Forms - Hide Control - vba

I am using MS Excel VBA forms.
On my form, there is a drop down list and based on the selection changed in dropdown, I want to hide and show controls.
I am able to hide the control by setting visible to true on control, but they still occupy space on the form.
Is there any way to collapse control while hiding controls on VBA forms (somewhat similar to WPF visibility = collapse)
Do let me know if you need any other information.

Related

Resize form, subform, content and controls in MS Access

I have followed the suggested code to resize a form Access 2010 VBA Forms - Automatic Form Resize.
I have a few issues with this code:
When Access opens, the form is automatically opened but it is not recognizing this code. If I open the form in Designer and then go back to the form, it has the code applied. What step would the EventProcedure need to be applied to? Form_Open?
The subform is not resizing - there is a subform within the original form. I have added the code to the primary form and the subform is not affected. If I add the code to the subform, also, then the font on the subform is HUGE. Could it be using the acHeader and acFooter from the main form? And is there anything that I should be considering for the subform so that it recognizes its own window size?
When the form initially opens without the code activated, the subform is overlapping the tabs of the main form. The subform is the content that is supposed to display on the tabs, so I do not understand why it would overlap the tab.
Since I am very new to working in VBA, any assistance is greatly appreciated. We have multiple forms and subforms within an existing application and I am tasked with either resizing everything manually to larger font, or finding a solution that will allow the user to resize the page/font themselves - which is the reference I have above. (questions/13337942)
Code was applied but is not executing when form opens with the main application - only after opening in Designer.
Subform is not being affected. When adding the same code to the subform, the font in the subform is a few times larger than the font in the main form.
When code is not activated (upon initial open with application), the subform is overlapping the tabs of the main form - and the subform resides within the tab.

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.

Determine "active" ActiveX control

Using VBA, how can I determine the ActiveX control currently selected in a Word document while in Design Mode? That would be the control for which the properties are offered in the context menu.
It isn't possible. While a control is selected Word is in Design Mode and code can't execute. When not in design mode no control is selected.

Excel AddIn - Keeping windows form always visible while w/in Excel

First of all, thank you for your time and assistance in reviewing this!...
I'm trying to upgrade an Excel VBA workbook to a VSTO Excel Add-in in VB.NET using VS 2010. In the original (i.e.- VBA) version I have a modeless UserForm (called frmMain) that floats on top and is visible at all times while the user is still within the Excel application, but is not visible if the user moves to another window outside of Excel.
For example, within Excel the user can click on any worksheet tab, select any cell, etc. and the UserForm is still visible. This is exactly how I'd like it.
The problem is, that in the new VSTO add-in, I can not get the Windows form to mimic this same behavior.
I use frmMain.Show() to show the form as a modeless form, but the moment the user clicks an Excel worksheet (i.e.- activates a worksheet) the form becomes hidden behind the worksheet.
I can manually Alt-Tab to bring the form back into view, but I need it to always remain in view - floating on top of the Excel worksheets so long as the user hasn't left the Excel application.
I tried various things, including setting the form to TopMost, however, that causes the form to be TopMost everywhere - including outside of Excel. Worse than that, if the user does anything that would normally result in Excel's launching a dialog box (e.g.- closing an open workbook, raising the alert "Do you want to save the changes...") the alert dialog box itself is hidden and inaccessible behind the frmMain form (since the frmMain is TopMost).
How can I get my form to behave in the desired way (i.e.- the same way it did in VBA)?
Thanks!!!
Rob
You should take a look at Custom Task Panes which can be docked or floated within the Excel application. You could also look into the COM interfaces for a lower level connection (see related SO Post) - although Task Panes are really what this type of behavior was intended for.
This method might work (worked for me):
Create a worksheet_selectionChange event handler inside your form class
Put these lines in this event handler:
Dim FormHandle As IntPtr = Me.Handle
Me.Visible = False
Me.Show(NativeWindow.FromHandle(FormHandle))

Clone Winform control

I have a groupox in a VB.NET winform app. The groupbox contains a few Labels, Textboxes and Checkboxes. This was created by simply dragging the controls out of VS toolbox.
What I need to do is take the 1 Groupbox and at runtime create multiple Groupboxes to display based on user selection. Instead of dynamically creating the Groupboxes and other controls nested inside, is there a way to clone or copy the original one.
Then I'd just change the properties. Label text, Textbox text, etc. And the location of the Groupbox in the layout.
What you could do is create a user control based upon your groupbox which would allow its reuse. You could then create the instances you require at runtime and add them to the form.
Maybe suggest that you look into creating user controls for winforms.
Could this be a help to you?
How to Clone/Serialize/Copy & Paste a Windows Forms Control