Search code examples
xmlvbaxsltms-wordmsxml

VBA XML transformation ignores included Stylesheets?


I am trying to transform an XML file into a Microsoft Word document, using VBA. However when I try to run the macro, I get an error stating: Error '-2147467259 (80004005)': The template 'example' doesn't exist in the stylesheet.

My transformation looks like this:

Public Function Transform(strXML As String, strXSLT As String, strOutput As String, _
         Optional strError As String) As Long

     Dim objXML As MSXML2.DOMDocument60
     Dim objXSLT As MSXML2.DOMDocument60
     Dim objOutput As MSXML2.DOMDocument60

     Set objXML = New MSXML2.DOMDocument60
     objXML.Load strXML
     If objXML.parseError = 0 Then
         Set objXSLT = New MSXML2.DOMDocument60
         objXSLT.Load strXSLT
         If objXSLT.parseError = 0 Then
             Set objOutput = New MSXML2.DOMDocument60
             objXML.transformNodeToObject objXSLT, objOutput
             objOutput.Save strOutput
         Else
             Transform = objXSLT.parseError.errorCode
             strError = ".xslt-file: " & vbCrLf & strXSLT & vbCrLf & objXSLT.parseError.reason
         End If
     Else
         Transform = objXML.parseError.errorCode
         strError = ".xml-file: " & vbCrLf & strXML & vbCrLf & objXML.parseError.reason
     End If

End Function

My XML-file is just fine. The XSL-file however contains an include at the top like this:

<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Word.Document"?>
 
<xslt:stylesheet version="1.0"
    xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
    xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
    xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2"
    xmlns:r="http://schemas.openxmlformats.org/package/2006/relationships">
    
    <xslt:output method="xml" encoding="UTF-8" indent="yes"/>
    <xslt:strip-space elements="*"/>
    
    <xslt:include href="layout.xsl"/>
    <xslt:include href="calculations.xsl"/>
    <xslt:include href="functions.xsl"/>
    <xslt:include href="styles.xsl"/>

It seems to me the VBA doesn't load those included files. Is there a way to make this possible?


Solution

  • You need, for MSXML 6, to explicitly set a property objXSLT.resolveExternals = True before loading the XSLT code.

    See https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762283(v=vs.85)#script-syntax saying "In MSXML 6.0, the default setting is False" and "If this property is set to False, no external includes and imports will be resolved".