VBScript Examples
The following topics describe how to use VBScript to automate common tasks. You can use these examples in your own vault configuration. For expert assistance with VBScript customization, contact the Accruent Professional Services department or a Solution Partner.
Nearly every vault has at least one property that must only contain unique values. That is, the value should not be duplicated anywhere in the vault, for example, document names or numbers. Just like Windows, Meridian prevents duplicate document names in the same folder, but to prevent duplicates in an entire vault requires a VBScript validation expression.
The IsUniqueValue method of the Document object queries the vault for a given value in a given property. You can use this method in an expression for the This expression must Be True option on the Validation page of a custom property:
Document.IsUniqueValue (Document.MyProperty, Value)
When documents are copied, their property values are copied as well. This may be undesirable for some properties that you want the user to enter or that should be unique. To accomplish this, you need VBScript to react and clear the properties when documents are copied. A custom event procedure for the DocGenericEvent_BeforeNewDocument event is the ideal solution. This event occurs just before document copies are created, when you can set the property values that will be saved for the copies. If the properties are not used for all document types in the same vault, you can use a conditional statement to take the appropriate action for each document type as in the following example.
Sub DocGenericEvent_BeforeNewDocument(Batch, Action, SourceFile, DocType, DocTemplate)
If Document.DocumentType.InternalName = "QualityDocument" Then
Document.Category = "Quality Documents"
Document.LastAuditBy = vbNullString
Document.LastAuditOn = Null
Document.AuditResult = vbNullString
Document.AuditorNotes = vbNullString
Document.NextAuditBy = vbNullString
Document.NextAuditOn = Null
Document.AuditHistory = vbNullString
Document.Author = vbNullString
Document.ApprovedBy = vbNullString
Document.ApprovedOn = Null
Document.Comments = vbNullString
Else If Document.DocumentType.InternalName = "NonConformance" Then
Document.Category = "Non Conformances"
End If
End Sub
Note:
Date properties do not accept string values. Therefore, instead of setting them to vbNullString, set them to Null.
It can be convenient for some property values to be set automatically at specific transitions in a document's workflow. This can be done by determining the state to which a document is being routed and setting property values accordingly. Meridian provides many constants that you can use, including the AS_WF_STATE enumeration used in the following example.
Sub DocWorkflowEvent_AfterChangeWFState(Batch, SourceState, TargetState, Person, Comment)
If TargetState = AS_WF_APPROVED Then
Document.ApprovedOn = vbDate
Document.ApprovedBy = User.FullName
End If
If TargetState = AS_WF_UNDERCHANGE Then
Document.ApprovedOn = Null
Document.ApprovedBy = ""
End If
End Sub
Automatically generating email messages is useful for many purposes, particularly during customized event procedures. The following example code shows a typical procedure for setting properties of a NewMailMessage object. It uses constants in the AS_MAPIMSG_RECIP_TYPE and AS_MAPIMSG_SEND_FLAGS enumerations to set various properties of the message before sending it.
Sub SendEmailMessage()
Dim oMessage
Set oMessage = Client.NewMailMessage
'Recipients
oMessage.Recipients.Add "",AS_MMRT_TO,"<recipient.name@anotherdomain.com>"
oMessage.Recipients.Add "",AS_MMRT_CC,"<recipient.name@anotherdomain.com>"
'Sender
oMessage.Originator.Address = "<sender.name@mydomain.com>"
oMessage.Originator.Name = "Sender's Name"
oMessage.Originator.Type = AS_MMRT_ORIG
'Subject
oMessage.Subject = "The subject of the message"
'Body text
oMessage.NoteText = "The body text of the message"
'Attachments
oMessage.Attachments.Add "c:\temp\my.bmp", ""
'Send it with MAPI options and reset when done
oMessage.Send AS_MMSF_LOGON_UI
oMessage.Clean
End Sub