Import XML into Word Document

L

LundK

Hello,

today I read an Article on MSDN on how to create Word templates that use XML
schemas. The Articel is called "Creating and Applying an XML Resume Template
in Microsoft Office Word 2003"
(http://msdn.microsoft.com/office/un...us/odc_wd2003_ta/html/odc_wdxmlresumetemp.asp)
The features described are very nice, at least the option to export the data
as XML. Now I am searching for a way to import this data into my template. Is
there a way to do this? I understand that the saved XML can be used in other
applications but I would like to use Word to show XML data from my .NET
application. This would be a very nice way for reports or maybe mailmerges.

Thanks
jan
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?THVuZEs=?=,
today I read an Article on MSDN on how to create Word templates that use XML
schemas. The Articel is called "Creating and Applying an XML Resume Template
in Microsoft Office Word 2003"
(http://msdn.microsoft.com/office/understanding/xmloffice/codesamples/default.asp
x?pull=/library/en-us/odc_wd2003_ta/html/odc_wdxmlresumetemp.asp)
The features described are very nice, at least the option to export the data
as XML. Now I am searching for a way to import this data into my template. Is
there a way to do this? I understand that the saved XML can be used in other
applications but I would like to use Word to show XML data from my .NET
application. This would be a very nice way for reports or maybe mailmerges.
You have, in Word 2003 without VSTO 2005, basically three possibilities:

1. Use a transform when opening the document that "merges" data into the Word XML
document.

2. Use the basic XML tools the .NET Framework offers to parse the (closed) Word
XML and add the data, then open the result

3. Automate the Word object model, using the InsertXML method to place the data
(in valid WordProcessingML format) into the opened document.

Current versions of Word provide for no "mail merge"-type ability to import XML
data. You might want to take a look at VSTO 2005 and see if the "data islands"
are closer to what you're looking for. Word 2007 will also have some new features
that will probably interest you, so when you can, sign up for Beta 2.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
J

Jan - LundK

Cindy M -WordMVP- said:
Hi =?Utf-8?B?THVuZEs=?=,

You have, in Word 2003 without VSTO 2005, basically three possibilities:

1. Use a transform when opening the document that "merges" data into the Word XML
document.

2. Use the basic XML tools the .NET Framework offers to parse the (closed) Word
XML and add the data, then open the result

3. Automate the Word object model, using the InsertXML method to place the data
(in valid WordProcessingML format) into the opened document.

Current versions of Word provide for no "mail merge"-type ability to import XML
data. You might want to take a look at VSTO 2005 and see if the "data islands"
are closer to what you're looking for. Word 2007 will also have some new features
that will probably interest you, so when you can, sign up for Beta 2.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)


This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)

Hi Cindy,

1. I'd like to keep the document editable by the user. I think that doesn't
work when I use a transform. (correct me if I am wrong)

2. Parsing the document seems to be a bit complicated, because I don't
really know where the data is stored exactly.
I tried to save the document from the article as XML-data and as XML-Document.

This is one tag from the XML-data file:

<Description>South Ridge, SC</Description>

This is the Word-document (hope the formatting is readable):

<ns0:Description>
<w:tc>
<w:tcPr>
<w:tcW w:w="3761" w:type="pct" />
<w:gridSpan w:val="3" />
</w:tcPr>
<w:p>
<w:pPr>
<w:pStyle w:val="Achievement" />
<w:listPr>
<w:ilvl w:val="0" />
<w:ilfo w:val="11" />
<wx:t wx:val="n" wx:wTabBefore="0" wx:wTabAfter="150" />
<wx:font wx:val="Wingdings" />
</w:listPr>
</w:pPr>
<st1:place w:st="on">
<st1:City w:st="on">
<w:r>
<w:t>South Ridge</w:t>
</w:r>
</st1:City>
<w:r>
<w:t>, </w:t>
</w:r>
<st1:State w:st="on">
<w:r>
<w:t>SC</w:t>
</w:r>
</st1:State>
</st1:place>
</w:p>
</w:tc>
</ns0:Description>

So parsing the XML would be complicated because Word splitted the source XML
into multiple <w:t> elements.

3. Word object model sounds good, I actually found a "XMLNodes"-Collection
in the document. I'll take a look at it when I have more time.

I started to play around with VSTO and it seems to be very nice, too. I hope
I'll find the time to get more into it soon.

Thanks again for your help

jan
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?SmFuIC0gTHVuZEs=?=,
1. I'd like to keep the document editable by the user. I think that doesn't
work when I use a transform. (correct me if I am wrong)
You're wrong :) The transform must, however, be applied before the user does
any editing. Once the user does anything to the document, Word considers it
to be a Word document (that might not be well-formed or valid), and no
transform can be applied (except via the object model, in memory). It is
possible, though to link the document's schema with a transform (in Word-XML
parlance, a "solution") so that it runs automatically when the document is
opened.
2. Parsing the document seems to be a bit complicated, because I don't
really know where the data is stored exactly.
I tried to save the document from the article as XML-data and as XML-Document.

This is one tag from the XML-data file:

<Description>South Ridge, SC</Description>
<snip>

Hmmm. Assuming that the data should always go into the same "target spaces",
you should be able to resolve this by using a SCHEMA that reflects your XML
data. Import the schema into Word's Schema Library (Tools/Templates and
Add-ins/XML Schema). Open the document you want to use as a "template"
(insert the data). Use the XML task pane to insert the data fields (XML
tags).

Note that XML in Word is very namespace-oriented/sensitive. All the elements
Word puts in an XML document are associated with a namespace. Your schema
must have a namespace. All the XML tags you insert will be in this namespace.
So parsing your data into the XML file isn't really that hard. Just pick up
the XML tags in your namespace and match them to your data.
3. Word object model sounds good, I actually found a "XMLNodes"-Collection
in the document. I'll take a look at it when I have more time.
XMLNodes would mean you have a schema and XML tags (as described in my answer
to 2).

If you want to use any other approach (such as dumping data into bookmarks),
then you'd need the InsertXML method for the target Range. Note that the XML
you insert using this method must be in valid WordProcessingML.

I highly recommend Office 2003 XML from O'Reilly press if you start feeling
overwhelmed or bogged down.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top