Nesting IncludeTemplate or Import template inside an IF

Mar 9, 2010 at 9:06 PM

Basically, the title says it all<g>

When I do that, teh openxml validate barks at me, claiming an incorrect child element, and work flat won't load the file.

The fix (for me anyway) was several parts

First, there's a special case check in PreProcessTag_ImportTemplate, at the top (sorry it's in, I converted it)

If parentIsParagraph Then
                Debug.WriteLine("FlexDocTemplateProcessor:PreProcessTag_ImportTemplate: Warning: ImportTemplate cannot be contained within a paragraph and has therefore been moved right after the paragraph")
                Dim newElement As OpenXmlCompositeElement = DirectCast(element.CloneNode(True), OpenXmlCompositeElement)
                element.Parent.InsertAfterSelf(Of OpenXmlCompositeElement)(newElement)
                element = newElement
End If


If I leave that in, things get really mangled if you nest tags. However, taking it out didn't seem to make any difference for nested or non-nested import commands. they seem to work in both cases.


At the end of the routine (and the PreProcessTag_IncludeTemplate routine as well) I had to change it to embed the imported text or altchunk into a new RUN:

Dim run = New Run
run.AppendChild(New RunProperties)
For Each templateChild As OpenXmlElement In docTemplate.MainDocumentPart.Document.Body.ChildElements
    'element.InsertAfterSelf(templateChild.CloneNode(True))         <------ the old line


Doing this appears to allow the imported markup to coexist INSIDE the paragraph that contains the outer IF tag. But it doesn't seem to have any ill effects on IMPORT or INCLUDE template commands that AREN'T nested.


The problem is, now, the validator is throwing errors about improper child elements (it doesnt' appear to like a RUN being embedded within another RUN). BUT.... Word still loads the document just fine.

I'm tempted to just continue on and disregard the errors.


In any event, are IMPORT or INCLUDE tags supposed to work when nested within an IF tag (basically conditionally include the template)? It's completely possible that I've fouled things up in my conversion to VB, but everything else appears to work the bees knees.



And regardless, this is a +very cool+ project!