2010-07-02 24 views
1

不確定綁定一個下拉列表內容控制正確的XML文件的最佳方式:所有我得到的第一個項目。Word 2007中VBA:ActiveDocument.CustomXMLParts和下拉列表

我假設我將不得不遍歷XML文檔,計算項目數,然後調用相應的控件上的.Add方法,但我不確定如何在VBA中執行此操作。

這是我有:

Dim ap As Document 
Dim cnt As Integer 
Set ap = ActiveDocument 
cnt = ap.CustomXMLParts.Count + 1 

ap.CustomXMLParts.Add 
ap.CustomXMLParts(cnt).Load ("C:\test\Employees.xml") 

Dim strXPath1 As String 
strXPath1 = "/Employees/Employee/@name" 
ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath1 

哪些(如預期)獲取第一個名稱屬性;只是不知道如何最好地填充從XML文檔內容控制下拉(參見下面的XML文檔):

<?xml version="1.0"?> 
<Employees> 
    <Employee name="Joe Blow"> 
    <Email>[email protected]</Email> 
    <Extension>201</Extension> 
    </Employee> 
    <Employee name="Bob Smith"> 
    <Email>[email protected]</Email> 
    <Extension>202</Extension> 
    </Employee> 
</Employees> 

回答

1

下拉列表是所有其他內容控件不同 - 你需要使用模式對他們來說:Walkthrough: Binding Content Controls to Custom XML Parts

你要開始像這樣雖然代碼:

Sub BindtoDropDown() 
    Dim ap As Document 
    Set ap = ActiveDocument 
    Dim cp As CustomXMLPart 
    Set cp = ap.CustomXMLParts.Add 
    cp.Load ("C:\Users\Me\Desktop\Employees.xml") 
    Dim strXPath1 As String 
    strXPath1 = "/Employees/Employee/@name" 
    Dim ddCC As ContentControl 
    Set ddCC = ap.ContentControls.Add(Type:=wdContentControlDropdownList) 
    ddCC.XMLMapping.SetMapping (strXPath1) 
End Sub 

這只是設置在喬的下拉打擊,但不填充項的其餘部分。上面的鏈接會告訴你如何做到這一點。

另一個偉大的項目要考慮與內容控制起步時和XML映射爲字內容控件工具包使用。它可用here

+0

我讀過,比如數次;有沒有下拉含量控制在提到有... OK,他們提到這個詞的「下拉」曾經在一個句子,但有沒有例子或指令。 – gravyface 2010-07-03 03:13:08

+0

我的歉意,錯誤的鏈接。我已經更正了上面的內容,並提供了另一個鏈接。 – 2010-07-03 03:29:34