Reputation: 1463
I wrote a macro method to auto-size the columns in an Excel sheet upon opening the sheet. However, this solution does not work in the case that the user has not enabled macros in Excel.
Is there any way to check if the user has VBA macros disabled in Excel, and then enable the macros with Excel VBA code?
Upvotes: 9
Views: 68571
Reputation: 95
There is a way to automate the macro enabling, but you need to have full control on client machine at least once. Use digital certificates and sign your macros with it, the first time you run macros signed, Excel will offer to you an option to always trust macros signed by this certificate. When you trust it, all macros signed with your certificate will be automatically enabled.
This is the best option to develop and manage macros on your corporate network.
Upvotes: 1
Reputation: 328835
This is a security feature. If you could disable it within Excel in vba it would defeat the purpose. I'm afraid the answer is no, the user will need to adapt his security settings...
That being said, you could change the settings from another application by modifying the registry. On recent versions of Office, this is the key that defines the behaviour: HKEY_CURRENT_USER\Software\Microsoft\Office\XXXX\Excel\Security
where you need to modify (or create) a DWORD named vbawarnings
. The values it can take are as follows (source):
Upvotes: 4
Reputation: 1157
Here is an alternate method to check if VBA is enabled without having to hide/show sheets. The problem with hiding/unhiding sheets is that the worksheet could be saved in an "enabled" state, be closed without saving, and then be re-opened and not display the warning.
Create the following function in a Module (not a worksheet or workbook macro):
Public Function MacrosEnabled()
MacrosEnabled = True
End Function
Now, in whatever cell you want to display a notice regarding whether macros are enabled or not, enter the following equation:
=IF(ISERROR(MacrosEnabled()&NOW()),"Macros are disabled","Macros are enabled")
If you just use ISERROR(MacrosEnabled())
the value is not recalculated every time the spreadsheet is opened. By concatenating NOW()
, the current time is read every time the spreadsheet is opened, and so the value of the cell is recalculated. I tested this method extensively, and it seems to very reliably indicate whether macros are enabled or not.
You can then apply conditional formatting to do a number of things if macros are disabled:
Highlight the check-cell in a bright color, and perhaps change the "macros are disabled" text to include instructions on how to enable macros.
"Hide" cells whose values depend on macros being enabled by setting their text color and background color to be the same.
Here is a spreadsheet that demonstrates this method.
Upvotes: 4
Reputation: 55692
The standard way of doing this is to force the user to enable macros by use of a splash screen.
Two links with full code for this technique are listed below
Upvotes: 14