We help IT Professionals succeed at work.

VBScript to add an .oft Form file into a new Outlook Toolbar

Last Modified: 2018-05-08
Hi everyone.

     I'm trying to develop a code in VBScript (.vbs file) that adds a button on Outlook (2010) and runs a custom form (already developed, .oft file) that is on teh same directory as the .vbs file.

     I have to install this button and custom form to more than one computer, so the ideia is that the VBScript install the .oft file permanently, so the user could access that form by pressing the new button without have to execute the .vbs file or install the form again.

    I'm halfway there, I was looking to this other topic, but I couldn't install the .oft Form with that. Could someone please point me the right direction to finish this VBScript?

Topic: https://www.experts-exchange.com/questions/25051822/VBScript-needed-for-Outlook-manipulation.html#a26403266

What I've got so far:

Const msoControlButton = 1
Const msoCommandBarButtonHyperlinkOpen = 1

'Change the toolbar name on the next line as desired.'
Const CMDBAR_TITLE = "Service Desk Bar"

Dim objItem     ' As Outlook.ContactItem
Dim objFD       ' As Outlook.FormDescription
Dim fso         ' As FileSystemObject    
Dim olkApp, olkSes, ofcBar, ofcButton
Set olkApp = CreateObject("Outlook.Application") ' As Outlook.Application
Set olkSes = olkApp.GetNamespace("MAPI")

'Change the profile name on the next line as needed.'
olkSes.Logon "Outlook"

'Add custom Form
Set fso = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = fso.BuildPath(fso.GetAbsolutePathName("."), "Teste1.oft")
Set objItem = olkApp.CreateItemFromTemplate(CurrentDirectory)
Set objFD = objItem.FormDescription

With objFD
    .DisplayName = "TemplateServiceDesk"
    .PublishForm olPersonalRegistry
End With
objItem.Close olDiscard

On Error Resume Next
'Clear any bars
Set ofcBar = olkApp.ActiveExplorer.CommandBars.Item(CMDBAR_TITLE)
If TypeName(ofcBar) <> "Nothing" Then
End If

'New bar
Set ofcBar = olkApp.ActiveExplorer.CommandBars.Add(CMDBAR_TITLE)
With ofcBar
    .Visible = True
End With

'Set Button
Set ofcButton = ofcBar.Controls.Add(msoControlButton)
With ofcButton
     'Change the caption on the next line.'
     .Caption = "ABRIR Chamado Service Desk"
     .HyperlinkType = msoCommandBarButtonHyperlinkOpen
     'Change the path to the template file on the next line.'
     .TooltipText = "" 'I DON'T KNOW WHAT TO PUT HERE
     .Visible = True
End With


Set olkSes = Nothing
Set olkApp = Nothing
Set ofcButton = Nothing
Set ofcPopup = Nothing
Set ofcBar = Nothing
Set objFD = Nothing
Set objItem = Nothing
Set fso = Nothing

Open in new window

Now the other exemple (Link above)  gave me the ability to Hyperlink a path to my desktop, for instance. But what I REALLY need is to that button opens the form that is already inside Outlook, on the personal registry!

Thank you all!
Watch Question

Chris RaisinRetired Senior Systems Analyst/Programmer

Working on it.

What is ofcPopup?

You offload it at the end of your code but have not defined or used it anywhere earlier in your code.



Dear Chris Raisin

     Thanks for the answer! The "ofcPopup" was a list of menu items (at first I was trying with two buttons, but then I've changed to only one button without the list, and I've forgotten to delete it). Just forget about him!

     I could make it work with addition of a Macro to select the specific Form. On the VBScript button part, I've changed to:

'Set Button
Set ofcButton = ofcBar.Controls.Add(msoControlButton)
With ofcButton
     .Caption = "ABRIR Chamado Service Desk"
     .OnAction = "RunMyForm"
     .Visible = True
End With

Open in new window

      And then I've add the following Macro module:

Sub RunMyForm()
  Dim myOlApp As Application
  Dim myNameSpace As NameSpace
  Dim myFolder As MAPIFolder
  Dim myItems As Items
  Dim myItem As Object

  Set myOlApp = CreateObject("Outlook.Application")
  Set myNameSpace = myOlApp.GetNamespace("MAPI")
  Set myFolder = _
  Set myItems = myFolder.Items
  Set myItem = myItems.Add("IPM.Note.FormTest1")

  Set myOlApp = Nothing
  Set myNameSpace = Nothing
  Set myFolder = Nothing
  Set myItems = Nothing
  Set myItem = Nothing
End Sub

Open in new window

      It worked perfectly, BUT... I have to figure it out a way WITHOUT using Macros, once we cannot implement Macros on Outlook automatically (this is supposed to be a release to a lot of people, so it has to be "click and install").

      Another idea that I had was to use the VBScript to "install" the .oft file on a Folder and the button would use Hyperlink with TooltipText pointing directly to the specific path, but that doesn't seem to be a good approach, since the Outlook will pop up an security alert every time.

      Thanks for the help, I'm still working on a  way around to this, if I figure it out something, I'll come back here to inform!
Chris RaisinRetired Senior Systems Analyst/Programmer

Rather than a macro that needs to be run explicitly, what about searching for an "Installed" flag in the registry by using "GetSetting()" function, with a default of 0 (False). If the value retrieved for the key (lets just call it "oftInstalled") is false upon your the startup of something in the Outlook session (perhaps by placing placing the code into "ThisOutlookSession" module to sense when an action happens (such as when an email is created, or arrives, or is sent). If it not installed, then some code installs it and saves the value "True" to the key in the registry (using "SaveSetting()") . If the value found is "True" (instead of false) then the file (or whatever it is you are creating) does not have to be created since it already exists, and you can go ahead and use it. There are many things you can save to registry, not js=t flags. Yu could save the locations and names of files
(which could be different on different machines). You can even ask the user to stipulate where a file is to belocated if (upon using GetSetting() you find no value held in the registry. Once the user supplies an answer (via a pop-up box or perhaps an "Open File"-type Dialogue you can then save the file anme and location back into the registry.

The downside of using the registry is that should it ever become corrupted or should the user restore from a back-up the current value may be lost.
I suppose that is no worse than the alternate, where we store vaue into text files and place then in a folder somewhere(where the filed could be deleted or corrupted. :-)

Excuse my carrying on here, but I am not too clear exactly what you are attempting.  Perhaps these thoughts (or ramblings) may lead your thought processes around so you may suddenly have a "Eureka!" moment in your method of attack on your problem.

If I am way off beam, please let me know.
If I could get a working sample going I could probably better envisage what you are attempting.



I've just noticed that the button that I've created dissapear when I close the Outlook =/ . Isn't there a way to save the changes ?

     In short, what I need is a VBScript that would install a .oft Form file and create a button to that Form into an Outlook User! So, everytime the user opens the Outlook, the button will already be there with the form installed, just one click and the form pops out (the VBScript and the external .oft file will not be necessary anymore).

     You've mentioned using flags on registry, but I'm not aware on doing this, this is the firts time I'm dealing with VBScript , could you enlight me on that? I could give any other information that you need.

Thanks for the help!
Retired Senior Systems Analyst/Programmer
Unlock this solution and get a sample of our free trial.
(No credit card required)
Chris RaisinRetired Senior Systems Analyst/Programmer

Since there has been no further ersponse and I have supplied the only assistance, I suggest the record be closed with my final comments to the author deemed to be the best solution. The offer to still help if the author needs questions answered still applies.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.