Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site

   

Subscribe to OutlookExchange
Anderson Patricio
Ann Mc Donough
Bob Spurzem
Brian Veal
Catherine Creary
Cherry Beado
Colin Janssen
Collins Timothy Mutesaria
Drew Nicholson
Fred Volking
Glen Scales
Goran Husman
Guy Thomas
Henrik Walther
Jason Sherry
Jayme Bowers
John Young
Joyce Tang
Justin Braun
Konstantin Zheludev
Kristina Waters
Kuang Zhang
Mahmoud Magdy
Martin Tuip
Michael Dong
Michele Deo
Mitch Tulloch
Nicolas Blank
Pavel Nagaev
Ragnar Harper
Ricardo Silva
Richard Wakeman
Russ Iuliano
Santhosh Hanumanthappa
Steve Bryant
Steve Craig
Todd Walker
Tracey J. Rosenblath
 
 

The use of VBScript with Outlook forms

The code that runs in a Outlook form is usually written in VBScript, a simple language that you probably know because of it utilization in Internet pages for Microsoft Internet Explorer. The VBScript comes along with Internet Explorer, but you can also do the download separately, with DCOM components. You should also have documentation about this language, since Outlook does not have information about functions, methods and VBScript objects.

 

VBScript is usually updated with the new versions of Internet Explorer, but it is better to be sure that the version is working correctly, since there is a certain functions’ types that with the time might disappear and can not be used.

 

Soon I am going to put in my page some examples of different VBScript utilization’s possibilities and VBScript process with Outlook forms and also some documentation and practical examples for interested users.

 

Code examples:

 

This code example is used to deactivate a control when you select other one.

 

The page is Ticket.

 

The controls are Checkbox 1 and Checkbox 2.

 

Fields are Yes, No and Experience’s Years.

 

Sub Item_CustomPropertyChange(ByVal FieldName)
  Set MyPage = Item.GetInspector.ModifiedFormPages("Ticket")
	Select Case FieldName
		Case "Sim"   'if user selects Sim
			MyPage.Controls("CheckBox1").value = false
			MyPage.Controls("Anos de experiência").enabled = true
		Case "Não"
			MyPage.Controls("CheckBox2").value = false
			MyPage.Controls("Anos de experiência").enabled = false
	End Select
End Sub

 

This example is necessary when users have to fulfil a required field:

 

The Model, Operative System and Software are fields, not controls.

 

Sub Send_click
    	Item.Save
End Sub
 
Function Item_Write
    If Item.UserProperties.find("Modelo computador") = "" Then
	MsgBox "Deverá preencher o Modelo do PC!"
	Item_Write = False
	Exit Function
    End If
    If Item.UserProperties.find("Sistema operativo") = "" Then
	MsgBox "Deverá preencher o Sistema Operativo!"
	Item_Write = False
	Exit Function
    End If
    If Item.UserProperties.find("Software") = "" Then
	MsgBox "Deverá preencher o Software!"
	Item_Write = False
	Exit Function
    End If
End Function

   

Code necessary to reveal the actual username.

 

Sub CommandButton1_Click()

      

   MsgBox "O nome de utilizador é " & Application.GetNameSpace("MAPI").CurrentUser

 

End Sub  

   

Creating Items (with sequential numbers) into a folder.

 

Sub Item_Open()

      ' Set the Outlook NameSpace

      Set olns = Application.GetNameSpace("MAPI")

      ' Set the folder to the Contacts folder

      Set myFolder = olns.GetDefaultFolder(olFolderContacts)

      ' #1/1/4501# is Outlook's internal representation of an empty data value.

      If Item.LastModificationTime = #1/1/4501# Then

         ' Item is a brand new item

         MsgBox "New Item"

         If Item.Mileage = "" Then

            ' We're creating the first item in the folder

            Item.Mileage = 1

         Else

            ' Increment the index in the Mileage field

            Item.Mileage = CStr(CInt(Item.Mileage) + 1)

         End If

         ' The following 3 lines re-publish the form to the Contacts folder with the

         ' new index number in the Mileage field.

         Set myForm = Item.FormDescription

         myForm.Name = myFormName

         myForm.PublishForm olFolderRegistry, myFolder

      Else

         ' Item is an existing item

         MsgBox "Existing Item, doing nothing"

      End If

End Sub  

 

Fulfilling a listbox or a combox with VBScript.

 

Sub Item_Open()

 

      ' Sets the name of page on the form (P.2)

     Set FormPage = Item.GetInspector.ModifiedFormPages("P.2")

 

      ' Sets Control to a list box called ListBox1.

     Set Control = FormPage.Controls("ListBox1")

 

      ' Assign values to the list box.

     Control.PossibleValues = "Blue;Green;Red;Yellow"

 

End Sub

 


RETURN to my Article Index


Disclaimer: Your use of the information contained in these pages is at your sole risk. All information on these pages is provided "as is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Stephen Bryant or Pro Exchange. OutlookExchange.Com, Stephen Bryant and Pro Exchange shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

Copyright Stephen Bryant 2008