Manage Learn to apply best practices and optimize your operations.

Manipulating the header and footer of Microsoft Word using LotusScript

Our LotusScript expert Cregg Hardwick explains how to manipulate the header and footer of Microsoft Word using LotusScript and COM.

Question 5: Do you know how to manipulate the header and footer of Microsoft Word using LotusScript (i.e., get inside a header type text and get out again)?

Cregg Hardwick: You will have to use COM. Generally, you will want to record a macro in Word, then take a look at the generated code to see how to write your code. If any constants are referenced in the macro, you'll have to manually create these in your LotusScript code since Lotus won't know about these constants. Other than that, VisualBasic (VB) and LotusScript are nearly identical.

In my version of Word, you would take the following steps:

  1. Click "Tools/Macro/Record new macro" and select the default macro name in the new macro dialog.
  2. Create a header (click "View/Header and Footer" from the menu).
  3. Click the button to "Stop recording".
  4. Click
  5. "Tools/Macro/Macros," select the new macro you just created and click "Edit."
  6. Examine the generated code. If possible, simplify it by removing anything you don't need. Run the macro as needed to test.
  7. To get the values of any constants referenced in the macro, add "MsgBox format(wConstwhatever)" and run the macro. Record the values so you can define them in Notes.
  8. Copy this code and paste it into your LotusScript code.
  9. For any constants that you need, add CONST declarations using the values collected above.
  10. Add code to open the Word document and so forth:

The finished code might look something like this:

 Dim Application As Variant
 Set Application = createObject("Word.application")
 Call Application.activate of your choice) 

Const wdFieldTime=32 
' These would be VB constants that we
 have to define in our code.
 Const wdFieldDate = 31
 Const wdSeekMainDocument=0
 Const wdSeekCurrentPageHeader = 9
 With Application
  .ActiveWindow.ActivePane.View.SeekView = 
  .ActiveWindow.ActivePane.View.SeekView = 
  Call .Selection.Fields.Add
(Application.Selection.Range, wdFieldTime)
  Call .Selection.Fields.Add
(Application.Selection.Range, wdFieldDate)
 End With

Record small macros so that the resulting VB code is understandable and test everything. Remember, VB uses name-value pairs to pass parameters, but LotusScript does not. VB will, however, generate the parameters in the correct order for calling without named parameters, so they are easy to convert. Also, VB code assumes the presence of the application it is running in, so in LotusScript you have to tack the application reference onto everything. Finally, LotusScript requires the "Call" keyword where VB sometimes does not. So where Word might generate the code

Selection.Fields.Add Range:=
Selection.Range, Type:=wdFieldDate

in LotusScript this translates to

 Call Application.Selection.Fields.

From there, just play around until you get it to do exactly what you want.

Cregg Hardwick is Collaboration Solutions Technical Lead for CenterPoint Energy in Houston. He has an MBA, a PCLP and an MCAD and focuses on collaboration and business process support applications, and has been a Notes developer since 1998. He has graciously agreed to answer some of the many questions that come in regarding LotusScript.

Dig Deeper on LotusScript



  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...

  • HTML cheat sheet

    This is a really cool cheat sheet if you're looking to learn more about HTML. You'll find just about everything you every wanted ...

  • Carol Woodbury: Security

    Carol Woodbury