More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

forum 24-May-2018 19:51

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

    Page [ 1 ]  
 Code Essentials Printer Friendly Version
Beginner’s section on how to work in the VBA environment, how to work with functions and expressions – generally not specific to Outlook
Get Last Number as a VBScript
VBScript GetLastNumber
The following code works in VB but not in VBScript. When run from a custom form it produces many many entries. I eventually get an error on the "myItem.Display" line after many many windows have been created. I expect only one resulting record. What am I doing wrong? Appreciate any advise that can be offered.

Sub Item_Open()
Dim myNamespace 'As Outlook.NameSpace
    Dim myItems 'As Outlook.Items
    Dim myFolder 'As Outlook.Folder
    Dim myItem 'As Outlook.TaskItem
Const olInteger = 20
Const olFolderTasks = 13

    Set myNamespace = Application.GetNamespace("MAPI")
    Set myFolder = myNamespace.GetDefaultFolder(olFolderTasks)
    Set myItems = myFolder.Items
    Set myItem = myItems.Add("IPM.Task.TaskMRF")
myItem.Subject = "New Subject from #2"
myItem.UserProperties("PrjID").Value = GetLastNumber("PrjID", myFolder) + 1

End Sub

Function GetLastNumber(fieldName, theFolder) ' As Long
    Dim colItems ' As Outlook.Items
    Dim objItem ' As Object
    Dim objProp ' As Outlook.UserProperty
    On Error Resume Next
    Set colItems = theFolder.Items
    colItems.Sort fieldName, True
    Set objItem = colItems.GetFirst
    Set objProp = objItem.UserProperties(fieldName)
    If Not objProp Is Nothing Then
        GetLastNumber = CLng(objProp.Value)
        GetLastNumber = -1
    End If
    Set colItems = Nothing
    Set objItem = Nothing
    Set objProp = Nothing
End Function

  20-Feb-2013  16:51
    Page [ 1 ]