Tag Archives: WordTemplate

How to prevent line breaks after an empty merge field

Problem

When you use the WordTemplate object to populate a WordWriter template document with data retrieved from a data source, you may have situations in which a merge field is not bound to a value. The most common scenario is creating a mailing address. Each field of a mailing address may be represented by a merge field. Typically, the second line of the address is optional. In the generated document, if the merge field corresponding to the second line of the address does not have a value, a blank line may be present.

In Microsoft Word, a blank line generated during a mail merge can be suppressed using application settings. With WordWriter, a blank line is not automatically removed.

WordWriter template document:

Generated document:

Solution

The following discussion uses a merge field called AddressLine2 as an example, but is applicable to any field with optional value.

In the WordWriter template document, if the line break is separate from the AddressLine2 merge field, the line break will be present in the generated document whether or not Continue reading How to prevent line breaks after an empty merge field

What’s new in OfficeWriter 8.5

Spring has sprung, bringing with it our newest release of OfficeWriter: OfficeWriter 8.5!  What’s in store for this maintenance release? Scroll down to see the latest additions our development team has been working on.

WordTemplate – Embed DOCX files into templates

In OfficeWriter 8.0, we added the ability to embed RTF or HTML documents in Word files with WordTemplate.  The feature uses the document modifier to signify that a RTF or HTML document will be inserted. To learn more about using the document modifier, see our guide on inserting an embedded document under our WordTemplate Tutorials.

HTMLtoWord

We have also extended the feature to include DOCX files. Now you can embed other Word documents into your WordWriter templates. Continue reading What’s new in OfficeWriter 8.5

Enhancing your Word Document with Images Using OfficeWriter

One of the questions that comes up frequently when talking with customers is how can they get their images into their Word document with OfficeWriter? Depending on how you are using OfficeWriter and making your Word documents, there are a few ways to do this. The following outline several of these methods and cases in which to use them.

1. Design your document in Word with images

This is the easiest way to get images into your document. Since OfficeWriter can use the Word file you have already created and allow you to enhance it with data from a database, you can get all of the design functionality of Word with the enhanced capabilities of OfficeWriter. This method is commonly used when designing a document that uses your company logo, for example.

2. Dynamically insert images into a document

Another option is to design your document in Word, but leave a place holder for a dynamically loaded image. (A quick overview on how to do this can be found here.) This method is great when the document is structured the same each time it is run and you want to keep your company logo static, but use different department logos.

3. Design your report with images stored in a database

You can use one of two methods to directly insert images into a Word template from an image column: the placeholder method or the the image modifier method.  For example, in MS SQL Server, there is a column type “varbinary” used just for that purpose. This method is perfect for creating an employee directory containing the employee’s picture along with their contact information, or for a product catalog, showing the product alongside the product details. (For further details on how to to design your report with images stored in a database, please see our documentation on inserting an image.)

4. Design your report with images referenced by PATH in a database

This is the more complicated case of the bunch. Unlike the previous instances where you were obtaining your images directly from a database, this process involves accessing your images stored in another location, such as a network share. Continue reading Enhancing your Word Document with Images Using OfficeWriter

How to insert HTML into Word documents

Problem

Many customers have rich text data that’s stored in databases as HTML and they need to display the HTML-formatted data in Word documents.

Solution

Using WordTemplate:

The ability to insert RTF and HTML documents into Word documents was introduced in WordWriter 8.0 for the WordTemplate object. For more information, read our tutorial for Inserting an Embedded Document in the documentation.

Using WordApplication:

SoftArtisans has an open source project called HTMLtoWord that allows users to insert well-formed HTML (XHTML) snippets into Word documents as formatted text. This uses the WordApplication project, so you must have WordWriter Enterprise Edition to use this project.

The project can be downloaded from SourceForge.net. For more information, see Using HTMLtoWord.

How to insert a line break using WordTemplate

Problem

To insert a line break in Word, you can press SHIFT+ENTER. This post covers how to insert a Word line break by injecting a newline character into the string of data that will populate a merge field.

Note: You cannot insert a paragraph break by injecting a character into your data. To start a new paragraph, you should use a separate merge field or use the WordApplication class to programmatically modify the document.

Solution

In this example, the template has one merge field, called “MyParagraph.”

The following code populates the merge field with two lines of text:

 WordTemplate wt = new WordTemplate(); wt.Open(Server.MapPath("./template.docx")); string[] fields = {"MyParagraph"}; object[] data = {"This is the first line." + '\n' + "This is the second line."}; wt.SetDataSource(data, fields); wt.Process(); wt.Save(Page.Response, "Output.docx", true); 

The populated document will contain a line break wherever ‘\n’ was inserted:

