Problem solve Get help with specific problems with your technologies, process and projects.

Can I use LotusScript to merge cells in a Microsoft Word table?

Read about merging cells in a Microsoft Word table using LotusScript and an Excel example, along with the thought process that goes along with it.

Is there any way to merge cells in a Microsoft Word table using LotusScript?
In a word, yes. But this will require communication with Microsoft Word through the component object model (COM). In theory, this isn't too difficult, but you need to understand COM.

Here are some key points to keep in mind:

  • First, you are going to create an object that is a handle to Microsoft Word. Microsoft Windows will actually open Word as though you clicked on it from the start menu.
  • Next, you'll need to know what you can call using this object, because you won't receive any auto-complete hints. An easy way to find out what to call is to open Microsoft Word and create a macro. You can then look at the macro to see what it did. To do so, open a Microsoft Word document and click Tools -> Macros -> Record new macro. In practice, there's more to it than that because Visual Basic for Applications (VBA) macros often use named parameters, instead of positional parameters.
  • Recorded VBA macros include all possible parameters for most calls, even when specifying default values. This is helpful since it allows you to use less-complicated calls from LotusScript.

Here is an example of a Microsoft Word interoperability agent. My first step was to write some LotusScript code to open Microsoft Word, and then make it visible:

Dim wApp As Variant 
Set wApp = CreateObject("Word.application")
wApp.Visible = True

Note: If the Microsoft Word application isn't visible when the code terminates -- either as requested or through an error -- an instance of Word will remain in memory. To close it, you must go to the processes list in task manager. If I didn't know which type of object to request, I could have looked on the "References" tab next to the agent's code window. This allows you to browse through all the COM objects registered on your system.

I went into Microsoft Word and recorded a macro while I created a blank document containing a table. The macro included the following code:

Documents.Add Template:="Normal", 
NewTemplate:=False, DocumentType:=0
ActiveDocument.Tables.Add Range:=
Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=w
dWord9TableBehavior, AutoFitBehavior:= _

Next, I migrated the code to LotusScript. The first line opens the blank document with the default template. This template could have been omitted. I didn't include the other parameters:

Call wApp.Documents.Add("Normal")

The next line is trickier. Because I'm unfamiliar with Microsoft Word's object model, I had to guess. I'm not concerned with the specifics, so I assumed that there would be an override -- taking only the number of rows and columns. This didn't work, so I included the range parameter again. This also was unsuccessful; therefore, I searched the Web for Word COM tables.add.

My search generated a Microsoft page that described the tables.add method. This confirmed that the range parameter is required. After following the link to the range method page, I found that it takes two parameters. I used the following code, which worked, but created the 3 x 5 tables without any cell borders:

Call xlApp.ActiveDocument.Tables.
Add(xlapp.documents(1).range(0,0),3, 5)

I didn't have a Microsoft Word example handy, so I used Excel instead. This should give you a general idea on the method.

Do you have comments on this Ask the Expert Q&A? Let us know.

Related information from

  • Tip: Create file system labels for Excel and Word mail merges
  • 10 tips in 10 minutes: Our top LotusScript tips
  • Reference Center: LotusScript tips and advice
  • 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 ...