I've gotten into the habit of always applying the elements to the selection, as that seems to be what I'd want in most situations anyway. Continue to highlight text and apply the elements as desired. After making your selections and applying the schema, you may or may not see much of a difference in your document.
With this option selected, you'll see the start and end tags graphically represented in your Word document, as shown in Figure 1. Now that you've applied the schema to your document, save it as an XML file so that you can parse it with your application code. To do this, start by choosing the Save As… option from the File menu.
None of the checkboxes should be selected for this example, as you do not want to apply a transform or save only the data without the tags.
This ensures that all of the information you have entered into your document is written out to XML. To make things a little cleaner in the XML output, you will want to ensure that you either spell everything correctly not very likely if you use my naming convention for the placeholder text or that you ignore any spelling errors flagged by Word If you leave in something that the Word spelling checker doesn't like, the resultant XML looks similar to the following snippet:.
As you can see, with the proofing errors, this changes the expected XML, because Word has embedded some proofErr elements. Once you handle the spelling errors e. Also, be aware of where your paragraph marks appear in relation to your applied schema elements. In the snippet shown above, the [ContactName] text appears on a line all by itself. This places a paragraph element the w:p element completely within the ContactName element. If, on the other hand, you placed ContactName on the same line as some other text or another element, the paragraph element won't appear within the ContactName element but outside of it.
Because my document contains both of these examples, the code will have to handle both situations appropriately. When you double-click it, it opens up within Word , not in your associated program for XML files which is, by default, Microsoft Internet Explorer. If you comment out the second line of this document and then save it, you no longer launch Word when double-clicking the XML file.
Now that the template has been defined and annotated as desired, you can write a small program to read data from an XML file and merge this data with the template. NET as the language. To make the example easier, I've saved this as a file called NWData. In the real world, I'd probably capture the desired data in a Web page or Windows application and then retrieve the data from a database instead of a disk file.
That means I'll have to be certain to skip these elements when processing the file; perhaps they'll be added to other document templates in the future.
NET Framework to do the bulk of the work. The Word document saved as XML is loaded through a method of a class instantiated as the oProcess object. Next, select the nodes from the data document with a simple XPath query, and iterate through them with a For-Next loop. Note that this code only assumes that a single customer record exists in the XML file.
If there are multiple customers, add another outer loop to iterate through each customer record. For the ProcessNodes method, the desired node name and new text are passed as parameters. A separate method is used because in my template, I have the ContactName element in two locations within the document. I want to ensure that both of these locations are replaced with the same name.
So, in the ProcessNodes method, the specified node name is used to create XPath queries to retrieve lists of matching nodes. The interesting part of the code is the XPath queries; there are two of them, to ensure that you catch all of the nodes with the specified node name. Because some of the nodes are within a single paragraph and others are embedded within a paragraph, there are queries to account for both situations.
The namespace prefix requires that the SelectNodes method specifies a NamespaceManager object, which is part of. NET's System. XML namespace. Otherwise, your SelectNodes query will fail with errors. The namespace URIs come directly from the Word XML file and may vary depending upon the target namespace declared in your schema and what Word assigns as a prefix to your schema.
The FillNodes method referenced in the ProcessNodes method receives a node list object and a new node value as parameters. The last bit is to take the modified XML and save it to disk with a different file name so that it can be viewed.
After running the program, you should now be able to double-click the output file and see the output in Word , as shown in Figure 3. If you double-click the output XML file and it doesn't load in Word , most likely you followed my earlier advice about commenting out the processing instruction in your template file so that you could view the XML in your registered XML application.
Simply remove the comment so that the processing instruction becomes active again, allowing the document to open directly in Word Another trick for ensuring that the document opens in Word is to force a DOC extension on the final output of the program.
For example, to force the OutFile. XML file to open in Word , rename the file as Outfile. With a little processing, the Word XML file is easily merged with XML data and can act as a template for a multitude of documents. My Subscriber Account Advertise Write. Training Home State of. Staffing Home Looking for Staff? Looking for Work? Contact Us. Dark Kimbie. Published in:. If you're not sure why you might try something like this, here are a few ideas: You can create documents from data within an application, such as form letters.
You can send Word documents to a client workstation over the Internet as XML and have it correctly interpreted at the client workstation as a Word document. Contains toolbar customizations, envelope data, and the Visual Basic project. Indicates that macros are present in the document. If the macrosPresent attribute is missing or if its set to no, Word wont load a document that has a docSuppData element. This attribute is strictly enforced. Indicates that an OCX control may have been used in the document.
If the embeddedObjPresent attribute is missing or if its set to no, Word wont load a document that has a docOleData element. This attribute is not, however, strictly enforced.
If the attribute is present and is set to yes, but Word doesnt find a docOleData element before the body element, Word will still load the document. If this file contains any OCX tags and this attribute is not present and set to yes, the file is corrupt.
0コメント