Does the VBA save word as XML???

Hello!

I'm really needing to know if there's any VBA code able to save a Word document as an XML doc.

Any sample is appreciate.
thanx

Marcos Hercules dos Santos



Answer this question

Does the VBA save word as XML???

  • James_Lin

    There an XML format called WordML that lets you save a native Word document to an XML document. The XML file thats produced contains a lot of XML that you might not want or need but you can perform transformations on it or use DOM to extract what you need or to reformat it.

    If you look at the Document.SaveAs method there is a enumeration for XML

    ActiveDocument.SaveAs FileName:="doc.xml", FileFormat:=wdFormatXML

    You'd need to look into WordML but it's a start.



  • Miche

    Got it working Marcos, check out my blog.

    http://dsmyth.blogspot.com/



  • Manu9456

    Here's some additional info from the support engineer:

    Hi,

    Thank you for contacting Microsoft ISV Partner Support.

    I think you need to specify if the XML type is to include WordML or just XML You will need to think through saving as XML, because there are a few options. There is WordML which allows for round-tripping of an XML file, and there is XML that requires a schema and the document to be valid against the schema, so that only the data is saved.

    I recorded doing a file save as, this may give you more information in which will help you better find the answer you are looking for.

    After that it is pretty easy to create a macro that will do the conversion. It will also be simpler to do in VBA since it only requires a few lines of code.

    //sample code

    ===

    Sub Macro1()

    '

    ' Macro1 Macro

    '

    With ActiveDocument

    .XMLSaveDataOnly = False

    .XMLUseXSLTWhenSaving = False

    .XMLSaveThroughXSLT = ""

    .XMLHideNamespaces = False

    .XMLShowAdvancedErrors = False

    .XMLSchemaReferences.UnderlineValidationErrors = True

    .XMLSchemaReferences.AutomaticValidation = True

    .XMLSchemaReferences.IgnoreMixedContent = False

    .XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = True

    .XMLSchemaReferences.ShowPlaceholderText = False

    End With

    ActiveDocument.SaveAs FileName:="This is a test.xml", FileFormat:= _

    wdFormatXML, LockComments:=False, Password:="",

    AddToRecentFiles:=True, _

    WritePassword:="", ReadOnlyRecommended:=False,

    EmbedTrueTypeFonts:=False, _

    SaveNativePictureFormat:=False, SaveFormsData:=False,

    SaveAsAOCELetter:= _

    False

    End Sub

    ===

    -brenda (ISV Buddy Team)



  • K8

    Can you please provide me with the actual error message

    -brenda (ISV Buddy Team)



  • lacus

    Hi Derek,

    I' got it. Very good, man.
    Actually I'm working on,(at least trying to) extracting words from ms-word documents, surely this only can be done
    using XML and a programming language like VBA.

    If you have access to some materiasl (article, samples,etc), please send to my e-mail
    mhercules@gmail.com

    I'll be looking out your Blog.

    Thanxs, v.m

    Marcos Hercules dos Santos


  • lmaster

    O.K, I'm gonna try this.

    Thanx for a while Derek




  • CEisen

    Marcos,

    I'm going to give it a try myself, its quite a handy thing to know.

    I'll post what I find on my blog and let you know.



  • Heath Stewart - MSFT

    Looking at it again, there are a few other methods you could try

    Search in the help for...

    ExportXML()

    and

    SaveAsXMLData()

    You'll see something called XmlMaps, I believe this is a Schema mapping that describes what you want to export.



  • Moody2006

    Hi Brenda,
    I'm trying to apply this macro you did, but in the line after "End With" it's been a sintax error.

    Would it be anything to include in references or troubles with the version I'm using Office2003.






  • Does the VBA save word as XML???