vba private scripts - vba

I know that even private vba scripts can be called by the user, such that making it "private" in fact only hides its name.
However, is there a way to set up a macro so it is only runnable if you are inside that particular VBA project? Not from Excel and not from any VBScript or the likes.

If you want to lock down the code you could
make the code private so the macro names areen't exposed
lock the protect, and then test that the project is actually unlocked before letting the code run
The sample code below checks that the VBA in the host workbook is not protected before running
Does this meet your needs?
Private Sub TestMe()
Dim objVB As Object
Set objVB = ThisWorkbook.VBProject
If objVB.Protection = 0 Then
Call TestSub
Else
MsgBox "Sorry sport - unauthorised", vbCritical
End If
End Sub
Private Sub TestSub()
MsgBox "Project unprotected - i've been run", vbOK
End Sub

Honestly, there is no foolproof way around it. You can have certain checks but that's all you can do.
If you are sure that a user will not have access to VBA Code then what brettdj suggested is the best way to go ahead. Protect your project. One cannot run a macro from Excel or from outside Excel if one doesn't know the macro name ;)
The basic intention of making a macro Private is not to prevent it from running but making it invisible to the user. Mostly, the only macros that needs to be private in Excel are the inbuilt Worksheet or Workbook events or macros referred by other macros which necessarily don't need to be accessed by the user. A user can still access them from outside VBA if he or she wants to.
Having said that, you can restrict (But not STOP - Not referring to disabling your macro security) the macros from running. Pass an authorization FLAG. Only when it receives an "Authorization", will it run.
For Example
Option Explicit
Private Sub Sample(Auth As Boolean)
If Auth = True Then
'~~> Your macro code goes here
End If
End Sub
Now if you want this macro to be called from VBA then you have to call it like this
Sample True
or
Call Sample(True)
This will ensure that the above macro will only run when you allow it to.
Would this method prevent user from running this from VBS?
NO. It won't. However, it won't run till the time the user specifies or gives the "Authorization"
HTH

Related

Excel VBA protect returning error

I've seen many posts on here and elsewhere about using the following code in the ThisWorkbook Object in order to protect your workbook but still allow macros to run:
Private Sub Workbook_Open()
ActiveWorkbook.Protect UserInterfaceOnly:=True
End Sub
However, the protect function tooltip that pops up only offers three arguments Protect([password], [structure], [windows]). Therefore, when I open the document I get the following error
"Compile Error: Named argument not found"
in regard to UserInterfaceOnly:=True.
Am I using the wrong protect function, or is there something else I'm missing? I want to allow users to input data into the workbook using forms/macros, but I don't want them to be able to manually change any cells.
It almost seems as if the protect function that I'm running has different arguments than many of the other protect solutions I've found. Could this be a version issue or something else?
The suggestion provided by Tim Williams worked. Protect doesn't work the same way at the workbook level, therefore I needed to write individual code for each sheet I wanted to protect. I used the following code to protect the sheets but allow forms to populate:
Private Sub Workbook_Open()
Worksheets("Sheet1").Protect UserInterfaceOnly:=True
Worksheets("Sheet2").Protect UserInterfaceOnly:=True
Worksheets("Sheet3").Protect UserInterfaceOnly:=True
End Sub

Is it possible to run a VBA macro in a READ-ONLY protected PowerPoint?

My colleague and I have been on a six month quest for a VBA macro that can run in a Read-Only, password protected PowerPoint.
Is it possible or will PowerPoint always block the VBA macro from running in the presentation, because of the read-only status?
Private Sub CheckBox1_Click()
Dim ppShp As Shape
Dim eff1 As Effect
Dim ani1 As AnimationBehavior
With ActivePresentation.Slides(1)
Set ppShp = .Shapes("Oval 4")
Set eff1 = .TimeLine.MainSequence.AddEffect(Shape:=ppShp, effectId:=msoAnimEffectAppear)
End With
End Sub
I tried using If ReadOnly = True Then conditions.
I want the user to use the macros and save, but not edit beyond that, or to open and 'look under the hood'.
(It's all for an educational program)
We get
Run-time error '-2147467259 (80004005)'
Presentation (unknown member) : Invalid request. Presentation cannot be modified.
Perhaps as a work around you could save a copy of the original read only presentation, run your macro on the new version and then save the new version as read only?
Hello this might be too late but if I understand you correctly, you want that other users can read, execute macros but not able to read the code behind it. Saving should be okay.
The macro that you used should be password protected. First go to the editor, select the macro and view the properties/protection and set up a password
Then after you are done, close the file. Right Click on the file/properties and check readonly. Users will then have access to the macro, not being able to see what code is behind it and can save it.

Is there a way to hide macros in Excel?

I just finished some VBA and I was wondering if there is a way to hide certain macros on Excel.
I need the user to run a certain macro and only that one, but it shows all the sub macros in Excel. I want to hide the unnecessary macros from the user so that way the user doesn't accidentally click on the wrong one.
You can also do this by placing the macros you want to hide in a separate module and using Option Private Module at the top of the module before the code. The macros will still be available to your project but will not appear in the Macros seen by the user when he clicks the Macros button.
You can either create a button in the ribbon to run the macro, or you can add "Private" before each "Sub" in the VBA editor that you don't want the user to easily access.
To subjectively 'hide' certain sub procedures (i.e. 'macros') from the (Alt+F8) Developer, Macros dialog use an optional non-variant parameter that means nothing.
Sub meh(Optional w As Worksheet)
Debug.Print "hello world"
End Sub
The meh macro will not show up in the list of macros to run. If you dim the parameter as variant it will show in the list. This is likely due to a optional variant parameter being able to use the IsMissing function. It will also not be able to be run from the VBE with F5 or stepped through with F8.
The test sub procedure will run the code correctly.
Sub test()
meh
End Sub
Sub meh(Optional w As Worksheet)
Debug.Print "hello world"
End Sub

Running Macros from Toolbar/Running one macro from another

I am trying to develop a macro for a publisher document. This macro will, when run, show a pop-up allowing the user to select one of three types of clients, and add different bullet points to a text box depending on which option was selected. I'm having two different problems which I suspect are coming from the same source. Problem number one is that I can't get the button on my User Form to run a different macro when the button is clicked. Problem two is that I've added my macros to one of the toolbars, and nothing happens when I click on them. In both cases, it's simply not running the macro. What am I doing wrong?
UserForm1
Private Sub CommandButton1_Click()
Application.Run ("ShapeTest")
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.AddItem ("Federal")
.AddItem ("State")
.AddItem ("Local")
End With
End Sub
ThisDocument
Private Sub GenerateStatement()
UserForm1.Show
End Sub
Private Sub ShapeTest()
MsgBox ("Hello!")
Application.ActiveDocument.Pages(1).Shapes(1).TextFrame.TextRange.InsertAfter`enter code here`(Chr(13) & "My Text")
End Sub
Why are you using Application.Run("ShapeTest") rather than simply ShapeTest?
I don't have enough information to be 100% sure, but the following should work: To make ShapeTest callable from the userform you do two things:
1) Move it from ThisDocument to a general code module (first Insert/Module in the editor).
2) Eliminate the word Private in front of Sub ShapeTest()-- you don't want this to be a private sub since you want code outside of the module to be able to use it.
On edit: Alternatively -- you could keep ShapeTest() where it is in ThisDocument, get rid of the Private qualifier and in the userform code refer to ShapeTest as ThisDocument.ShapeTest. I prefer using the first method since I tend to like to keep as much code as possible in general code modules (reserving things like ThisDocument for event handlers) but OTOH my VBA experience is mostly Excel with a smattering of Word and there might be reasons to keep the code in ThisDocument in Publisher. I don't know Publisher, but a problem that I have run into in Word at times is I have sometimes accidentally put code in the Normal template that I wanted to go in the document's project. If something similar is possible in Publisher you should double check where your code is living.

