Share via


Using Document Fragments [Office 2003 SDK Documentation]

A document fragment is a smart document control that is available for smart documents that you create for Microsoft Office Word 2003. Document fragments allow you to have pieces of text, or boilerplate text, that your users can quickly and easily add to a new document.

Creating document fragments

You can create either internal document fragments, which are hardcoded strings that reside within your smart document code, or external document fragments, which are contained within external Word documents.

Internal document fragments

If you create an internal document fragment, there are no special formatting issues to be aware of. You just program the text as a normal string.

External document fragments

If you create an external document fragment, the content of the document fragment must be in a Word document that has been saved as XML.

How?

  1. Create the document in Word.
  2. On the File menu, click Save As.
  3. Browse to the folder where you want to store your document fragment file.
  4. In the Save as type drop-down list, select XML Document (*.xml).
  5. Make sure the Apply transform and Save data only check boxes are cleared.
  6. Click Save.

Using the API

Use the PopulateDocumentFragment method to specify either the hardcoded text to display for the document fragment or for the path and file name of the file that contains the document fragment.

Use the InvokeControl method to specify the text to insert into the document when a user clicks on the document fragment in the Document Actions task pane. Again, you either hardcode the text into the code or provide the path and file name of the file that contains the document fragment.

Understanding the code

When you use a document fragment from an external file, the Document Actions task pane displays the first page of the document fragment. However, for longer document fragments, you may want to shorten the text that is displayed in the task pane. For example, you might have several paragraphs for the introductory section of a will, and rather than display all the paragraphs in the task pane, you might want the document fragment in the task pane to read "Introduction for a will".

In this case, you can specify the string to display in the PopulateDocumentFragment method and the path and file name in the InvokeControl method. However, it's important to note that if you choose to do this, you must open the external file by using the Word Visual Basic for Applications (VBA) object model in the InvokeControl method and insert the fragment manually. In this case, Word will not automatically insert the contents of the external document.

For code examples on how to work with document fragments, see the "Document Fragments" topic in one of the following tutorials.