Attaching VBA Form to Outlook Application - vba

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.

Related

How to manage application options/advanced options?

I need to quickly toggle File > Options > Advanced > Reminders > Play reminder sound setting.
In certain meetings I need to keep Outlook running to get reminders, but don't want the reminder sound.
Currently, I manually toggle the File > Options > Advanced > Reminders > Play reminder sound checkmark.
This needs to be an effective single-click.
I don't want to restart Outlook as there are usually many things open.
I cannot add it to the Quick Access Toolbar.
On the left the option is present in the list of QAT commands, but not in the list of Ribbon commands, otherwise we could access it with ExecuteMso.
Three reasons:
The button is disabled when not in the Calendar window. That's just inelegant.
When I click the button, Outlook crashes and I lose everything that wasn't saved. (Autosave isn't sufficient or functioning. That's a different problem.)
Understanding how to access Outlook settings with VBA opens a whole new world of possibilities.
This Microsoft article series starting at https://learn.microsoft.com/en-us/office/vba/outlook/concepts/getting-started/automating-outlook-from-a-visual-basic-application is about automating Outlook user tasks, like making a calendar appointment. That's not what I want, I want to manage Outlook's options.
There are some discussions about COM add-ins as one method, but that appears beyond me. The effort to learn COM add-ins is out of line with manual effort to achieve the desired goal.
I have some limited Outlook VBA experience but am reasonably comfortable with VBA in Word, Excel.
To disable reminder sound you need to set the below registry key to 0.
Registry key: PlaySound (REG_DWORD) to be set to 0.
Path: HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\options\Reminder
where 16.0 indicates the Outlook version.
Don't forget to restart Outlook to apply changes.
These settings can be overridden on the per appointment basis - you can simply set AppointmentItem.ReminderPlaySound property to false using VBA.
Simulate a button press with ExecuteMso.
https://learn.microsoft.com/en-us/office/vba/api/Office.CommandBars.ExecuteMso
Hover over the button where you would add it to the ribbon/QAT. See text in brackets at the end.
Are the command codes for ExecuteMso documented?
Private Sub ExecuteMso_TextInBrackets()
' https://learn.microsoft.com/en-us/office/vba/api/Office.CommandBars.ExecuteMso
' https://stackoverflow.com/questions/25610998/are-the-command-codes-for-executemso-documented
Dim oAppt As Object
Set oAppt = ActiveInspector.CurrentItem
Debug.Print oAppt.subject
ActiveInspector.CommandBars.ExecuteMso ("TextInBrackets")
End Sub
As with most things in life, the answer is a workaround. In this case, it's AutoHotkey to press the keys for me (ALT F, T, down x 9, ALT P, Enter):
;WIN-O toggles the Outlook alarm sound setting
#o::
SetTitleMatchMode,2 ;inexact match
WinGetActiveTitle, MyWindowTitle
If WinActive(" - email#company.com") ;Poor way to "prove" we're in Outlook
{
;MsgBox, We're in Outlook
Send !ft{Down 9}
Send !p{Enter}
}
Return

How to run code in a custom appointment tab?

I'm trying to write an extension to my Outlook calendar - More specifically the appointment module. (Form designer)
I have a button in my designer, that I want to write some simple code around.
Sub CommandButton1_Click()
msgbox "Hello World"
End Sub
The above code is put into the code editor that appears when I press "View code". Nothing happens.
How do I get a commandbutton to trigger a simple alert in the Outlook form designer?
Custom form script is now disabled by default. You can see more details for the known issue and you can re-enable it as the following link suggests.
You can read more about that in the Custom Form Security Changes article

Launch Userform when Outlook Template is opened

I have a User Access Request form that I created in Word, and have now been asked to replicate in an outlook email template. This .oft file will be placed on our website, so that when users click on the link, they will download the .oft file. When the user opens the file, an email with the User Access Request form in the body will load.
I was able to set up the bookmarks, and import the Userform from Word, and have even gotten everything to populate correctly in the email from the Userform...however I have hit a wall.
I can only get the Userform to appear when I run it from Visual Basic in the Developer tab.
What I am looking to do is have the Userform pop up as soon as the template is opened, and only when that specific template is opened by a user (I don't want the Userform to open if they are trying to create a new email, or when they open outlook, etc). I have tried adding a module with the Userform.show code, I have tried adding the code below into the UserForm's code as well, with no luck.
Private Sub UserForm_Activate()
UserForm1.Show
End Sub
Do you have any suggestions as to how I can get this to work properly?
Thanks in advance!
You will need your custom logic to be present on every user's PC in order for this to work. Since you have created a UserForm in your Outlook VBA project, you will need to copy that form and supporting macros to every user's Outlook VBA project.
Often it is better to develop a COM Add-in to host your logic, and deploy it using a Windows Installer package. Deploying the .otm file where the VBA is stored is not supported.
Looks like I was able to get it to work. I added the following code:
Private Sub Application_ItemLoad(ByVal Item As Object)
R1AccessRequest.Show vbModeless
End Sub
Worked beautifully... until I sent the template over to a colleague to test... Userform appears to be tied to my outlook, and didn't appear for him. We opened up VB on his laptop, and saw that the form was not present :( From what I have been able to gather online, it doesn't seem possible.

VB macro to disable "Edit Document" prompt when opening a word document from sharepoint

Is there a way to disable the message (and have the document editable by default):
Server Document To modify document, click Edit Document followed by the button with text "Edit Document".
I cannot find a word setting to do this. In addition I cannot see a way to make a VB macro to do this with a key stroke. I have used a small autohotkey script to position the mouse and click this prompt, but this does not always work since it depends on the position of the window. it is impossible to use the tab key to get to this prompt.
I have to modify about 50+ documents a day from sharepoint, ideally I would like to combine this with another macro which does other automated processing for me. But I can't find a VB solution for clicking the Edit button.
Depending on your security settings (you mentioned that they were blocked), this may or may not work.
Create a new macro enabled template in your Word startup folder (usually at C:\Users[YourID]\AppData\Roaming\Microsoft\Word\STARTUP), and add a new class module. I called mine "AutoEditEnable". You can name it anything, but you'll need it to match how you declare it in the other module.
This code goes in the class:
Option Explicit
Private WithEvents app As Application
Private Sub Class_Initialize()
Set app = Application
End Sub
Private Sub app_ProtectedViewWindowOpen(ByVal PvWindow As ProtectedViewWindow)
PvWindow.Edit
End Sub
Basically, this will hook any Application events you need to - in this case the ProtectedViewWindowOpen event or the ProtectedViewWindowActivate event (either should work).
Put the following code in ThisDocument to grab a reference to it when your template loads:
Option Explicit
Private hook As AutoEditEnable
Private Sub Document_Open()
Set hook = New AutoEditEnable
End Sub
Close Word and restart it, then make sure your new template shows up as a loaded add-in.

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!