Application.Quit in UserForm attempts to run rest of macro before exiting

My question is: using VBA in Excel 2013 how can I gracefully close an entire instance of Excel when the user decides they don't want to fill out a UserForm and clicks quit or cancel?
Currently, if the user clicks quit or cancel, I check to see if my instance is the only one open. If it is not, I can use ThisWorkbook.Close and I think I will be okay. However, if it is, I do not want the application to still be present, so I used Application.Quit. This, though, tries to finish running the macro, throws errors (originally "type mismatch" because I unload the form), and only closes after I click "Debug" or "End" (which it does so fast for either I cannot actually debug). I'm ignoring the first case for now and just trying to exit the entire application. It's a very long macro with a lot of subroutines and functions, so for debugging and posting here, I have shortened it. The type mismatch error no longer occurs, but I believe that was a consequence of the actual error: code running after the command to close the application is called.
First, here's the code that starts everything:
Private Sub CommandButton1_Click()
Call form_variables
frm_REQUEST.Show
Call a_REQUEST_main
End Sub
The subroutine
form_variables
is a subroutine that creates public variables so I can store the data from the UserForm.
frm_REQUEST.Show
initializes (including calling a function that finds another workbook, extracts a list, does some formatting, closes the workbook and enters the list into the userforms drop down box) and shows the form, and finally
a_REQUEST_main
uses the public variables (where UserForm data is stored) and does its thing (but shouldn't do anything if the UserForm is closed).
The code that is executed when .Show is called is:
Private Sub UserForm_Initialize()
' Get job numbers from other workbook
Dim job_selection_list As Variant
job_selection_list = get_job_list()
With frm_REQUEST.Job_Number_ComboBox
.List = job_selection_list
End With
' set focus on Job Numbers
JN_combobox.SetFocus
End Sub
Private Sub cancel_button_Click()
Set job_selection_list = Nothing
Unload Me
Application.Quit
End Sub
Private Sub submit_button_Click()
' Values from userform saved as global (?) variables so other subroutines can access.
End Sub
I stepped through the program and saw that, once Application.Quit is called in the UserForm, the macro, in the main subroutine, steps to
Call a_REQUEST_main
but it should really just close everything out. I tried doing "save" commands, and changing the order of things, and read about objects needing to be set to nothing (hence the setting of the job_selection_list which is created when the drop down list is initialized), but I cannot seem to get this to work, or find anything online. Can anyone provide some guidance or let me know of a better way to close an excel instance? Help me Stack-Overflow Kenobi, you're my only hope!
Thanks.
Just add a variable to account for when the user closes the form
in the form
'hold flag if users cancels form
Public btnCancel As Boolean
Private Sub CommandButton1_Click()
Unload Me
btnCancel = True
End Sub
'set the flag each time the form active
Private Sub UserForm_Activate()
btnCancel = False
End Sub
then in your code
Call form_variables
frm_REQUEST.Show
If frm_REQUEST.btnCancel Then
Application.Quit
Else
Call a_REQUEST_main
End If
Put Application.Quit in the form's Terminate event handler instead of in the button's Click event handler.
The reason is that clearly the procedure will keep running even if the form has unloaded. So use the events to your advantage.
Putting it in the Click event will unload the form, but the procedure will keep running which of course may raise errors or other undesired effects.
Note: You may be prompted to save/discard changes (if any) to the workbook.