Personalizzazione avanzata > Business Logic Customization > DTI Customization Framework > Customize the Windchill tab in Microsoft Office > Create Custom Commands Using Macros
  
Create Custom Commands Using Macros
Windchill Desktop Integration only expects the macro code to be available to the Office document in question.
DTI can run both types of macros: those which were injected into the Office document through the import_module action or those which already existed in the document.
* 
PTC recommends that the macros be thoroughly tested using live Microsoft Office documents before being uploaded on the Windchill server for deployment. Windchill Desktop Integration is not responsible for any exceptions arising during macro execution. All errors are displayed to the users.
The following example illustrates a custom action configuration:
<action method="run_macro" type="CLIENT">
<defaultdata key="macro_name">Macro1</defaultdata>
<defaultdata key="macro_args">Albert Einstein</defaultdata>
</action>
* 
The macro_name key is a reserved and mandatory keyword.
The macro_args is optional and can be used in case additional data needs to be provided to the macro.
Handling Multiple Inputs
In case the macro needs multiple inputs, PTC recommends that this be handled using VBA code within the macro itself.
For example, assuming the macro needs more than one argument, an XML file could be generated with the relevant data and the relative filename provided as a value to macro_args. This XML file could again be placed on the server so that it is picked up by all Windchill Desktop Integration clients on the subsequent connect action. Alternatively, multiple pieces of information could be provided using some delimiter (with proper XML escaping). The VBA macro code could again split the data on the delimiter and recreate the original information.
Injecting Macros
1. Create macro modules for injection.
The following example illustrates how to create a macro module for injection in Microsoft Excel:
a. Create a new folder named “Excel” under <Windchill>/codebase/com/ptc/windchill/enterprise/nativeapp/msoi/client/custom.
b. Create Module1.bas under it. and paste the following code into the file:
Sub Macro1(sUsername As String)
MsgBox("Hello " & sUsername & "! This is a macro alert.")
End Sub
* 
The Windchill Desktop Integration client automatically picks up all modules on the subsequent connect action on the server.
2. Import macro modules into Microsoft Office.
Importing macro modules into Microsoft Office requires changes to the wtCustomActions.xml file on the server. The following example illustrates the import module action:
<action method="import_module" type="CLIENT">
<defaultdata key="module_path">custom\Excel\Module1.bas</defaultdata>
</action>
* 
If the value of module path parameter is not an absolute path, then it is expected to be the relative path from the custom folder.
* 
The module is imported to different files and templates based on the Microsoft Office application in question. For example, Microsoft Word typically imports into the Normal.dot* template. Excel directly imports into the open workbook. The location can be confirmed by using the VBA editor (shortcut Alt + F11).
Removing Macros
To remove the macro in the Microsoft Office document after it has been run, use the following action configuration:
<action method="remove_module" type="CLIENT">
<defaultdata key="module_name">Module1</defaultdata>
</action>
The module_name key value should match with what is displayed as the module name in the VBA editor (Alt + F11).