ControlCollection.AddDatePickerContentControl 方法

定义

重载

AddDatePickerContentControl(String)

在文档的当前选定内容中添加一个新的 DatePickerContentControl

AddDatePickerContentControl(ContentControl, String)

将新 DatePickerContentControl 添加到集合中。 新控件基于文档中已存在的本机内容控件。

AddDatePickerContentControl(Range, String)

在文档的指定范围中,添加一个新的 DatePickerContentControl

AddDatePickerContentControl(String)

在文档的当前选定内容中添加一个新的 DatePickerContentControl

public:
 Microsoft::Office::Tools::Word::DatePickerContentControl ^ AddDatePickerContentControl(System::String ^ name);
public Microsoft.Office.Tools.Word.DatePickerContentControl AddDatePickerContentControl (string name);
abstract member AddDatePickerContentControl : string -> Microsoft.Office.Tools.Word.DatePickerContentControl
Public Function AddDatePickerContentControl (name As String) As DatePickerContentControl

参数

name
String

新控件的名称。

返回

DatePickerContentControl,已添加到该文档。

例外

namenull,或其长度为零。

ControlCollection 中已存在具有相同名称的控件。

示例

下面的代码示例将新的 DatePickerContentControl 添加到文档的开头。 该示例还修改控件显示日期的格式。

此版本用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 AddDatePickerControlAtSelection 方法。

private Microsoft.Office.Tools.Word.DatePickerContentControl datePickerControl1;

private void AddDatePickerControlAtSelection()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    this.Paragraphs[1].Range.Select();

    datePickerControl1 = this.Controls.AddDatePickerContentControl("datePickerControl1");
    datePickerControl1.DateDisplayFormat = "MMMM d, yyyy";
    datePickerControl1.PlaceholderText = "Choose a date";
}
Dim datePickerControl1 As Microsoft.Office.Tools.Word.DatePickerContentControl

Private Sub AddDatePickerControlAtSelection()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Me.Paragraphs(1).Range.Select()
    datePickerControl1 = Me.Controls.AddDatePickerContentControl("datePickerControl1")
    datePickerControl1.DateDisplayFormat = "MMMM d, yyyy"
    datePickerControl1.PlaceholderText = "Choose a date"
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 AddDatePickerControlAtSelection 方法。

private Microsoft.Office.Tools.Word.DatePickerContentControl datePickerControl1;

private void AddDatePickerControlAtSelection()
{
    if (this.Application.ActiveDocument == null)
        return;

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();
    vstoDoc.Paragraphs[1].Range.Select();

    datePickerControl1 = vstoDoc.Controls.AddDatePickerContentControl("datePickerControl1");
    datePickerControl1.DateDisplayFormat = "MMMM d, yyyy";
    datePickerControl1.PlaceholderText = "Choose a date";
}
Dim datePickerControl1 As Microsoft.Office.Tools.Word.DatePickerContentControl

Private Sub AddDatePickerControlAtSelection()
    If Me.Application.ActiveDocument Is Nothing Then
        Return
    End If

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
    vstoDoc.Paragraphs(1).Range.Select()
    datePickerControl1 = vstoDoc.Controls.AddDatePickerContentControl("datePickerControl1")
    datePickerControl1.DateDisplayFormat = "MMMM d, yyyy"
    datePickerControl1.PlaceholderText = "Choose a date"
End Sub

注解

使用此方法在运行时在文档中的当前选定内容处添加新 DatePickerContentControl 的 。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于

AddDatePickerContentControl(ContentControl, String)

将新 DatePickerContentControl 添加到集合中。 新控件基于文档中已存在的本机内容控件。

public:
 Microsoft::Office::Tools::Word::DatePickerContentControl ^ AddDatePickerContentControl(Microsoft::Office::Interop::Word::ContentControl ^ contentControl, System::String ^ name);
public Microsoft.Office.Tools.Word.DatePickerContentControl AddDatePickerContentControl (Microsoft.Office.Interop.Word.ContentControl contentControl, string name);
abstract member AddDatePickerContentControl : Microsoft.Office.Interop.Word.ContentControl * string -> Microsoft.Office.Tools.Word.DatePickerContentControl
Public Function AddDatePickerContentControl (contentControl As ContentControl, name As String) As DatePickerContentControl

