Home     Products      Support      Corporate     Sign In 
Support Knowledge Base, Article 733
Product
WordWriter
Version
1.0 SE
Title
Populate a WordWriter generated document with line items, such as a bulleted list, with an unknown number of text entries.
Problem

A common need for WordWriter applications is the ability to add a bulleted list at runtime, when the number of lines is not known at design time. Version 1 of WordWriter does not have the ability to "stretch" or "shrink" a document to accomodate a variable number of line items. This feature will be available in a future release of the product.

However, there is a way to accomplish the desired effect using macros which will execute client-side when the WordWriter-generated document is opened in MS Word. This can be achieved by planning for the maximum possible number of line items, providing blank strings for the unnecessary datamarkers (to prevent runtime errors) and writing a macro in the template to clean out unused line items.

Solution

First, start by creating a template with the maximum number of bulleted merge fields that could possibly fill your template.

  • {MERGEFIELD Value0 }
  • {MERGEFIELD Value1 }
    ...
  • {MERGEFIELD Value20 }

Next, create a macro for your template with a function to remove unused line items and hook it up to the open event of the document. This macro will run when your document is opened on the client. VBA code for this macro is provided below:

Sub RemoveUnusedBullets()
   'Loop through the paragraph collection
   'and delete any paragraphs that have
   'less than two characters. The first
   'character would be the bullet; the second
   'character would be the start of text that should be saved.

 i = 1
  While (i <= ActiveDocument.ListParagraphs.Count)
      If (ActiveDocument.ListParagraphs(i).Range.Characters.Count < 2) Then
         ActiveDocument.ListParagraphs(i).Range.Delete
      Else
         i = i + 1
      End If
   Wend

End Sub

Sub AutoOpen()
  RemoveUnusedBullets
End Sub

Lastly, in your server-side script, be sure to provide logic to handle the unused datamarkers. If your template contains 20 line items, but your datasource is only providing 15 line items, be sure to provide blank strings for the last 5 values.

For example, in the template attached to this KB article, 6 datamarkers are expected.

  • {MERGEFIELD ProductName \* MERGEFORMAT }
  • {MERGEFIELD CompanyName \* MERGEFORMAT }
  • {MERGEFIELD URL \* MERGEFORMAT }
  • {MERGEFIELD ProvideNoValue1 \* MERGEFORMAT }
  • {MERGEFIELD ProvideNoValue2 \* MERGEFORMAT }
  • {MERGEFIELD ProvideNoValue3 \* MERGEFORMAT }

However, in the associated ASP script* (also attached) only three values are available for populating the template, so the last three values of the data array will be provided as blank strings. When the document is opened, the macro will run and strip out these last three bullets that are not associated with a line of text.

    '--- The array of names that match the datamarker names
    '--- the array indices of the name array should correspond
    '--- to the indices of the value array
    arrNames = Array("CompanyName", &_
      "ProductName", &_
      "URL", &_
      "ProvideNoValue1", &_
      "ProvideNoValue2", &_
      "ProvideNoValue3")

    '--- The array of values
    '--- these values are what will be populated into mergefields in the
    '--- template document
    arrValues = Array("SoftArtisans", &_
      "WordWriter", &_
      "http://www.softartisans.com", & _
      "","","")

The resulting bulleted list will appear with the appropriate number of bullet points as follows:

  • SoftArtisans
  • WordWriter
  • http://www.softartisans.com

Note: This solution requires that the client-user allows macros to run in Microsoft Word Documents. If they choose to disable macros, the data will be transferred successfully and without errors, however the unused bullets will not be stripped out of the document.

*The attached sample uses classic ASP to illustrate server-side scripting. WordWriter for Windows is a native .NET product but also includes a COM interop assembly that allows it to run in a COM (ASP, VB) environment. The code provided in this article that demonstrates the creation of an array, is the same code that would be used in an ASP.NET application written with VB.NET. For complete examples using arrays as a datasource written with C# or VB.NET, or samples for WordWriter for Java, please check the product samples that install with your version of WordWriter.
Related Links
OfficeWriter Home Page
OfficeWriter Enterprise Edition
Latest OfficeWriter News
OfficeWriter: Programmatic Runtime Control

Attachments
Attachments/KB733_WordWriterV1.zip
Created : 10/31/2003 12:54:40 PM (last modified : 10/31/2003 12:54:40 PM)
Rate this article!
 
Comments