VBA: How would my code check whether I have unlocked it? - vba

Is there a way for my code to check whether the code in the VBA editor was unlocked for editing? I want to create a global "admin mode" variable which would give me more control in a dev vs. a live environment.

This will do the trick, and should work as a UDF (though it will be volatile). However, this requires that macros have programmatic access to VBProject, which is NOT enabled by default and must be enabled (PER USER) in the Trust Center options in Excel. Chick and egg, there's no way to programmatically set that trust center option.
Public Function IsDevMode() As Boolean
IsDevMode = (ThisWorkbook.VBProject.Protection=0)
End Function
However, there's no event to trap to know when this value changes, so it will be difficult to use it to make the changes to your workbook to move in and out of "developer mode." A better option might be to add a button somewhere that toggles "developer mode" (hiding and showing things, unlocking/locking sheets, etc.), and in that toggle button's click method, use the code above to see if the code has been unlocked in this session. If so, proceed, and if not, ignore. That way, by unlocking your code, you basically grant yourself access to use said button.

Related

Attaching VBA Form to Outlook Application

I'm creating a custom form for appointments in Outlook for a project (add catering request to a meeting) - this is my first rodeo at doing this - and I'm striking out big time regardless of my intense (and failing) Google-Fu.
With a new appointment open, on the developer tab, I select "Design This Form". I go to tab "(P.2)" and build a stupid-simple, two-object form... CheckBox1 and TextBox1. In properties, TextBox1.Visible is False. Click View Code and input the following...
Private Sub CheckBox1_Click()
if CheckBox1.value=True Then
Me.TextBox1.Visible = True
Else Me.TextBox1.Visible = False
End If
End sub
I then click "Run This Form" and see/click CheckBox1 but nothing happens. If I could make this run, I might be in business. But it won't. So, I look for a work-around.
Grasping at straws, I open "Visual Basic" and basically do the same thing - create a form "UserForm1" with the same two objects and add the same code. Click the go-button and it works as expected. The TextBox1 appears and disappears with the CheckBox1 state.
As I can make the code function, I would build everything I need in the Visual Basic editor, however... Here's the problem - I have absolutely no idea how to get the form I create here into the Outlook application as a tab or button or whatever. I basically want the custom form VBA editor to be a selectable option in any Appointment.
I have watched tutorials, read doc, saw something about creating a macro - but nothing was written/stated dumbed-down enough for me to follow.
So my question: How do I get the UserForm1 that's built in VBA Editor to appear in a New Appointment when a button is clicked in Outlook?
You need to add the Click event handler for the CheckBox control, not just paste the code to your custom form.
Following the June 13 2017 security update, users discovered published custom forms no longer worked because VBScript behind the form and some controls are blocked by default. See Custom Form Security Changes and Custom form script is now disabled by default for more information.
Microsoft disabled custom form script functionality. If you need it enabled, you'll need to set two keys, one to enable scripting and a second one with the message class name of each form that has code behind it. For example:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\16.0\Outlook\Security
DWORD: DisableCustomFormItemScript
Value: 0 (to enable)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\16.0\Outlook\Forms\TrustedFormScriptList
REG_SZ: IPM.Contact.custom-form-name
Value: (leave blank)
In some cases, forms in secondary mailboxes and Public folders still don't work after the registry key settings. To fix this, enable scripts in the Trust Center:
Click File > Options. Then select Trust Center > Trust Center Settings > Email Security.
Under the Script in Folders section, click the checkbox for Allow script in shared folders and Allow script in Public folders and click OK and OK again to close out the windows.

Making a MS Word document "absolutely" read only

Is it possible in vba to make a document read only and in such a way as to prevent any user from making any changes to it?
The protect method for the document object allows you to make a document read only, but as soon as the user tries to edit the document it provides access to the Restrict Editing Pane which gives the user access a "Stop Protection" button.
I would like to prevent the Restrict Editing Pane from appearing.
Whilst it doesn't seem possible to stop the "Restrict Editing" panel from opening. It is possible to prevent the user from enable editing by adding a mouse hook and catching any mouse clicks within the Restrict Editing panel. Code in Delphi is
Hdl := WindowFromPoint (Mouse.CursorPos);
GetClassName (Hdl, clsName, 256);
GetWindowText (Hdl, wText, 256);
if (uppercase(clsName) = 'BOSA_SDM_MSWORD')
and (uppercase(wText)= 'PROTECT DOCUMENT') then {do whatever}
This catches any clicking within Restrict Editing panel and is not pinpointedto the "Stop Protection" button. I would assume it would be possible to drill down further to identify the button specifically, but it seems to me unnecessary to do so. Also, if the class name or window text changes then the function will fail.
Aside from being clunky and potentially not future proof, it does however work.

