XML Schemas for Content Controls in Outlook. Saved to NormalEmail.dotm file

188 Views Asked by At

So I recently learned that content controls in MS Word can be mapped to XML schemas that will update other content controls that are mapped to it.

I had this idea to save a Quick Part in the NormalEmail.dotm file with content controls that are mapped to an XML schema that I saved to NormalEmail.dotm.

My goal was for the repeating areas in the quick part to automatically update upon changing one of them as it would in MS Word.

I figured that in the same way I can save the XML schema to the global template in a Normal.dotm file and have it work in a quick part that it could in theory do the same when I save it to a NormalEmail.dotm file.

Unfortunately, when I begin drafting an email in MS Outlook and open the quick part it doesn't update the other parts of the text that are mapped to it.

Does anyone know if this actually can work or has tested it before? I initially worked around adding content controls to Outlook by copy and pasting them from MS Word or creating a Formatted AutoCorrect Entry since there is no option for content controls. Please Help!

2

There are 2 best solutions below

1
Bryce Chan On

You can map content controls in MS Word to an XML schema and update them automatically, but as I know it's not available in MS Outlook.

They are two separate programs with different capabilities.

There is a complicated work around though, you can create a custom form in MS Outlook of the repeating content you need, then use Visual Basic for Applications code to populate the form fields based on the data in your XML file.

3
jonsson On

This is a partial answer as I'm not certain it can be turned into a functioning solution. i.e. it replicates the content controls while you're composing the message, but I haven't looked in detail at what a recipient might see when they receive it. Although it looks OK so far.

As background, this is what I did.

I opened NormalEmail.dotm in Word (not a Document based on the .dotm), inserted the following VBA in a module in it, and ran it. It clears out all custom XML parts, inserts a new one, and inserts two plain text COntent COntrols mapped to the same element in the Part.

Sub setupcxpandccs()
Dim cc As Word.ContentControl
Dim cxp As Office.CustomXMLPart
Dim i As Long
Dim rng As Word.Range
Dim x As String
x = ""
x = x & "<?xml version='1.0' encoding='UTF-8'?>" & vbCrLf
x = x & "<root>" & vbCrLf
x = x & "<c1></c1>" & vbCrLf
x = x & "</root>" & vbCrLf

For i = ActiveDocument.CustomXMLParts.Count To 1 Step -1
  If Not ActiveDocument.CustomXMLParts(i).BuiltIn Then
    ActiveDocument.CustomXMLParts(i).Delete
  End If
Next
Set cxp = ActiveDocument.CustomXMLParts.Add(x)

ActiveDocument.Content.Text = ""
Set cc = ActiveDocument.ContentControls.Add(wdContentControlText)
cc.XMLMapping.SetMapping "/root[1]/c1[1]", , cxp
Set cc = Nothing
ActiveDocument.Content.InsertParagraphAfter
Set rng = ActiveDocument.Content
rng.Collapse WdCollapseDirection.wdCollapseEnd
Set cc = ActiveDocument.ContentControls.Add(wdContentControlText, rng)
cc.XMLMapping.SetMapping "/root[1]/c1[1]", , cxp
Set cc = Nothing
Set rng = Nothing
Set cxp = Nothing
End Sub

I verified that text typed into one part replicated into the other.

I selected one of the content controls and saved it to the .dotm as a Quickpart, then deleted the document body (i.e. the two controls and a paragraph mark), saved and closed everything.

I then created a new document in Word, based on NormalEmail.dotm, and verified that it had 4 Custom Xml Parts (there are 3 builtin parts).

I then closed Word, opened Outlook, created a new email, used Insert->QuickParts to verify that the quickpart was there, and inserted two copies. Text typed into one was not replicated in the other.

I then checked the number of Custom Xml Parts in the email document using the following Outlook VBA:

Public Sub checkCXPCount()
Dim oApp As Word.Application
Set oApp = Application.ActiveInspector.WordEditor.Application
' I don't know what the best way to get the Document 
' for the current email is - this will do for now
Debug.Print oApp.Documents(1).CustomXMLParts.Count
Set oApp = Nothing
End Sub

The count was 3, not 4.

I then ran the following Outlook VBA to insert a CXP with the same content as I had used to set up the template's CXP:

Sub replacecxp()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim cc As Word.ContentControl
Dim cxp As Office.CustomXMLPart
Set oApp = Application.ActiveInspector.WordEditor.Application
Set oDoc = oApp.Documents(1)
Dim i As Integer
For i = oDoc.CustomXMLParts.Count To 1 Step -1
  If Not oDoc.CustomXMLParts(i).BuiltIn Then
    oDoc.CustomXMLParts(i).Delete
  End If
Next
Set cxp = oDoc.CustomXMLParts.Add("<?xml version='1.0' encoding='UTF-8'?><root><c1></c1></root>")
Set cxp = Nothing
Set oDoc = Nothing
Set oApp = Nothing
End Sub

After that, the text replication functioned. However, at one point I could not repeat that and had to go back and step through the code. I wondered if I was getting interference from a copy of Word left in memory. If I had time to experiment further I would probably add a namespace name to the XML and test that that also works.

A couple of things I didn't get around to trying were

  • inspecting the XML content of the QUickPart in the .dotx. If there were mapping problems, that might reveal why
  • using VBA to get the Custom Xml Part from the template in order to copy it into the EMail Document myself rather than use hardcoded XML. Just couldn't work out how to reference it, but perhaps that's actually straightforward.