我有一个Microsoft Word 2010文档,我有一些用于用户交互的内容控件。我想从CustomXMLPart XML Document填充和DropdownListEntries。我添加了一个CustomXMLPart并将我的XML文档加载到CustomXMLPart但不确定如何??绑定? CustomXMLPart的内容控件。下面是我的代码和XML文档:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SitesTable>
<SiteRow>
<site hull="BXR">COMPHIBRON ONE</site>
</SiteRow>
<SiteRow>
<site hull="AOT5205">MT EVERGREEN STATE</site>
</SiteRow>
<SiteRow>
<site hull="AK4543">MV LTC JOHN U D PAGE</site>
</SiteRow>
<SiteRow>
<site hull="AK4544">MV SSG EDWARD A CARTER JR</site>
</SiteRow>
<SiteRow>
<site hull="AK323">MV TSGT JOHN A CHAPMAN</site>
</SiteRow>
<SiteRow>
<site hull="AK5065">SS CAPE MOHICAN</site>
</SiteRow>
<SiteRow>
<site hull="AVB4">SS CURTISS</site>
</SiteRow>
<SiteRow>
<site hull="AK 3015">USNS 1ST HARRY L MARTIN</site>
</SiteRow>
<SiteRow>
<site hull="AK 3010">USNS 1ST LT BALDOMERO LOPEZ</site>
</SiteRow>
<SiteRow>
<site hull="AK 3008">USNS 2ND LT JOHN P BOBO</site>
</SiteRow>
<SiteRow>
<site hull="AGOS 20">USNS ABLE</site>
</SiteRow>
<SiteRow>
<site hull="CVN 73">USS GEORGE WASHINGTON</site>
</SiteRow>
<SiteRow>
<site hull="SSGN 729">USS GEORGIA</site>
</SiteRow>
<SiteRow>
<site hull="CVN 78">USS GERALD R FORD</site>
</SiteRow>
<SiteRow>
<site hull="LSD 42">USS GERMANTOWN</site>
</SiteRow>
<SiteRow>
<site hull="CG 64">USS GETTYSBURG</site>
</SiteRow>
<SiteRow>
<site hull="MCM 11">USS GLADIATOR</site>
</SiteRow>
<SiteRow>
<site hull="DDG 66">USS GONZALEZ</site>
</SiteRow>
<SiteRow>
<site hull="DDG 107">USS GRAVELY</site>
</SiteRow>
<SiteRow>
<site hull="LPD 20">USS GREEN BAY</site>
</SiteRow>
<SiteRow>
<site hull="SSN 772">USS GREENEVILLE</site>
</SiteRow>
<SiteRow>
<site hull="DDG 101">USS GRIDLEY</site>
</SiteRow>
<SiteRow>
<site hull="LSD 44">USS GUNSTON HALL</site>
</SiteRow>
<SiteRow>
<site hull="DDG 97">USS HALSEY</site>
</SiteRow>
<SiteRow>
<site hull="SSN 767">USS HAMPTON</site>
</SiteRow>
<SiteRow>
<site hull="LSD 49">USS HARPERS FERRY</site>
</SiteRow>
<SiteRow>
<site hull="CVN 75">USS HARRY S TRUMAN</site>
</SiteRow>
<SiteRow>
<site hull="SSN 768">USS HARTFORD</site>
</SiteRow>
<SiteRow>
<site hull="SSN 776">USS HAWAII</site>
</SiteRow>
<SiteRow>
<site hull="SSN 725">USS HELENA</site>
</SiteRow>
<SiteRow>
<site hull="SSBN 730">USS HENRY M JACKSON</site>
</SiteRow>
<SiteRow>
<site hull="DDG 76">USS HIGGINS</site>
</SiteRow>
<SiteRow>
<site hull="DDG 70">USS HOPPER</site>
</SiteRow>
<SiteRow>
<site hull="SSN 713">USS HOUSTON</site>
</SiteRow>
<SiteRow>
<site hull="DDG 83">USS HOWARD</site>
</SiteRow>
<SiteRow>
<site hull="CG 66">USS HUE CITY</site>
</SiteRow>
<SiteRow>
<site hull="PC 3">USS HURRICANE</site>
</SiteRow>
<SiteRow>
<site hull="SSN 786">USS ILLINOIS</site>
</SiteRow>
<SiteRow>
<site hull="LCS 2">USS INDEPENDENCE</site>
</SiteRow>
<SiteRow>
<site hull="SSN 789">USS INDIANA</site>
</SiteRow>
<SiteRow>
<site hull="LHD 7">USS IWO JIMA</site>
</SiteRow>
<SiteRow>
<site hull="LCS 6">USS JACKSON</site>
</SiteRow>
<SiteRow>
<site hull="SSN 699">USS JACKSONVILLE</site>
</SiteRow>
<SiteRow>
<site hull="DDG 95">USS JAMES E WILLIAMS</site>
</SiteRow>
<SiteRow>
<site hull="DDG 109">USS JASON DUNHAM</site>
</SiteRow>
<SiteRow>
<site hull="SSN 759">USS JEFFERSON CITY</site>
</SiteRow>
<SiteRow>
<site hull="SSN 23">USS JIMMY CARTER</site>
</SiteRow>
<SiteRow>
<site hull="CVN 74">USS JOHN C STENNIS</site>
</SiteRow>
<SiteRow>
<site hull="DDG 53">USS JOHN PAUL JONES</site>
</SiteRow>
<SiteRow>
<site hull="DDG 56">USS JOHN S MCCAIN</site>
</SiteRow>
<SiteRow>
<site hull="SSN 785">USS JOHN WARNER</site>
</SiteRow>
<SiteRow>
<site hull="LHD 3">USS KEARSARGE</site>
</SiteRow>
<SiteRow>
<site hull="SSBN 737">USS KENTUCKY</site>
</SiteRow>
<SiteRow>
<site hull="DDG 100">USS KIDD</site>
</SiteRow>
<SiteRow>
<site hull="SSN 701">USS LA JOLLA</site>
</SiteRow>
<SiteRow>
<site hull="DDG 58">USS LABOON</site>
</SiteRow>
<SiteRow>
<site hull="CG 57">USS LAKE CHAMPLAIN</site>
</SiteRow>
<SiteRow>
<site hull="CG 70">USS LAKE ERIE</site>
</SiteRow>
<SiteRow>
<site hull="DDG 82">USS LASSEN</site>
</SiteRow>
<SiteRow>
<site hull="CG 55">USS LEYTE GULF</site>
</SiteRow>
<SiteRow>
<site hull="SSBN 743">USS LOUISIANA</site>
</SiteRow>
<SiteRow>
<site hull="CG 59">USS PRINCETON</site>
</SiteRow>
<SiteRow>
<site hull="SSN 719">USS PROVIDENCE</site>
</SiteRow>
<SiteRow>
<site hull="DDG 61">USS RAMAGE</site>
</SiteRow>
<SiteRow>
<site hull="SSBN 740">USS RHODE ISLAND</site>
</SiteRow>
<SiteRow>
<site hull="CVN 76">USS RONALD REAGAN</site>
</SiteRow>
<SiteRow>
<site hull="DDG 80">USS ROOSEVELT</site>
</SiteRow>
<SiteRow>
<site hull="DDG 71">USS ROSS</site>
</SiteRow>
<SiteRow>
<site hull="LSD 47">USS RUSHMORE</site>
</SiteRow>
<SiteRow>
<site hull="DDG 59">USS RUSSELL</site>
</SiteRow>
<SiteRow>
<site hull="DDG 102">USS SAMPSON</site>
</SiteRow>
<SiteRow>
<site hull="LPD 17">USS SAN ANTONIO</site>
</SiteRow>
<SiteRow>
<site hull="LPD 22">USS SAN DIEGO</site>
</SiteRow>
<SiteRow>
<site hull="SSN 711">USS SAN FRANCISCO</site>
</SiteRow>
<SiteRow>
<site hull="CG 56">USS SAN JACINTO</site>
</SiteRow>
<SiteRow>
<site hull="SSN 751">USS SAN JUAN</site>
</SiteRow>
<SiteRow>
<site hull="SSN 763">USS SANTA FE</site>
</SiteRow>
<SiteRow>
<site hull="MCM 8">USS SCOUT</site>
</SiteRow>
<SiteRow>
<site hull="SSN 756">USS SCRANTON</site>
</SiteRow>
<SiteRow>
<site hull="SSN 21">USS SEAWOLF</site>
</SiteRow>
<SiteRow>
<site hull="MCM 3">USS SENTRY</site>
</SiteRow>
<SiteRow>
<site hull="CG 67">USS SHILOH</site>
</SiteRow>
<SiteRow>
<site hull="DDG 86">USS SHOUP</site>
</SiteRow>
<SiteRow>
<site hull="LPD 25">USS SOMERSET</site>
</SiteRow>
<SiteRow>
<site hull="SSN 790">USS SOUTH DAKOTA</site>
</SiteRow>
<SiteRow>
<site hull="SSN 761">USS SPRINGFIELD</site>
</SiteRow>
<SiteRow>
<site hull="DDG 111">USS SPRUANCE</site>
</SiteRow>
<SiteRow>
<site hull="DDG 104">USS STERETT</site>
</SiteRow>
<SiteRow>
<site hull="DDG 63">USS STETHEM</site>
</SiteRow>
<SiteRow>
<site hull="DDG 106">USS STOCKDALE</site>
</SiteRow>
<SiteRow>
<site hull="DDG 55">USS STOUT</site>
</SiteRow>
<SiteRow>
<site hull="SSBN 734">USS TENNESSEE</site>
</SiteRow>
<SiteRow>
<site hull="SSN 775">USS TEXAS</site>
</SiteRow>
<SiteRow>
<site hull="SSBN 736">USS WEST VIRGINIA</site>
</SiteRow>
<SiteRow>
<site hull="LSD 41">USS WHIDBEY ISLAND</site>
</SiteRow>
<SiteRow>
<site hull="DDG 110">USS WILLIAM P LAWRENCE</site>
</SiteRow>
<SiteRow>
<site hull="DDG 81">USS WINSTON S CHURCHILL</site>
</SiteRow>
<SiteRow>
<site hull="SSBN 742">USS WYOMING</site>
</SiteRow>
</SitesTable>
以下是写入ThisDocument的代码:
Sub ReadCustomXML()
On Error GoTo ErrorReadCustomXMLInfo
'Declares variables
Dim XMLNameSpace As String
Dim XMLNameSpaceCustom As String
Dim XMLNameSpacePrefix As String
Dim XMLParts As CustomXMLParts
Dim XMLPart As CustomXMLPart
Dim XMLSiteNodes As CustomXMLNodes
Dim XMLSiteNode As CustomXMLNode
Dim XMLHullNodes As CustomXMLNodes
Dim XMLHullNode As CustomXMLNode
Dim i As Integer
Dim strXPath As String
Dim ListItemEntry As String
Dim objXMLMapping As XMLMapping
'Set the Documents-Level CustomXMLParts
Set XMLParts = ThisDocument.CustomXMLParts
Debug.Print XMLParts.Count
If XMLParts.Count > 4 Then
For Each XMLPart In XMLParts
'Deletes the CustomXMLPart
CustomXMLParts.SelectByNamespace("").item(1).Delete
Next XMLPart
'Adds a CustomXMLPart to the Document
CustomXMLParts.Add
'Loads the CustomXMLPart with data from a XML File
CustomXMLParts(CustomXMLParts.Count).Load ("C:\Users\troy.pilewski\Desktop\CSI\SiteList.xml")
For Each XMLPart In XMLParts
XMLNameSpace = XMLPart.NamespaceURI
If Len(XMLNameSpace) = 0 Then
Set XMLSiteNodes = XMLPart.SelectNodes("/SitesTable/SiteRow/site")
If XMLSiteNodes.Count > 0 Then
For Each XMLSiteNode In XMLSiteNodes
' Debug.Print XMLSiteNode.Text & " (" & XMLSiteNode.Attributes.item(1).Text & ")"
ListItemEntry = XMLSiteNode.Text & " (" & XMLSiteNode.Attributes.item(1).Text & ")"
Debug.Print ListItemEntry
With ActiveDocument.ContentControls(1).DropdownListEntries
.Add ListItemEntry
End With
Next XMLSiteNode
End If
End If
Next XMLPart
ElseIf XMLParts.Count = 4 Then
For Each XMLPart In XMLParts
'Deletes the CustomXMLPart
CustomXMLParts.SelectByNamespace("").item(1).Delete
Next XMLPart
'Adds a CustomXMLPart to the Document
CustomXMLParts.Add
'Loads the CustomXMLPart with data from a XML File
CustomXMLParts(CustomXMLParts.Count).Load ("C:\Users\troy.pilewski\Desktop\CSI\SiteList.xml")
For Each XMLPart In XMLParts
XMLNameSpace = XMLPart.NamespaceURI
If Len(XMLNameSpace) = 0 Then
Set XMLSiteNodes = XMLPart.SelectNodes("/SitesTable/SiteRow/site")
If XMLSiteNodes.Count > 0 Then
For Each XMLSiteNode In XMLSiteNodes
' Debug.Print XMLSiteNode.Text & " (" & XMLSiteNode.Attributes.item(1).Text & ")"
ListItemEntry = XMLSiteNode.Text & " (" & XMLSiteNode.Attributes.item(1).Text & ")"
Debug.Print ListItemEntry
With ActiveDocument.ContentControls(1).DropdownListEntries
.Add ListItemEntry
End With
Next XMLSiteNode
End If
End If
Next XMLPart
Else
'Adds a CustomXMLPart to the Document
CustomXMLParts.Add
'Loads the CustomXMLPart with data from a XML File
CustomXMLParts(CustomXMLParts.Count).Load ("C:\Users\troy.pilewski\Desktop\CSI\SiteList.xml")
For Each XMLPart In XMLParts
XMLNameSpace = XMLPart.NamespaceURI
If Len(XMLNameSpace) = 0 Then
Set XMLSiteNodes = XMLPart.SelectNodes("/SitesTable/SiteRow/site")
For Each XMLSiteNode In XMLSiteNodes
' Debug.Print XMLSiteNode.Text & " (" & XMLSiteNode.Attributes.item(1).Text & ")"
ListItemEntry = XMLSiteNode.Text & " (" & XMLSiteNode.Attributes.item(1).Text & ")"
Debug.Print ListItemEntry
With ActiveDocument.ContentControls(1).DropdownListEntries
.Add ListItemEntry
End With
Next XMLSiteNode
End If
Next XMLPart
End If
ErrorReadCustomXMLInfo:
End Sub
以下是架构:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="SitesTable">
<xs:complexType>
<xs:sequence>
<xs:element name="SiteRow" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="site">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="hull" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>