InsertXml - not getting the same result as opening xml file

H

hayrob

I have an Word Document stored in an xml file. When I open the document
using word, I get the result I expect.
When I start Word, add a new blank document, use InsertXML to insert the Xml
in my Word file, the Word document does not look the same.
Is there a trick I am missing here?
 
P

Peter Huang [MSFT]

Hi

Here is a link for your reference.
Private Sub InsertRawXML()
Dim sXML As String
sXML = "<Customer xmlns='testNameSpace'>" & _
"<Name>Fred</Name><Address state='CA'>Palm Springs</Address>" &
_
"</Customer>"
Selection.InsertXML (sXML)
End Sub
Get to know .InsertXML
http://blogs.officezealot.com/chris/archive/2003/11/06/1253.aspx

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Klaus Linke

hayrob said:
I have an Word Document stored in an xml file. When I open the
document using word, I get the result I expect.
When I start Word, add a new blank document, use InsertXML to insert
the Xml in my Word file, the Word document does not look the same.
Is there a trick I am missing here?



Hi,

If you open the WordML file, it'll be formatted with the styles from that file.

If you insert it in some Word document, it'll be formatted with the styles of that document.

Same for page layout, headers and footers, option settings that might affect the layout, ...

Regards,
Klaus
 
H

hayrob

Klaus

I see what you are getting at, I think. I have created a new document using
a template which contains the headers and footers, section structure etc
that I use, then inserted the XML and I get the result I expected.

I'm left puzzled though. I thought if you supplied the complete document
content as you do with InsertXml, then all the information needed for Word
to render the document correctly was in the Word XML. Is it the case that
the previous settings ( layout, headers etc) are still applied even though
they are not referenced in the WordXml. When I save the Document which is
not being rendered correctly, the XML looks OK, but when I reopen the
document it is still not rendered correctly!. Or is there a way to force
Word to "start again" with the document with the new Xml?


hayrob said:
I have an Word Document stored in an xml file. When I open the
document using word, I get the result I expect.
When I start Word, add a new blank document, use InsertXML to insert
the Xml in my Word file, the Word document does not look the same.
Is there a trick I am missing here?



Hi,

If you open the WordML file, it'll be formatted with the styles from that
file.

If you insert it in some Word document, it'll be formatted with the styles
of that document.

Same for page layout, headers and footers, option settings that might affect
the layout, ...

Regards,
Klaus
 
H

hayrob

Thanks Peter.

My problem is with the case where I insert a complete document WordXml into
a new document. The formatting - particularly the headers are not rendered
correctly. When I open a document consisting of exactly the same WordXml,
the headers etc are shown correctly!
 
P

Peter Huang [MSFT]

Hi Hayrob,

If you wants to insert xml of a whole document, then why not directly save
the document as a new document of xml format.
Also I think because you call the InsertXML in certain range,(e.g. the
selection), then the content should be of the body of the document.

If you still have any concern, please feel free to post here.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

hayrob

Peter

Yes - that is what I have been doing - saving the xml to an .xml file, and
then opening it in Word and I get the result I expect. However, I thought it
would be better to have single Word document open, and simply change its xml
using InsertXml. But it doesn't look as if that works - shame.
 
P

Peter Huang [MSFT]

Hi

Based on my understanding, the InsertXML is used to insert xml into the
exist document, some attribute of word is defined in advance, that is to
say when we new a document some attributes have been set.
The InsertXML is used to change the content of the document but not the
whole document attribute.
If you want to do that, I think you may try to use the word's Object Modal
to do that.
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Klaus Linke

Word's object model doesn't really allow for this. You can only have one one set of styles. And regarding page layout and headers/footers, you have to be very careful about section breaks, and may need to insert one before you insert the XML file:
http://www.word.mvps.org/faqs/formatting/WorkWithSections.htm

I've seen it requested that there should be a kind of "binder" mechanism that just lets you put together different kinds of documents (based on different templates...), just putting one in the middle of another, keeping their original look. Maybe you could make a suggestion to Microsoft along these lines.

It just isn't something really feasible in Word right now. You can get close by renaming all the (built-in?) styles in the XML file you insert, and carefully managing the sections of the original and inserted doc, but it's hard work.

Regards,
Klaus
 
H

hayrob

Thanks Klaus

You are right about need to be careful with Sections when formatting
Headers!

I will make a suggestion to MS along the lines you have suggested.

Thanks again for your help and interest.


Word's object model doesn't really allow for this. You can only have one one
set of styles. And regarding page layout and headers/footers, you have to be
very careful about section breaks, and may need to insert one before you
insert the XML file:
http://www.word.mvps.org/faqs/formatting/WorkWithSections.htm

I've seen it requested that there should be a kind of "binder" mechanism
that just lets you put together different kinds of documents (based on
different templates...), just putting one in the middle of another, keeping
their original look. Maybe you could make a suggestion to Microsoft along
these lines.

It just isn't something really feasible in Word right now. You can get close
by renaming all the (built-in?) styles in the XML file you insert, and
carefully managing the sections of the original and inserted doc, but it's
hard work.

Regards,
Klaus
 
Y

Yan-Hong Huang[MSFT]

Hi Hayrob,

I am reviewing the issue thread. How is everything going? If you have any
more concerns on this issue, please feel free to post here and we will
follow up.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

hayrob

Thanks for the responses I have received.
I now understand that it is not possible to achieve what I wanted using
InsertXml, and I have reverted to the way that I was achieving the effect I
wanted - ie save the XML document, then open it in Word. Would have been
nice simply to have one IO operation but it works!

Thanks again for the responses.
 
P

Peter Huang [MSFT]

Hi

You are welcome!
If you still have any concern, please feel free to post here.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Y

Yan-Hong Huang[MSFT]

It is our pleasure.

If there is any more questions on office programming, please feel free to
discuss it in the community. :)

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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