Word to XML Creation

A

Anil

I have a word document which applied using xml schema.

For example:

<Root><Text>Sample data<><> like this.

Now i want to conver this document in to xml like below.

<Root><Text>Sample data<?Text></Root>


If any attribues i have defined in the xml schema i need to include that
also in the xml file. Please suggest me how can i do that?

Thanks
Anil
 
P

Peter Jamieson

In the user interface,
a. In Word 2003, you Save As, then choose the .xml format.
b. In Word 2007, you have to choose Save As->Other formats...->Word
2003 XML Document

Then make sure that the Save Data Only box is checked (I forget if it
has the same name in Word 2003). If you choose the wrong format in Word
2007 ("Word XML Document") you do not see this option, which suggests
that Microsoft do not think that many people actually make use of this
feature.

In VBA, you can probably do it using something like this:

Sub SaveAsXMLData()
ActiveDocument.XMLSaveDataOnly = True
ActiveDocument.SaveAs _
FileName:="mydoc.xml", _
FileFormat:=wdFormatXML
End Sub
If any attribues i have defined in the xml schema i need to include that
also in the xml file. Please suggest me how can i do that?

If you have applied attributes in the document, they should be saved in
the XML. If you are not sure how to apply attributes, do this:
a. right-click on the element tag that the attribute belongs to - if
the Element is "order", right click on the Order tag.
b. click Attributes...
c. select the attribute you want to add, e.g. "status"
d. type a value in the "Type value" box
e. make sure you click the "add" button (it is easily missed!)


Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv
 
A

Anil

Hi Peter,

Thanks for the reply and works for me.

I need some more regarding this schema process. if i want to find some text
and apply some schema automatically how can i do that?

For example i have a word document like

head1
para

I need to apply "doc" first to complete and then i need to select head1 and
apply "h1" and for para "p" like that.

Can you please suggest me.

Thanks
Anil
 
P

Peter Jamieson

I don't know this stuff in detail either.

But very briefly,
a. the Word macro recorder is your friend. If you switch it on, select
some text and apply an element, you will see that it records something like

Activedocument.XMLNodes.Add "elementname", "schemaname"

b. typically any kind of automated operation along the lines you
mention requires that you
- look up the help/documentation for any objects you are unfamiliar
with - e.g. the XMLNodes collection)
- experiment to discover whether you can Add objects by applying them
to a Range object, or if that is not possible, the Selection object
- work out how you are going to specify those Range objects, or make
the necessary Selections. If you can Add an object to a Range, you will
typically be able to do it by using Selection.Range as well.
- experiment to discover other practical difficulties. e.g. if you
nest a second element, the recorder does not record the schemaname and
in fact providing it causes an error (which does not appear either to be
what the documentation claims or a particularly sensible way to do
things, but there you go)

So in the example you mention, you start with something like

Sub ApplyElements()
' Assume your schema is called "myschema"
' "Content" is a range that specifies
' the entire content of the document body
ActiveDocument.Content.XMLNodes.Add "doc", "myschema"
ActiveDocument.Paragraphs(1).Range.XMLNodes.Add "h1", ""
ActiveDocument.Paragraphs(2).Range.XMLNodes.Add "p", ""
End Sub

A simple refinement that may help clarity is

Sub ApplyElements()
' Assume your schema is called "myschema"
' "Content" is a range that specifies
' the entire content of the document body
With ActiveDocument
.Content.XMLNodes.Add "doc", "myschema"
.Paragraphs(1).Range.XMLNodes.Add "h1", ""
.Paragraphs(2).Range.XMLNodes.Add "p", ""
Wend
End Sub

Beyond that, it's up to you to work how to do what you /really/ want :)

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv
 

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