Making scripting faster

This page in 2007
Top  Previous  Next

Example 1: Basic Use

To introduce OAK scripting as simply as possible, we intentionally provided an example code fragment that had as few lines as possible.

Option Explicit

Sub Main()
  Dim o As Operis_OAK.IOAKAddIn
  Set o = CreateObject("Operis.OAK.Connect")
  Set o.ExcelApplication = Application
  o.BuildNameDatabase
End Sub

Example 2: Caching the COM Object

The CreateObject step is relatively expensive. If you are going to use OAK scripting intensively, it is a good plan to cache this item so that the object only gets created once in a session.

Option Explicit

Private oakObject As Operis_OAK.IOAKAddIn

Function MyOAKAddIn as Operis_OAK.IOAKAddIn
  If oakObject is Nothing Then
     Set oakObject = CreateObject("Operis.OAK.Connect")
     Set oakObject.ExcelApplication = Application
  End If
  Set MyOAKAddIn = oakObject
End Function

Sub Main()
  MyOAKAddIn.BuildNameDatabase
End Sub

The IOAKAPI object can be cached similarly.

Option Explicit

Private oakObject As Operis_OAK.IOAKAPI

Function MyOAKAPI as Operis_OAK.IOAKAPI
  If oakObject is Nothing Then
     Set oakObject = CreateObject("Operis.OAK.Connect")
     Set oakObject.ExcelApplication = Application
  End If
  Set MyOAKAPI = oakObject
End Function

Sub Main()
  Dim r as Range
  Set r = MyOAKAPI.SearchFor(Selection, "dog", SearchContentType_Value, True)
  If Not r Is Nothing then r.Select
End Sub

Example 3: Modules and Properties

Since the OAK COM class implements both the IOAKAPI and IOAKAddIn interfaces, the same cached object can be used for both:

Module: OperisAnalysisKit

Option Explicit

Private oakObject As Object

Private Function GetOAKObject() As Object
  If oakObject Is Nothing Then
     Set oakObject = CreateObject("Operis.OAK.Connect")
     Set oakObject.ExcelApplication = Application
  End If
  Set GetOAKObject = oakObject
End Function

Public Property Get API() As Operis_OAK.IOAKAPI
  Set API = GetOAKObject
End Property

Public Property Get UI() As Operis_OAK.IOAKAddIn
  Set UI = GetOAKObject
End Property

Module: OtherModule

Option Explicit

Public Sub UseOAK1()
  Dim r As Range
  Set r = OperisAnalysisKit.API.SearchFor(Selection, "dog", SearchContentType_Value, True)
  If Not r Is Nothing Then r.Select  
End Sub

Public Sub UseOAK2()
  OperisAnalysisKit.UI.BuildNameDatabase
End Sub