How to get a list of bookmarks from a Word document

Problem

You want to get a list of all the bookmarks in a document using WordWriter.

Solution

WordTemplate

The WordTemplate.Boomarks will return the names of all the bookmarks in a WordWriter template. This can be useful for binding data to the template with SetRepeatBlock, which requires the name of the bookmark for the repeatblock.

WordApplication

Use the GetElements method to retrieve all the elements of a particular type. In this case, the Element.Type is “Bookmark”.

For example, to retreive an array of all the bookmarks in a document:

Element[] eArray = document.GetElements(Element.Type.Bookmark);
//Get a handle on the first bookmark in the document
Bookmark bookmrk = (Bookmark)eArray[0];

If you need to retrieve a particular bookmark, use Document.GetBookmark(string), which returns the Bookmark object with the specified name.

New line characters are not displayed in WordTemplate output

Problem

The data source for a merge field contains text with new line characters (e.g. Chr(13) & Chr(10), “/n”), but the output is not being displayed on multiple lines in OOXML documents (.DOCX, DOCM) in WordWriter 4.0.0-4.1.0.

Solution

This issue was resolved in WordWriter 4.5.0. WordWriter software updates are available for download at www.officewriter.com/product-updates.

Error: BC3002: Type ‘WordTemplate’ is not defined

Problem

When working with a dynamically compiled aspx page (meaning that it does not have a code behind aspx.cs or aspx.vb page), the following error message may occur:

Compiler Error Message: BC30002: Type ‘WordTemplate’ is not defined

The error is thrown on the line that instantiates the WordTemplate object.

Solution

Note: Although the error message lists WordWriter, this applies to ExcelWriter as well.

Most customers have precompiled web applications or web sites, which means that the site is precompiled from Visual Studio with references to any necessary DLLs, including SoftArtisans.OfficeWriter.ExcelWriter.dll or SoftArtisans.OfficeWriter.WordWriter.dll. For more information on how to add OfficeWriter to a precompiled .NET application, see Adding OfficeWriter to your .NET Application.

However, if working with dynamically compiled pages, the references aren’t added ahead of time. The sample principles for precompiled applications apply here as well:

  1. Make sure to import or include the SoftArtisans.OfficeWriter.WordWriter (or SoftArtisans.OfficeWriter.ExcelWriter) namespace.
  2. Make sure to import the System.Web namespace because the ExcelWriter and WordWriter output options have a dependency on System.Web.
  3. Make sure that SoftArtisans.OfficeWriter.WordWriter.dll or SoftArtisans.OfficeWriter.ExcelWriter.dll is in the bin directory of the application. The alternative is to install the DLL into the GAC.

Word Error: Could not start converter mswrd632.wpc

Problem

When you try to save a Word 2007 template as part of a SQL Server Reporting Services RDL you get an error:

Word cannot start the converter mswrd632.wpc

Solution

This error is caused by a Windows security update published December 2009, affecting Windows XP, Windows 2000 and Windows Server 2003. You can resolve the issue by disabling the converter through the registry. Open regedit and delete the registry keys at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Text Converters\Import\MSWord6.wpc. Restart Word and the error should disappear.

You can find more information in this article. It also provies a script that will perform the registry fix for you. The additional steps mentioned in the update to that article have not been tested, as unregistering the converter has solved the problem in every instance in which we’ve encountered it.

Note that this issue only affects Word 2007 SSRS templates. Word 2003 templates are not affected. Also, Excel 2007 is not affected by this issue.

Documents generated by WordWriter do not print

Problem

Using WordTemplate or WordApplication, the document is generated and opens in Word. But when you try to print the file in Word, nothing happens. There are no error messages, the printer just does not start printing.

Solution

There are several possible reasons for why a WordWriter-generated document will not print:

Document contains an image that is set to “Behind text”

If the template file contains an image that is formatted to be behind text, then this issue was addressed in WordWriter 3.9.2. The recommended solution is to upgrade to the latest version of WordWriter.

If upgrading is not an option, then a possible workaround is to set the image as a watermark, so it will appear behind the text of the document:

Word 2007/2010:

  1. Go to the Layout tab.
  2. In the Page Background tab group, go to the Watermark dropdown.
  3. Select Custom Watermark.
  4. Select Picture watermark.
  5. Select the image that you want to appear behind the text.

Word 2003:

  1. Go to Format
  2. Go to Background
  3. Go to Printed Watermark
  4. Select the image that you want to appear behind the text

MS Word Background Printing Option

There is a known issue with some WordWriter-generated documents and the Background Printing option in MS Word. If your WordWriter-generated documents do not print or trying to print those documents results in error messages, try turning off Background Printing as described in this post.