参数

contentControl
ContentControl

一个 ContentControl,它是新控件的基础。

name
String

新控件的名称。

返回

DatePickerContentControl,已添加到该文档。

例外

contentControlnull.-或- name is null 或 具有零长度。

ControlCollection 中已存在具有相同名称的控件。

contentControl不是构建基块库 (也就是说, TypecontentControl 属性没有值 Microsoft.Office.Interop.Word。WdContentControlType.wdContentControlDate) 。

示例

下面的代码示例为文档中的每个本机日期控件创建一个新的 DatePickerContentControl

此版本用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 CreateDatePickerControlsFromNativeControls 方法。

private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.DatePickerContentControl> datePickerControls;

private void CreateDatePickerControlsFromNativeControls()
{
    datePickerControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.DatePickerContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlDate)
        {
            count++;
            Microsoft.Office.Tools.Word.DatePickerContentControl tempControl =
                this.Controls.AddDatePickerContentControl(nativeControl,
                "VSTODatePickerContentControl" + count.ToString());
            datePickerControls.Add(tempControl);
        }
    }
}
Private datePickerControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.DatePickerContentControl)

Private Sub CreateDatePickerControlsFromNativeControls()
    If Me.ContentControls.Count <= 0 Then
        Return
    End If

    Dim count As Integer = 0
    For Each nativeControl As Word.ContentControl In Me.ContentControls
        If nativeControl.Type = Word.WdContentControlType.wdContentControlDate Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.DatePickerContentControl = _
                Me.Controls.AddDatePickerContentControl(nativeControl, _
                "VSTODatePickerContentControl" + count.ToString())
            datePickerControls.Add(tempControl)
        End If
    Next nativeControl
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 CreateDatePickerControlsFromNativeControls 方法。

private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.DatePickerContentControl> datePickerControls;

private void CreateDatePickerControlsFromNativeControls()
{
    if (this.Application.ActiveDocument == null)
        return;

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    datePickerControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.DatePickerContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlDate)
        {
            count++;
            Microsoft.Office.Tools.Word.DatePickerContentControl tempControl =
                vstoDoc.Controls.AddDatePickerContentControl(nativeControl,
                "VSTODatePickerContentControl" + count.ToString());
            datePickerControls.Add(tempControl);
        }
    }
}
Private datePickerControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.DatePickerContentControl)

Private Sub CreateDatePickerControlsFromNativeControls()
    If Me.Application.ActiveDocument Is Nothing Then
        Return
    End If

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If vstoDoc.ContentControls.Count <= 0 Then
        Return
    End If

    Dim count As Integer = 0
    For Each nativeControl As Word.ContentControl In vstoDoc.ContentControls
        If nativeControl.Type = Word.WdContentControlType.wdContentControlDate Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.DatePickerContentControl = _
                vstoDoc.Controls.AddDatePickerContentControl(nativeControl, _
                "VSTODatePickerContentControl" + count.ToString())
            datePickerControls.Add(tempControl)
        End If
    Next nativeControl
End Sub

下面的代码示例为用户添加到文档的每个本机日期控件创建一个新的 DatePickerContentControl

此版本用于文档级自定义。 若要使用此代码,请将其粘贴到 ThisDocument 项目中的 类中。 对于 C#,还必须将 ThisDocument_DatePickerContentControlAfterAdd 事件处理程序附加到 ContentControlAfterAdd 类的 ThisDocument 事件。

