Importing and Exporting Text Fragments in Word 2010
Handy Programming Tips for Microsoft Word 2010: Learn how to export and import text fragments in Microsoft Word 2010.
Last modified: May 19, 2011
Applies to: Office 2010 | VBA | Word 2010
Published: June 2011
Provided by: Frank Rice, Microsoft Corporation
Microsoft Word 2010 allows you to export ranges of text as fragments. You can them import these fragments and use them in any document. In this topic, you programmatically export a fragment, modify its format, and then import the fragment. To complete this task, you must do the following:
In this task, you add programming code to the Visual Basic Editor.
To add code to the Visual Basic Editor
On the Developer tab, click Visual Basic. This opens the Visual Basic Editor.
If you do not see the Developer tab in Word 2010, click the File tab, and then click Options. In the categories pane, click Customize Ribbon, select Developer, and then click OK.
In the Project pane, click ThisDocument.
Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.
Sub RangeExportImportFragments() ' Word 2007 and later allows you to export ranges of text as a fragments. ' You can them reimport these fragments and use them in any document. ' Given the sample document with 5 paragraphs, try exporting a fragment, ' then modifying the format and reimporting the fragment to see the behavior. ' Change this path to match your own situation. Const FRAGMENT_FILE As String = "C:\Temp\Fragment.docx" Dim rng As Range Set rng = Range(15, 150) rng.Bold = True rng.ExportFragment FRAGMENT_FILE, wdFormatDocumentDefault ' Take a moment and load the fragment file into Microsoft Word. ' It should load like any other document. ' Set formatting back to normal. rng.Bold = False ' Although the second paramter indicates style behavior for the incoming ' fragment, it doesn't appear to make any different which option ' you choose. Both True and False give the same result. If False, the ' fragment should use the formatting from the original document, not the ' current document, but this doesn't appear to be the case. The ImportFragment ' method uses the formatting from the source document, no matter how this ' parameter has been set: Words(1).ImportFragment FRAGMENT_FILE, False Words(100).ImportFragment FRAGMENT_FILE, True End Sub
In this task, you add text to a document and then step through the VBA code that exports a range of text, manipulates its format, and then imports the text into the same document.
To step through the code
In the document, type the following command (without the quotes) to add five paragraphs of three sentences each.
Drag the Visual Basic Editor window to the right side of your monitor.
Drag the Word window to the left side of your monitor and adjust the windows until you can see them both.
Place the cursor in the RangeExportImportFragments module and press F8 to step through the code line-by-line and watch the code behavior.