The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
The purpose of this Microsoft Word Tips & Microsoft Word Help page is is to offer a basic demonstration of how you can validate mapped Content Control (CC) data entries with an attached XML Schema. For additional information on mapped Content Controls see my: Repeating Data (or populating fields).
The discussion offered is basic because the field of XML and its validation methods are fairly broad and complex and I only have a basic understanding of the subject myself. Hopefully you can build on what I have presented here to meet your specific needs.
The illustration below shows a document with two content controls. The second control has been mapped to a CustomXMLPart and validated with an attached XML schema. Note the red dashed outline around the placeholder text. This indicates that the content does not meet the validation requirements of the attached schema and the condition is flagged for the user.
Unfortunately the validation that you can perform using an attached XML schema is a lot like Caspar Milquetoast (i.e., very timid). It will flag invalid data for the user, but there doesn't seem to be any way to require validation before a user can save, print, or otherwise complete the document.
You can create a document with a mapped content control validated with an attached schema by performing the following steps:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <ccMap xmlns="http://ValidationDemo"> <ccElement_SexualContact></ccElement_SexualContact> </ccMap>
Note: My XML script is saved as D:\CCValidationDemoXML.txt
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ValidationDemo" xmlns="http://ValidationDemo" elementFormDefault="qualified"> <xs:element name="ccMap"> <xs:complexType> <xs:sequence> <xs:element name="ccElement_SexualContact"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Yes"/> <xs:enumeration value="No"/> <xs:enumeration value="yes"/> <xs:enumeration value="no"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"></xs:any> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Notes:
1. The XSD file I used is saved on my PC as D:\CC ValidationDemoSchema.xsd
2. The following link will introduce you to a complete W3Schools tutorial on creating XSD files:
XSD Tutorial
Notes:
1. The following VBA procedures can be used to perform steps
5 and 6.
2. Users with Word 2013 or higher can load
and map the content control using the Developer>XML Mapping Pane tools.
Sub MapCC() Dim oCC As ContentControl Dim XPath As String LoadCXP Set oCC = ActiveDocument.SelectContentControlsByTitle("Sex?").Item(1) XPath = "/ns0:ccMap/ns0:ccElement_SexualContact" oCC.XMLMapping.SetMapping XPath lbl_Exit: Exit SUb End Sub Sub LoadCXP() Dim oCustXMLPart As CustomXMLPart ClearXMLParts 'Optional Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add 'Be sure to use your own file name. oCustXMLPart.Load "D:\CCValidationDemoXML.txt" lbl_Exit: Exit Sub End Sub Sub ClearXMLParts() Dim lngIndex As Long For lngIndex = ActiveDocument.CustomXMLParts.Count To 4 Step -1 ActiveDocument.CustomXMLParts(lngIndex).Delete Next lngIndex lbl_Exit: Exit Sub End Sub
See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.
- For Word 2007 and 2010 users, on the Developer tab, in the XML group, select "Schema."
-For Word 2013 and higher, on the Developer tab, in the Templates Group, Select "Document Template."
-In the "Templates and Add-Ins" dialog, XML Schema tag and select "Add Schema."
-In the Add Schema dialog, select your saved schema file and click "Open."
- In the Schema Setting dialog enter an alias (e.g., CC Validation Demo) and click "OK."
-Note that schema is attached and click "OK."
- Save, close and reopen the document.
Your content control is now mapped and validated to the attached schema.
I still have a lot to learn about working with XML and associated schema. I would appreciate any feedback on this Microsoft Word Help & Tips page. Feedback to make it better or to address areas I may have missed or just plain got wrong.
That finishes it! I hope you have found this Microsoft Word Help & Tips page useful and informative. You can download a demo package containing the files used to create this page here: CC Validate Schema Demo.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!