How to run code in a custom appointment tab? - vba

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

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.

Using VBA to allow a checkbox to hide/show content in Microsoft Word

I've gone through a number of tutorials and instructional videos trying to achieve my intended result of simply allowing a checkbox in my form to hide content when selected, or re-show it when being de-selected, but nothing seems to be working.
Currently, I've created a bookmark for the content I want hidden, and try to call his this in VBA with the following statement - which a number of resources indicated as the solution:
Private Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
But despite doing this, selecting the checkbox has no affect on the content.
Is there something additional I'm missing (I'm using Microsoft Word 2013).
Your code worked fine when I tested it, but I ran into a few issues since I've never messed with userforms/checkboxs in Word VBA and I suspect you may have the same.
For instance, the first thing I did was create Insert --> Module. This is incorrect. What you want to do is Insert --> Userform then drag a checkbox over from the ToolBox
https://smallbusiness.chron.com/use-check-boxes-word-54673.html
Then double click the checkbox to bring up the "module" it would run, notice there is no module in the side pane! Edit the module, then go back to the userform and press F5. You should be presented with a checkbox that will hide/unhide your text.
Here is my module:
Public Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
Here is an image:
Note: I didn't test how to insert the checkbox into the word doc, I'll leave you some of the learning!
Update:
This sub will make the CheckBox appear when run, but I'm not sure the logic you would use to call it, perhaps an event like opening of document?
Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub
This could be called via a keyboard shortcut or event, but this will cause a "pop-up window". If you want an inform checkbox you may need to look into using this Legacy Form/Checkbox. I was following the URL from above but I think it's dated.
Update:
You could also add this easily to a toolbar, but that isn't likely what you want. I found the best way is to use a "field code" see --> https://word.tips.net/T001571_Assigning_a_Macro_to_a_Button_in_Your_Text.html
I was able to get this to work by pressing CTRL + F9 then typing { MacroButton Loadform ClickMe} then clicking this text. This may be the best bet as using an image seems not to be a great idea.. (https://answers.microsoft.com/en-us/msoffice/forum/all/using-graphic-with-macrobutton/a9c1ef3b-cf1f-48ba-92a8-c44bffcdc131) & (http://www.addbalance.com/usersguide/parts/macrobutton_fields.htm#Different_behavior)
Gif Example:

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 show windows form when a custom ribbon button is clicked

I am creating a Word Addin using Visual Studio 2008 with Visual Basic language. I have created a custom ribbon group and a button name "Show_Form". I want that when I click on that button a windows form named "Form" will show.
Sub Button1_Click()
Form.show()
End Sub
It showing a error. Can you please help me.
There is no form called "Form"
You will need to do Form1.Show or Form2.Show etc
However it is good practice to name the forms to what they will be used for otherwise it can become difficult to understand the program code if everything is named so that it doesn't actually identify what it does.

Auto displaying form on opening a template file, dotm from explorer

I have written a form based document generation macro (in VBA) for distribution to a sales team.
For their ease of use, I want to provide a self-contained file which will display the form as soon as the document is opened.
Using AutoOpen I can get the form to display as intended if word is already open and the dotm file is opened within. However, if I double click the file from within explorer, nothing happens and I have to launch the macro manually. I thought AutoExec might allow this but no luck there. I've spent considerable time trying to get this to work through googling etc. but I'm not getting anywhere.
How can I make the form display even when the file is opened with a double click? Is it possible to do this without having to change normal.dotm for each user?
For further background, I am using Word 2013 with macros fully enabled during testing. The dotm file is stored in a trusted location.
I am using a macro to launch the form like this...
Public Sub AutoOpen()
StartPage.Show
End Sub
I have tried using AutoExec as well to no avail.
In the "generator.dotm" file got to Visual Basic and go in to the "ThisDocument" Microsoft Word Object.
At the top of the Visual Basic Editor select "Document" in the left hand side and then click on "New" on the right hand side. Private Sub Document_New() method will appear for you to be able to edit. Then you can call your userform in there. Similar to:
Private Sub Document_New()
Dim myForm As UserForm1
Set myForm = New UserForm1
myForm.Show
End Sub
Save your Generator.dotm and double click it through Windows explorer and you should get the results that you would like.