void ThisDocument_DatePickerContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlDate)
    {
        this.Controls.AddDatePickerContentControl(NewContentControl,
            "DatePickerControl" + NewContentControl.ID);
    }
}
Private Sub ThisDocument_DatePickerContentControlAfterAdd(ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlDate Then
        Me.Controls.AddDatePickerContentControl(NewContentControl, _
            "DatePickerControl" + NewContentControl.ID)
    End If
End Sub

若要使用此代码,请将其粘贴到 ThisAddIn 项目中的 类中。 此外,必须将事件处理程序附加到ActiveDocument_DatePickerContentControlAfterAddContentControlAfterAdd活动文档的 事件。

void ActiveDocument_DatePickerContentControlAfterAdd(
    Word.ContentControl NewContentControl, bool InUndoRedo)
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlDate)
    {
        vstoDoc.Controls.AddDatePickerContentControl(NewContentControl,
            "DatePickerControl" + NewContentControl.ID);
    }
}
Private Sub ActiveDocument_DatePickerContentControlAfterAdd( _
    ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean)

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If NewContentControl.Type = Word.WdContentControlType. _
        wdContentControlDate Then
        vstoDoc.Controls.AddDatePickerContentControl(NewContentControl, _
            "DatePickerControl" + NewContentControl.ID)
    End If
End Sub

注解

使用此方法在运行时添加基于文档中本机内容控件的新 DatePickerContentControl 。 如果在运行时创建 , DatePickerContentControl 并且希望在下次打开文档时重新创建同一控件,这非常有用。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于

AddDatePickerContentControl(Range, String)

在文档的指定范围中,添加一个新的 DatePickerContentControl

public:
 Microsoft::Office::Tools::Word::DatePickerContentControl ^ AddDatePickerContentControl(Microsoft::Office::Interop::Word::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Word.DatePickerContentControl AddDatePickerContentControl (Microsoft.Office.Interop.Word.Range range, string name);
abstract member AddDatePickerContentControl : Microsoft.Office.Interop.Word.Range * string -> Microsoft.Office.Tools.Word.DatePickerContentControl
Public Function AddDatePickerContentControl (range As Range, name As String) As DatePickerContentControl

参数

range
Range

Range,可为新控件提供边界。

name
String

新控件的名称。

返回

DatePickerContentControl,已添加到该文档。

例外

namenull,或其长度为零。

ControlCollection 中已存在具有相同名称的控件。

示例

下面的代码示例将新的 DatePickerContentControl 添加到文档的开头。 该示例还修改控件显示日期的格式。

此版本用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 AddDatePickerControlAtRange 方法。

private Microsoft.Office.Tools.Word.DatePickerContentControl datePickerControl2;

private void AddDatePickerControlAtRange()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();

    datePickerControl2 = this.Controls.AddDatePickerContentControl(this.Paragraphs[1].Range,
         "datePickerControl2");
    datePickerControl2.DateDisplayFormat = "MMMM d, yyyy";
    datePickerControl2.PlaceholderText = "Choose a date";
}
Dim datePickerControl2 As Microsoft.Office.Tools.Word.DatePickerContentControl

Private Sub AddDatePickerControlAtRange()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    datePickerControl2 = Me.Controls.AddDatePickerContentControl(Me.Paragraphs(1).Range, "datePickerControl2")
    datePickerControl2.DateDisplayFormat = "MMMM d, yyyy"
    datePickerControl2.PlaceholderText = "Choose a date"
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 AddDatePickerControlAtRange 方法。

private Microsoft.Office.Tools.Word.DatePickerContentControl datePickerControl2;

private void AddDatePickerControlAtRange()
{
    if (this.Application.ActiveDocument == null)
        return;

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();

    datePickerControl2 = vstoDoc.Controls.AddDatePickerContentControl(
        vstoDoc.Paragraphs[1].Range,
         "datePickerControl2");
    datePickerControl2.DateDisplayFormat = "MMMM d, yyyy";
    datePickerControl2.PlaceholderText = "Choose a date";
}
Dim datePickerControl2 As Microsoft.Office.Tools.Word.DatePickerContentControl

Private Sub AddDatePickerControlAtRange()
    If Me.Application.ActiveDocument Is Nothing Then
        Return
    End If

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
    datePickerControl2 = vstoDoc.Controls.AddDatePickerContentControl( _
        vstoDoc.Paragraphs(1).Range, "datePickerControl2")
    datePickerControl2.DateDisplayFormat = "MMMM d, yyyy"
    datePickerControl2.PlaceholderText = "Choose a date"
End Sub

注解

使用此方法在运行时在文档中的指定区域添加新 DatePickerContentControl 的 。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于