VBA ActiveX control changes size after sharing - vba

I have a simple excel form with an ActiveX control (ListBox)
When I share this over email, the recipient does the following steps:
1. Open the excel
2. Enable Content for the macro
Now - the ListBox grows in size.
I'm unable to dynamically resize or figure out the exact event for "Enable Content".
Is there anyway I can retain the dimensions of the ListBox?

Your problem has nothing to do with sharing the document via email. It has everything to do with window scaling. To prove this to yourself try connecting to a projector with the the excel document open. Use some Active X controls and they will shrink or Expand. I've had this problem and found the only way to avoid it in a reasonable manor is to implant a form inside the excel document that holds all the needed controls or ensure the end user is not scaling their display in any manor.

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.

ActiveX controls changing to pictures (Excel/VBA)

My company creates workbooks for clients that contain ActiveX controls (in most cases we need the extra functionality as compared to Form Controls). When we create them, they work fine on our end, but when we zip them up and send them to our clients, they open the WB and the ActiveX controls are no longer ActiveX controls, if you right click it, it behaves like a picture.
I feel like this is due to the MS ActiveX debacle. I have had them run the Fix It and making sure all the MS Office components were closed, and had them reboot, but still nothing.
I am pulling out my hair, and have asked on other forums if anyone else has had this issue, and what they have done to fix it. Thanks in advance.
Could you please check in client system whether macros and active x controls are enabled in Trust center setting?
https://support.office.microsoft.com/en-us/article/Enable-or-disable-macros-in-Office-documents-7b4fdd2e-174f-47e2-9611-9efe4f860b12?CorrelationId=fe6a4d24-f4d9-4c6b-afa0-40d2828ad22e&ui=en-US&rs=en-US&ad=US
use above link if needed.
Try closing excel and deleting the .EXD files from your computer.
Source: http://www.mrexcel.com/forum/excel-questions/296750-command-button-turned-into-image.html
While saving the Excel document, save it as "Macro enabled workbook".xlsm
In my case, the worksheet prompts a non responsive error sometimes. When I reopen it, Excel repairs it and turns all active X check boxes (sometimes drop down list and command buttons) into images. It happened many times and I had to spend a lot of my time recreating them and liking to cells.
I experienced a similar problem when Excel repaired a file; this may be similar to what's happening with the Zipped file. FormControl Buttons don't appear to have the same problems but I see you want to keep the additional functionality of the ActiveX Control.
You may be able to make use of the code I created by inserting one Form Control button that runs the Sub RepairMissingButtons() that your clients can press to restore the images to CommandButtons. This currently fixes buttons for any selected sheets, so you'd want to first identify which sheets need to be selected (or add a form control button on each sheet that needs the repair done).
You'd need to modify the Sub CreateButton() to meet your needs (currently requires hard-coding the Caption, BackColor and any other ActiveX properties that may have been lost by the button being converted to an image):
Reverse Excel ActiveX command buttons erroneously converted to images after corrupted file repair
Having this problem, activex controls sometimes change to pictures.
Many places around the internet the answer is: Use form controls, well as far as I can tell form controls don't actually do anything useful.
Even Microsoft's support site says, recommend using form controls, how can that be the answer. being able to interact with VBA is as far as I can tell the only reason to use Microsoft applications instead of free ones.

Event when VBA code module is reset?

I am developing an excel sheet which has lots of ActiveX elements in it.
I am not using UserForm and all the data is loaded when an activeX button is pressed.
At this point a hidden excel app is created where various actions are performed.
This new app is then combined with other data etc etc.. based on what the user is doing on the front-end with the provided ActiveX controls.
My problem comes when the "project is reset", where all the objects are released from memory and everything.
If this was a user form I would have implemented "on terminate" event where I would close hidden all workbooks that I have used, basically clean after myself.
However this is not a userform and there is no way to detect when the "project is reset"(as far as I know) so the hidden excel app that I've created remain open but nothing points to it.
If I go to the task manager i see various "EXCEL.EXE" and the number of those is basically the number of times I have run the tool.
So, is there any way for me to detect when VBA is about to "stop the project" in the same was as there is "terminate" for user forms so I can delete unsused stuff and reset elements in the spreadsheet?
Thank you!
I have found that there is no corresponding event to catch this.
Best way to handle this is to create a hidden userform object which will "hold" all global variables and objects that I define. I can then implement a UserForm_Deactivate event to catch when the form is exiting and clean up after myself.

Excel Button Growing

I'm working on a workbook in Excel 2010 that someone else created (I don't know which version they were using) with a button in it that invokes a macro. There are a lot of macros defined, so I'm right-clicking on it to find out which one it calls, but the context menu doesn't appear. Instead, when I click off, the button gets larger. I can make this happen as many times in a row as I'd like. There is another button the same worksheet that has the same context menu problem, but instead of growing, the text shrinks each time. There is another button that functions normally when I do this.
Growing buttons in Excel is a fairly common issue, with several theories about why this happens, including the use of multiple monitors or using proportional fonts. I have yet to see a definitive answer about this, but there are several workarounds that may work for you.
Delete and re-create the buttons.
Programmatically set the height and width of the buttons when the workbook is opened and when a button is clicked.
Select the button with another object or two on the sheet and group them.
Don't use them at all.
My personal choice is #4. As an alternative to buttons, I either use hyperlinks or shapes with macros assigned to them.
I think you want to enter "Design Mode" in the work book:
You should be able to right-click on the button to see what it does after that.
I have this same issue. I have two Excel workbooks with similar buttons on each. This only happens on one of them, but it happens every time I open that file.
I have found a sort of work-around. I open a blank Excel document, then I open the affected one and the buttons do not change size any more. When I open the second one, I have to drag it into the window with the already-open file. If I double-click on it, it opens in a new window and the problem remains.
I have the same issue sometimes. In my case, I could replicate it 100% on one file but it was inconsistent on an virtually identical file. I also found the size error wasn't permanent -- I could save and reopen the file to restore the button's appearance. I could also create a new window and then discard the damaged window.
For me, the button resized when I accessed the sheet's HPageBreaks collection. I was able to avoid the problem by temporarily changing the window view as follows:
ActiveWindow.View = xlPageBreakPreview
' do pagination stuff using HPageBreaks
ActiveWindow.View = xlNormalView

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))