Read Excel VBA Macro Security Settings - excel

I have created a macro-enabled Excel workbook that requires users to have "Trust access to the VBA project object model" enabled in the Excel Macro Security settings.
I forgot I had already done this, and my coworker got stuck on (not responding) when trying to run the macro.
This window can also be brought up with this code:
Application.CommandBars.ExecuteMso("MacroSecurity")
Can I programmatically determine the value of this checkbox with VBA? I'd like to be able to check whether a user has this feature enabled before allowing them to run the macro that could potentially hang up their machine.

Thanks, y'all
Private Function VBATrusted() As Boolean
On Error Resume Next
VBATrusted = (Application.VBE.CommandBars.Count) > 0
End Function

Related

How to make application level event handlers in VBA?

I have a VBA add-in that sits in a shared file drive that three users in my organization have added to their Excel. I thought the add-in would be handy because I could push changes to the code without updating the code in any individual Excel sheets. When I tried updating the code in the add-in, the add-in was read-only because other users had Excel open. I've been trying to figure out how to make the add-in read only whenever anyone else (meaning anyone other than me) opens an instance of Excel. I've added the below code to a class module in my add-in. I've tried debugging, but no part of the below code is executed when a user opens a new workbook. I'm totally stuck.
Private WithEvents App As Excel.Application
Private Sub Workbook_Open()
Set App = Application
End Sub
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
If App.UserName <> "Andrew Lubrino" Then
SetAttr "C:\FilePath\Add-in.xlam", vbReadOnly
End If
End Sub
I've found a number of posts on this topic all with accepted code for seemingly the same problem, but the code simply doesn't work in my environment. Can anyone tell me what's going wrong?
edit:
I'm trying to make the add-in read only for all other users so that when I try to edit and make changes to the distributed code, I can save those changes. With the current set up, I can't save the code whenever any other user has Excel open. For the above code to work, you'd need the event to trigger for any workbook that another user opens

What are the differences between the microsoft excel security notice popup and the message bar in excel 2013?

