I think most people have read an article or tip about the benefits of creating reports from Lotus Notes in Microsoft Word, using LotusScript and OLE. One of the challenges I ran into, however, is how to navigate within a MSWord document while the report is being created. Specifically, when adding tables to a report, how do I move the cursor out of a table and to the next line of the report in order to add more information? And then, how do I create another table after the text I just entered?
The example I have included shows how to:
- Create the MSWord OLE object.
- Define a range at the end of whatever text has already been entered.
- Add a simple table to the document, adding some text and basic table customization.
- Navigate out of the table.
- Then add some more text without a table.
- And finally, loop through and add some more tables and text.
Of course, you could write a whole routine to create several Word documents one right after another. Check other tips on how to programmatically save your new Word document.
P.S. I've included extra constants to save you the work of finding them out!
'OLE: Create Word Document: Option Public Option Declare ' WdGoToItem Constants Const wdGoToLine% = 3 Const wdGoToLast% = -1 ' WdTableFormat Constants Const wdTableFormatClassic2% = 5 ' WdColorIndex Constants Const wdAuto% = 0 Const wdBlack% = 1 Const wdBlue% = 2 Const wdBrightGreen% = 4 Const wdByAuthor% = -1 Const wdDarkBlue% = 9 Const wdDarkRed% = 13 Const wdDarkYellow% = 14 Const wdGray25% = 16 Const wdGray50% = 15 Const wdGreen% = 11 Const wdNoHighlight% = 0 Const wdPink% = 5 Const wdRed% = 6 Const wdTeal% = 10 Const wdTurquoise% = 3 Const wdViolet% = 12 Const wdWhite% = 8 Const wdYellow% = 7 ' WdParagraphAlignment Constants Const wdAlignParagraphCenter% = 1 Const wdAlignParagraphLeft% = 0 Const wdAlignParagraphRight%= 2 Sub Initialize ' Set the Microsoft Word Object Dim varWrdApp As Variant Set varWrdApp = CreateObject( "Word.Application" ) ' Show Word varWrdApp.Visible = True ' Add a new document varWrdApp.Documents.Add ' Set the Word Selection Dim varWrdSelection As Variant Set varWrdSelection = varWrdApp.Selection ' Start a loop to create sections Dim varWrdRange As Variant Dim varWrdTable As Variant Dim intPos As Integer Dim x As Integer For x% = 1 To 5 ' loops this many times for example's sake ' Find the end of the Word selection intPos = varWrdSelection.End ' Define the range to the end of the selection and add a new table Set varWrdRange = varWrdApp.ActiveDocument.Range( intPos, intPos ) Set varWrdTable = varWrdApp.ActiveDocument.Tables.Add( varWrdRange,
1, 1 ) ' simple 1 x 1 table varWrdSelection.TypeText "Heading " & x% With varWrdTable ' Set the shading on the first row to light gray .Rows( 1 ).Cells.Shading.BackgroundPatternColorIndex
= wdGray25% ' could expand to multiple rows ' Bold the first row .Rows( 1 ).Range.Bold = True ' Center the text in Cell (1,1) .Cell( 1, 1 ).Range.Paragraphs.Alignment =
wdAlignParagraphCenter% End With ' Put the cursor at the end of the selection varWrdSelection.GoTo wdGoToLine%, wdGoToLast% ' Add text to document Call InsertLines( varWrdSelection, 1) varWrdSelection.TypeText "Here's line one of Heading " & x%
& "'s report." Call InsertLines( varWrdSelection, 1) varWrdSelection.TypeText "Here's line two of Heading " & x%
& "'s report." Call InsertLines( varWrdSelection, 1) varWrdSelection.TypeText "Here's line three of Heading " &
x% & "'s report." Call InsertLines( varWrdSelection, 2) Next ' Delete the objects Set varWrdTable = Nothing Set varWrdRange = Nothing Set varWrdSelection = Nothing Set varWrdApp = Nothing End Sub Sub InsertLines( varWrdSelection As Variant, intNumLine As Integer ) Dim intCount As Integer ' Insert the specified number of blank lines For intCount = 1 To intNumLine varWrdSelection.TypeParagraph Next intCount End Sub
This was first published in March 2002