Password protected VBA but code still visible

I have an excel macro file(.xlsm), when I open the VBA editor, on the left panel I right click on VBA project XYZ and click on VBAProject properties, it prompts for a password. This indicates that VBA is password protected. However, I am still able to see the macro codes in modules/sheets.
Is that possible and if yes then what's the point here in protection?
Though I am able to see the code, when I try to add a reference, I again get that password prompt. Why?
That is because you or the author haven't clicked on Locked project from viewing while protecting the VBA Project.
Also you need to close and re-open the workbook for the Lock Effect to take place after Locked project from viewing is checked
Your Questions
Is that possible and if yes then what's the point here in protection?
Yes it is possible. The intention here could be to ensure that you don't make any changes to say references as you have already experienced. OR someone simply forgot to click on Locked project from viewing like shown above.
Though I am able to see the code, when I try to add a reference, I again get that password prompt. Why?
As mentioned above, since the VBA Project is locked, you cannot make changes like adding a reference.

Unable to Debug Code

I have a very weird issue with Access. I have a form for which I created a command button with a simple sub for the Click event:
MsgBox "Hello"
When I put a break point in, it doesn't stop at the break statement, it just executes the sub. I don't program in access everyday but I have been coding on/off with Access for over 10 years. I have never had this issue, and I have worked on this clients Access Database before.
Any advice would be appreciated.
When you check Access Options->Current Database->Use Access Special Keys option, break points will no longer be triggered. Having this option unchecked also prevents things such as F11 opening the access objects window.

How to force user to deal with the Security Warning when starting Access 2007?

When a user start an Access 2007 database that have macros and vba, a security warning is shown. I want the user to deal with this warning, so if the the content is't enabled, the user should not be able to use the database.
Now I use a macro named AutoExec (opens a form that works like a menu), and that macro is run before the user deal with the security warning. But I want to check if the content is enabled and if not I will show a form that inform the user that they should enable the content.
So what I'm actually asking for is how do I do this:
If vba and macros is not enabled -> show form "information"
If vba and macros is enabled -> show form "start menu"
Ok, after a while I have the solution. Thanks for the answers who led me the right way.
This article from Microsoft is very helpful.
In the AutoExec-macro, I have two lines:
Line one: Conditions: [CurrentProject].[IsTrusted]=False and then I choose witch Form I want to open and in this case it is the "info about security warning form"
Line two: Conditions: [CurrentProject].[IsTrusted]=True and now open the "start menu form"
And that's all!
If the content is disabled, then you cannot check, since your code cannot run....
You might like to consider a start-up form ("information"). This will show without macros.
In addition, you can run some start-up code or a macro that closes the information form and opens the main form ("start menu"), if macros are disallowed, this will not run. However, I think you may get an unsightly warning.
EDIT
Set the timer interval to say, 100 and add a little code to Information form:
Private Sub Form_Timer()
DoCmd.Close acForm, "Information"
DoCmd.OpenForm "start menu"
End Sub
Just to add my solution -- I was just dealing with this issue.
By default, in database options have it set to open with form "notEnabled"
On this "not enabled" form, have some text, pictures, or what have you that lets the user know that he/she needs to 'enable content'.
In the on load event for this form, just put some VBA to open the actual form you want the user to be presented and close the "notEnabled" form.
This way, if the user opens the database without making it trusted, enabling content, they are stuck on the form that tells them how to do that. As SOON as it's trusted, the on-load event of the form will fire and redirect the user to whichever form you want, with content enabled.
If the user opens the database and already has trusted the file, they don't see the form telling them to make it trusted.
You can avoid this by setting the IsTrusted flag to TRUE in your AutoExec macro. See Transitioning Your Existing Access Applications to Access 2007 -- search for IsTrusted to get you to the heart of the explanation of how to handle it.
I don't know why people give suggestions that have not been tested yet. My solution is simple:
If: [CurrentProject].[IsTrusted]=False
RunMenuCommand: CloseDatabase
Else
If: [CurrentProject].[IsTrusted]=True
RunCode: (you run the code or macro you wanted to in the first place here)
This basically closes the database if the security warnings are coming on. If they are not, it opens just fine. The user that is the admin will need to decrease the macro security levels on the computer of whoever wants to access the database. This macro unlike others will actually run because it agrees with what Access wants.
You're Welcome!