Problem inserting WordML with Range.InsertXML

F

Fourge

Hi
I have a strange problem.
Background:
I am developing a solution using Word, WordML, SmartDocuments, C#, VS.NET
and .NET Framework 1.1

The scenario is:
I have a Word document saved as XML (let's call it docA.xml). I am using
SmartDocument technology to automate this document. At different points in
time, I want to insert an XML Node that contains a combination of WordML
Element as well as my own custom Elements (lets call this nodeB) - nodeB is
well formed, valid and all namespaces are clearly declared.

Approach 1:
I have approached this firstly by opening docA.xml in an XML editor, opening
nodeB in the same editor and then pasting nodeB at the desired place in
docA.xml. I then save docA.xml and open it in Word and the document is
perfect - ie. it is formatted correctly, no extra markup is added...

Approach 2:
My second approach (which is the one I need to use in my final solution) is
using a CodeBehind class for the Word document that implements
ISmartDocument. With this approach, I obviously use the Word Object Model to
get access to the document and to add or remove content, XML etc. To achieve
my goal, I get the current Range object, set a string variable to the XML
text that constitutes nodeB. I then call Range.InsertXML and pass in nodeB
as a string (as per the documentation). The result is that nodeB is inserted
into the docA.xml, but the formatting (ie. WordML tags) have been tampered
with by Word and therefore the inserted nodeB is quite different to what it
was before insertion. Essentially, Word adds extra paragraph markup, it
indents the text and ignores things like bold styling.

Comment:
I have spent many long nights on this only to arrive at the current
conclusion that Word is doing something under the covers once I have called
the Range.InsertXML method. This is out of my control at the moment and I am
not sure where to go from here. One of the thoughts I am exploring is that
maybe there is another Property of the Range class that instructs Word not to
alter the inserted XML in any way.

Any help would be greatly appreciated.
 
P

Peter Huang [MSFT]

Hi

I think you may try to take a look at the link below.
Based on my test, the code below will insert the wordml into a
smartdocument together with its format tag.
Private Sub InsertWordML()
Dim sXML As String
sXML = "<wordDocument
xmlns='http://schemas.microsoft.com/office/word/2003/wordml'><body>" & _
"<p><r><t xml:space='preserve'>Example of WordML and
InsertXML run at </t></r>" & _
"<r><rPr><b/></rPr><t>" & Now() &
"</t></r></p><p/><p/></body></wordDocument>"
'Insert WordML
Selection.InsertXML (sXML) 'Selection is also an range.
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.
 

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