The Usage of VBSript in 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 utilizations 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 Experiences 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 experincia").enabled = true Case "No" MyPage.Controls("CheckBox2").value = false MyPage.Controls("Anos de experincia").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

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.