Share via


DocumentBase.SelectLinkedControls 方法

傳回文件中連結至特定自訂 XML 節點的所有內容控制項。

命名空間:  Microsoft.Office.Tools.Word
組件:  Microsoft.Office.Tools.Word.v4.0.Utilities (在 Microsoft.Office.Tools.Word.v4.0.Utilities.dll 中)

語法

'宣告
Public Function SelectLinkedControls ( _
    node As CustomXMLNode _
) As ContentControls
public ContentControls SelectLinkedControls(
    CustomXMLNode node
)

參數

傳回值

型別:Microsoft.Office.Interop.Word.ContentControls
ContentControls 集合,包含連結至指定之自訂 XML 節點的內容控制項。

範例

下列程式碼範例會將三個純文字內容控制項加入至目前文件。 這個範例也會加入包含員工資料的 CustomXMLPart,並將其中兩個內容控制項連結至 CustomXMLPart 中的 XML 節點。 接下來,程式碼會取得連結至員工姓名節點的控制項,顯示列出所找到之已連結控制項數目的訊息方塊,最後逐一查看連結的控制項以顯示每個已連結控制項的標題。 執行這個程式碼時,您應該會取得一個連結的控制項,而且其標題應為 Employee Name。 若要使用這個範例,請在文件層級專案中的 ThisDocument 類別執行。

Private Sub LinkedControls()
    Me.Paragraphs.Last.Range.InsertParagraphAfter()
    Dim employeeName As Microsoft.Office.Tools.Word.PlainTextContentControl _
        = Me.Controls.AddPlainTextContentControl(Me.Paragraphs.Last.Range, _
        "employeeName")
    employeeName.Title = "Employee Name"
    Me.Paragraphs.Last.Range.InsertParagraphAfter()
    Dim employeeHireDate As  _
        Microsoft.Office.Tools.Word.PlainTextContentControl = _
        Me.Controls.AddPlainTextContentControl(Me.Paragraphs.Last.Range, _
        "employeeHireDate")
    employeeHireDate.Title = "Employee Hire Date"
    Me.Paragraphs.Last.Range.InsertParagraphAfter()
    Dim comments As Microsoft.Office.Tools.Word.PlainTextContentControl _
        = Me.Controls.AddPlainTextContentControl(Me.Paragraphs.Last.Range, _
        "comments")
    comments.Title = "Comments"

    Dim xmlString As String = _
        "<?xml version=""1.0"" encoding=""utf-8"" ?>" _
        + "<employees>" _
        + "<employee>" _
        + "<name>Karina Leal</name>" _
        + "<hireDate>1999-04-01</hireDate>" _
        + "</employee>" _
        + "</employees>"
    Dim employeeXMLPart As Office.CustomXMLPart = _
        Me.CustomXMLParts.Add(xmlString)

    employeeName.XMLMapping.SetMapping("/employees/employee/name")
    employeeHireDate.XMLMapping.SetMapping("/employees/employee/hireDate")

    Dim node As Office.CustomXMLNode = employeeXMLPart.SelectSingleNode( _
        "/employees[1]/employee[1]/name[1]")

    Dim linkedControls As Word.ContentControls = Me.SelectLinkedControls(node)

    MessageBox.Show("Number of controls linked to the " + node.XPath _
        + " node: " + linkedControls.Count.ToString())
    For Each linkedControl As Word.ContentControl In linkedControls
        MessageBox.Show("Linked control title: " + linkedControl.Title)
    Next
End Sub
private void LinkedControls()
{
    this.Paragraphs.Last.Range.InsertParagraphAfter();
    Microsoft.Office.Tools.Word.PlainTextContentControl employeeName = 
        this.Controls.AddPlainTextContentControl(this.Paragraphs.Last.Range, 
        "employeeName");
    employeeName.Title = "Employee Name";
    this.Paragraphs.Last.Range.InsertParagraphAfter();
    Microsoft.Office.Tools.Word.PlainTextContentControl employeeHireDate = 
        this.Controls.AddPlainTextContentControl(this.Paragraphs.Last.Range, 
        "employeeHireDate");
    employeeHireDate.Title = "Employee Hire Date";
    this.Paragraphs.Last.Range.InsertParagraphAfter();
    Microsoft.Office.Tools.Word.PlainTextContentControl comments =
        this.Controls.AddPlainTextContentControl(this.Paragraphs.Last.Range, 
        "comments");
    comments.Title = "Comments";

    string xmlString = 
        "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" 
        + "<employees>" 
        + "<employee>" 
        + "<name>Karina Leal</name>" 
        + "<hireDate>1999-04-01</hireDate>"             
        + "</employee>" 
        + "</employees>";
    Office.CustomXMLPart employeeXMLPart = 
        this.CustomXMLParts.Add(xmlString, missing);

    employeeName.XMLMapping.SetMapping(
        "/employees/employee/name", "", employeeXMLPart);
    employeeHireDate.XMLMapping.SetMapping(
        "/employees/employee/hireDate", "", employeeXMLPart);

    Office.CustomXMLNode node = employeeXMLPart.SelectSingleNode(
        "/employees[1]/employee[1]/name[1]");

    Word.ContentControls linkedControls = this.SelectLinkedControls(node);
    MessageBox.Show("Number of controls linked to the " + node.XPath 
                    + " node: " + linkedControls.Count.ToString());
    foreach (Word.ContentControl linkedControl in linkedControls)
    {
        MessageBox.Show("Linked control title: " + linkedControl.Title);
    }            
}

.NET Framework 安全性

請參閱

參考

DocumentBase 類別

Microsoft.Office.Tools.Word 命名空間