Differences Between Scripting and OLE Automation

VBScript programs and OLE Automation programs are very similar. You can easily create VB or VBA programs, if you know how to use VBScript. However, some differences remain. The following example program highlights what differentiates OLE Automation from VBScript.

VBScript Program

The following VBScript program allows you to count the number of classes defined in an OOM and display that number in PowerDesigner Output window, then create a new OOM and display its name in the same Output window.

To do so, the following steps are necessary:

  • Get the current active model using the ActiveModel global function

  • Check the existence of an active model and if the active model is an OOM

  • Count the number of classes in the active OOM and display a message in the Output window

  • Create a new OOM and display its name in the Output window

'* Purpose:  This script displays the number of classes defined in an OOM in the output window.
Option Explicit
' Main function
' Get the current active model
Dim model
Set model = ActiveModel
If model Is Nothing Then
 MsgBox "There is no current model."
ElsIf Not Model.IsKindOf(PdOOM.cls_Model) Then
 MsgBox "The current model is not an OOM model."
Else
 ' Display the number of classes
 Dim nbClass
 nbClass = model.Classes.Count
 Output "The model '" + model.Name + "' contains " + CStr(nbClass) + " classes."
' Create a new OOM
 Dim model2
 set model2 = CreateModel(PdOOM.cls_Model)
 If Not model2 Is Nothing Then
  ' Copy the author name
  model2.author = model.author
  ' Display a message in the output window
  Output "Successfully created the model '" + model2.Name + "'."
 Else
  MsgBox "Cannot create an OOM."
 End If
End If

OLE Automation Program

To do the same with OLE Automation program, you should modify it as follows:

  • Add the definition of the PowerDesigner application

  • Call the CreateObject function to create an instance of the PowerDesigner Application object

  • Prefix all the global functions (ActiveModel, Output, CreateModel) by the PowerDesigner Application object

  • Release the PowerDesigner Application object

  • Use specific types for the variables "model" and "model2"

'* Purpose:  This script displays the number of classes defined in an OOM in the output window.
Option Explicit
' Main function
Sub VBTest()
 ' Defined the PowerDesigner Application object
 Dim PD As PdCommon.Application
 ' Get the PowerDesigner Application object
 Set PD = CreateObject("PowerDesigner.Application")
' Get the current active model
 Dim model As PdCommon.BaseModel
 Set model = PD.ActiveModel
 If model Is Nothing Then
  MsgBox "There is no current model."
 ElsIf Not model.IsKindOf(PdOOM.cls_Model) Then
  MsgBox "The current model is not an OOM model."
 Else
  ' Display the number of classes
  Dim nbClass
  nbClass = Model.Classes.Count
  PD.Output "The model '" + model.Name + "' contains " + CStr(nbClass) + " classes."
' Create a new OOM
  Dim model2 As PdOOM.Class
  Set model2 = PD.CreateModel(PdOOM.cls_Model)
  If Not model2 Is Nothing Then
   ' Copy the author name
   model2.Author = Model.Author
   ' Display a message in the output window
   PD.Output "Successfully created the model '" + model2.Name + "'."
  Else
   MsgBox "Cannot create an OOM."
  End If
 End If
' Release the PowerDesigner Application object
 Set PD = Nothing
End Sub