Passing arguments to Word macros


Update 2012.02.10:  a better solution is now available . See Passing arguments to Word macros (2)

Passing arguments to Word macros is very desirable and often necessary. Having parametrized macro allows the macro to be reused in your organisation. Unfortunately there is no direct way to provide arguments to macros when invoking the macros from outside Office, like RPE does.

The solution is given by the concept of Document variables. You need design your macro to use these variables as arguments and set the values when you invoke the macro. To demonstrate this I will show how to design a macro that will set the document title, parametrize the macro and invoke it from RPE.

Step 1: Design the macro

[The final Word document containing the macro is attached. testdoc]

Create a new Office document and enter the code bellow.


' The macro adds a title to the document. The title is provided in the
' document variable "rpe_title"
Public Sub addTitle()

ActiveDocument.Range.Select

' insert a paragraph and its content at the beginning of the document
Selection.InsertParagraphBefore
Selection.InsertBefore ("<Document title>")

ActiveDocument.Range.Paragraphs.Item(1).Style = "Title"

End Sub

The above macro adds a title at the beginning of the document but the content is hardcoded. Enter “document variables”

' The macro adds a title to the document. The title is provided in the
' document variable "rpe_title"
Public Sub addTitle()

Dim title As String
' set a default value
title = "<document title>"

' iterate all the variables and get the value of the variable named rpe_title
' Best practice: avoid using common names for your variables such as "title", "author" etc
' as they could be used by others. It is best to prefix them with a string that is unlikelly to
' be used by others
Dim var As Variable
For Each var In ActiveDocument.Variables
If var.Name = "rpe_title" Then
title = var.Value
End If

Next

ActiveDocument.Range.Select

' insert a paragraph and its content at the beginning of the document
Selection.InsertParagraphBefore
' use the variable's value for the title's content
Selection.InsertBefore ( title)

ActiveDocument.Range.Paragraphs.Item(1).Style = "Title"

End Sub

Best Practice: avoid using common names for your variables such as “title”, “author” etc as they could be used by others. It is best to prefix them with a string that is unlikelly to  be used by others

Best Practice: provide a default value for all your variables in your macro. Choose a value that can be easily noticed in the document as it will help you spot problems in the macro design or invocation.

The title’s content is no longer hard coded and now it’s time to show how to invoke it.

Step 2 – Test the macro

I have modified runmacro.vbs provided with RPE to achieve this.


'*******************************************************************************
' *
' * Licensed Materials - Property of IBM
' *
' * (c) Copyright IBM Corp. 2008, 2009 All Rights Reserved
' *
' * US Government Users Restricted Rights - Use, duplication or
' * disclosure restricted by GSA ADP Schedule Contract with
' * IBM Corp.
' *
'*******************************************************************************

' ignore errors
On Error Resume Next

' start only if 2 arguments are provided
if WScript.Arguments.Count <> 3 then
wscript.quit
end if

Set word = CreateObject("Word.Application")
word.Visible = TRUE

' the document to open is received as argument 0
Set doc = word.Documents.Open( Wscript.Arguments(0))

' NEW: set the variable for the document
' the name of the variable is received in argument 3
' the value of the variable is received in argument 4
doc.Variables.Add Wscript.Arguments(3), Wscript.Arguments(4)

if doc <> nul then
' the macro to run is received in argument 1
word.Run( Wscript.Arguments(1))
word.ActiveDocument.Save
end if

' avoid MS Word prompting to save normal.dot
word.NormalTemplate.Saved = true
word.Quit 0

The arguments passed to the script are:

  • 0 – the path of the document containing the macro
  • 1 – the macro to run
  • 2 – the name of the variable  to set
  • 3 – the value of the variable

Tip the script can be extended to support setting more variables

The only change in this script compared to the one shipped with RPE is line 30 where the variable is set in the document before the macro is run. Invoking the macro is done using the following command line:

cscript "d:\rpeactual\runmacro.vbs" "d:\rpeactual\testdoc.doc" "addTitle" "rpe_title" "Test doc title"

The last piece of the puzzle is to invoke this from RPE.

Step 3 – Invoke the macro from RPE

To connect all the pieces I will be using RPE’s abilities to use stylesheets and the new “post processing” feature added in RPE 1.1.2

  1. set RPE to use as stylesheet the .doc/.dot containing the macro ( same as previouse)
  2. invoke the macro in the postprocessing command instead of using the “macro” property of the Word output

You can observe that the same command line is used as the one that tested the script. The only change is to use the ${Word} variable so that the changes are operated on the Word output.

And the result:

The Word document containing the macro is attached. testdoc

Advertisements

Author: Dragos Cojocari

Architect for Rational Publishing Engine

6 thoughts on “Passing arguments to Word macros”

  1. Dragos,

    I assume that you could use this technique to set document properties? Or is the exporting fields technique better?

  2. Neal, Dragos….
    Is there any information out there on how windows scripting can be integrated with RPE?? I need something very basic considering my intense unfamiliarity with shell scripting and RPE…

    1. You can use the RPE pre and post processing commands, as well as the Data Source commands to run external commands. These commands can simply be invocations of Windows Shell Scripts. Actually this is how RPE runs its macros, it invokes the Windows Script interpreter to run a script.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s