Launching a VB Macro with Arguments  

The task below illustrates how to add arguments to a macro.

Macros with arguments are features that can be:

stored in CATPart or CATProduct documents,
stored in catalogs. Double-click them in the catalog to run them,
called from a rule (VBScriptRun) or a reaction. In this case, arguments are passed from the rule.

The icon enables you to access the macro editor. In addition to the usual 'edit and run' capabilities, this editor allows you to:

specify arguments 
carry forward a feature definition to the editor just by selecting the feature either in the tree or in the geometry area.

ascenari.gif (1364 bytes)

  1. Click the icon. The script editor is displayed.

  2. Copy/paste the script below into the editor:

    Dim oActiveDoc As Document
    Set oActiveDoc = CATIA.ActiveDocument
    
    If (InStr(oActiveDoc.Name,".CATPart")) <> 0 Then
    
    Dim oParams As Parameters
    Set oParams = oActiveDoc.Part.Parameters
    
    Dim strParam1 As StrParam
    Set strParam1 = oParams.CreateString("FirstName", "")
    
    Dim strParam2 As StrParam
    Set strParam2 = oParams.CreateString("LastName", "")
    
    strParam1.Value = fname
    strParam2.value = lname
    
    Else MsgBox "The active document must be a CATPart"
    End If
    
    End Sub
  3. Enter the fname and lname arguments in the field located between the parentheses. The arguments must always be separated by a comma.

  4. Click OK to add the macro to the document. A 'VB Scripts' sub-node is added to the specification tree below the Relations node. A VB Script object is added below this sub node.

  5. Double-click the VB Script object. The Script Editor is displayed. The Insert Object Resolution button allows you to retrieve a feature definition. The VB Script.2 macro of the KwrObject.CATPart sample illustrates how to use this capability.

  6. Click Run script.... The Select Inputs for Script Arguments is displayed.

  7. If need be, select fname in the argument list, then enter a string into the value field (no quotation marks). Then select lname and enter the lname value.

  8. Click OK to run the script.  The two string type parameters are added to the document. Their values are those you have just specified.

Note that the VB script features with arguments are provided with a contextual menu enabling the user to launch the script.

aendtask.gif (1477 bytes)

Back Up Next