I am encountering an issue developping a VBA functionality in an excel Workbook.
This workbook is geared toward being used by end-users without administration rights, who need to activate the macros on this specific workbook before being able to use it.
To this end, I have done the following:
I created a "temporary home worksheet" which asks the user to activate the macros (no code, a simple message in the cells of this worksheet), which is initially visible
I created a "home worksheet" which is initially (very) hidden, and which holds buttons and other controls that can be used
I wrote a simple sub which is called:
when the workbook is opened and the activesheet is the "temporary home worksheet"
or when the "temporary home worksheet" is activated
This sub does the following (see code below):
hide the "temporary home worksheet" (to very hidden)
show the "home worksheet"
This enables to:
show a message asking the user to activate the macros if he/she has not done it
redirect to the target "home worksheet" if the user activate the macros while the "temporary home worksheet" is activated
redirect to the target "home worksheet" if the macros are activated and the user activates the "temporary home worksheet"
' Code to hide temporary ws and show target ws
Private Sub setTargetVisibility()
Dim ws As Excel.Worksheet
ThisWorkbook.Activate
Set ws = ThisWorkbook.Sheets(getParm("tempHomeWSName"))
With ws
.Visible = xlSheetVeryHidden
End With
Set ws = ThisWorkbook.Sheets(getParm("homeWSName"))
With ws
.Visible = xlSheetVisible
.Activate
End With
End Sub
This works fine most of the time, because the users get prompted to activate the macro through the message bar:
However, they sometimes get another prompt to activate the macros: a microsoft excel security notice popup. Whenever they activate the macro through this popup, they get a runtime error 1004 (e.g. the method Activate of the _workbook object failed), in the sub with the code above.
Edit: an important thing to note is that this excel file is opened in Protected view as it is generated by a web application and downloaded by the user before use.
What cause these different way of enabling macro in excel 2013 (message bar vs. popup)? What are the behaviour differences between them?
Thanks in advance.
Regards,
By default, when you first open a macro-enabled workbook you’ll see a yellow “SECURITY WARNING” bar appear just underneath the ribbon. Clicking the “Enable Content” button will enable macros.
This will trigger any macros that run when the workbook is opened, so don’t click this by mistake!
If the Visual Basic Editor is open at the time you are opening the file with macros, the Microsoft Excel Security Notice will be displayed.
If you trust the source of the file and know that all the macros are secure, click the Enable Content or Enable Macros button. This will turn on the macros and make the file a trusted document. The next time you open the workbook, the security warning won't appear.
If the source of the file is unknown and you don't want to enable macros, you can click the 'X' button to close the security warning. The warning will disappear, but macros will remain disabled. Any attempt to run a macro will result in the following message
As mentionned by #David Donayo in the accepted answer, what triggers the Microsoft Excel Security Notice popup vs. the message bar warning is the fact that the VBA Editor is already open for another xlsm file.
What caused my issue (runtime error 1004 when trying to make Object Model calls such as Application.Calculate or ThisWorkbook.Activate at WorkbookOpen event) is that the issue described here (https://support.microsoft.com/en-us/help/2745652/object-model-calls-may-fail-from-workbookopen-event-when-exiting-prote) is still present in excel 2013 when the Protected View is exited and then the macros are activated through the Microsoft Excel Security Notice.
This issue is not raised when the Protected View is exited and the macros are activated via the message bar warning.
I did not implement the workaround described in the link above (defer Object Model calls from the WorkbookOpen event to the WorkbookActivate event if the workbook is opened in Protected view) as in my context end users should not have the VBA editor open. Therefore I cannot tell whether this workaround works, but see no reason why it should not.

Enabling macro by default on a workbook

I am new to VBA/Excel programming, I am refreshing a pivot table using a macro. I have the following VBA code on one of my sheets.
Excel version : 2013 ( saving my file as macro enabled workbook)
Private Sub Worksheet_Activate()
RefreshPivotTables
End Sub
Private Sub Worksheet_Deactivate()
RefreshPivotTables
End Sub
The code works fine when I switch back and forth after enabling the macro, but when I open the Excel doc I get this "Enable macro ribbon" at the top which is preventing the macro from running. I need to switch to another tab and come back to my original sheet to activate the macro.
Is there a way to set "Enable Macro" for the workbook by default / some code that will do this on behalf of the running user? I cannot expect all my users to switch workbooks and come back to see the refresh happen.
Go into the options in Excel and then to the trust center settings. Put the location in as a trusted location.
Alternatively on my machine if I open a sheet, enable macros then close it again a few times Excel asks if I want to make this a trusted document.

Installing a VBA macro in Excel 2007

I've got a VBA macro in an Excel 2003 spreadsheet and I'd like to 'install' it on a machine running Excel.
I created a 'Trusted Location' on the local machine, and I know how to copy the module to the existing workbook and assign a key combination to invoke it, but I don't know how to make the macro appear automatically when someone starts Excel. Help?
The simplest solution is ( both for xl2003 and xl2007 ) to copy your xls containing the macro to the path = Application.StartupPath from your client machine ( can build a simple vbscript installer which instantiates excel and retrieves this information ).
This way your macro will be available in any workbook opened since the xls files located in startuppath are loaded at excel startup.
I remember the old way was to save your macro in your Personal.xls
workbook - then it would be accessible
any time you opened Excel - has this changed for 2007?
That was a way. A better way was (and is) to create an Add-in, which you can then enable & disable via Tools->Add-ins. (An Add-in will remain enabled even if you close and re-start Excel). You can save any .XLS file as an Add-in (.XLA).
Within your Add-in you could just use an Auto_Open method:
Private Sub Auto_Open()
DoStuff
End Sub
...or, you could hook up the Workbook_Open event, as Ryan suggests.
Of course, since the user can disable the Add-in, you also want to do the reverse in Auto_Close (or in the Workbook_BeforeClose event).
Actually, if you use events it'd be better to use the Workbook_AddinInstall and Workbook_AddinUninstall events. Using those is slightly more "correct", and also has the benefit that the 'close' event doesn't fire if you close Excel and then hit Cancel when prompted to save.
I'm not sure what you mean by 'appear', but you can execute it automatically by calling it from
Private Sub Workbook_Open()
End Sub
in the ThisWorkbook object module. Is that what you're looking for?
I remember the old way was to save your macro in your Personal.xls workbook - then it would be accessible any time you opened Excel - has this changed for 2007?

Insert current date in Excel template at creation

I'm building an excel template (*.xlt) for a user here, and one of the things I want to do is have it insert the current date when a new document is created (ie, when they double-click the file in windows explorer). How do I do this?
Update: I should have added that I would prefer not to use any vba (macro). If that's the only option, then so be it, but I'd really like to avoid forcing my user to remember to click some 'allow macro content' button.
You could use the worksheet function =TODAY(), but obviously this would be updated to the current date whenever the workbook is recalculated.
The only other method I can think of is, as 1729 said, to code the Workbook_Open event:
Private Sub Workbook_Open()
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Date
End Sub
You can reduce the problem of needing the user to accept macros each time by digitaly signing the template (in VBA IDE Tools | Digital Signature...) and select a digital certificate, however, you will need to get a certificate from a commercial certification authority (see http://msdn.microsoft.com/en-us/library/ms995347.aspx). The user will need to select to always trust this certificate the first time they run the template, but thereafter, they will not be prompted again.
You can edit the default template for excel -
There is a file called Book.xlt in the XLSTART directory, normally located at C:\Program Files\Microsoft Office\Office\XLStart\
You should be able to add a macro called Workbook_Open
Private Sub Workbook_Open()
If ActiveWorkBook.Sheets(1).Range("A1") = "" Then
ActiveWorkBook.Sheets(1).Range("A1") = Now
End If
End Sub
My VBA is a little rusty, but you might find something like this works.
To avoid VBA, and if you think your users might follow instructions, you could ask them to copy the date and then paste special->values to set the date so that it won't change in future.

Resources