Share via


ControlCollection.AddDatePickerContentControl Method

Definition

Overloads

AddDatePickerContentControl(String)

Adds a new DatePickerContentControl at the current selection in the document.

AddDatePickerContentControl(ContentControl, String)

Adds a new DatePickerContentControl to the collection. The new control is based on a native content control that is already in the document.

AddDatePickerContentControl(Range, String)

Adds a new DatePickerContentControl at the specified range in the document.

AddDatePickerContentControl(String)

Adds a new DatePickerContentControl at the current selection in the document.

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

Parameters

name
String

The name of the new control.

Returns

The DatePickerContentControl that was added to the document.

Exceptions

name is null or has zero length.

A control with the same name is already in the ControlCollection.

Examples

The following code example adds a new DatePickerContentControl to the beginning of the document. The example also modifies the format in which the control displays dates.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project, and call the AddDatePickerControlAtSelection method from the ThisDocument_Startup method.

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

This version is for an application-level add-in that targets the .NET Framework 4 or the .NET Framework 4.5. To use this code, paste it into the ThisAddIn class in your project, and call the AddDatePickerControlAtSelection method from the ThisAddIn_Startup method.

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

Remarks

Use this method to add a new DatePickerContentControl at the current selection in the document at run time. For more information, see Adding Controls to Office Documents at Run Time.

Applies to

AddDatePickerContentControl(ContentControl, String)

Adds a new DatePickerContentControl to the collection. The new control is based on a native content control that is already in the document.

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

Parameters

contentControl
ContentControl

The ContentControl that is the basis for the new control.

name
String

The name of the new control.

Returns

The DatePickerContentControl that was added to the document.

Exceptions

contentControl is null.-or- name is null or has zero length.

A control with the same name is already in the ControlCollection.

contentControl is not a building block gallery (that is, the Type property of contentControl does not have the value Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlDate).

Examples

The following code example creates a new DatePickerContentControl for every native date control that is in the document.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project, and call the CreateDatePickerControlsFromNativeControls method from the ThisDocument_Startup method.

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

This version is for an application-level add-in that targets the .NET Framework 4 or the .NET Framework 4.5. To use this code, paste it into the ThisAddIn class in your project, and call the CreateDatePickerControlsFromNativeControls method from the ThisAddIn_Startup method.

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

The following code example creates a new DatePickerContentControl for every native date control that the user adds to the document.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project. For C#, you must also attach the ThisDocument_DatePickerContentControlAfterAdd event handler to the ContentControlAfterAdd event of the ThisDocument class.

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

To use this code, paste it into the ThisAddIn class in your project. Also, you must attach the ActiveDocument_DatePickerContentControlAfterAdd event handler to the ContentControlAfterAdd event of the active document.

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

Remarks

Use this method to add a new DatePickerContentControl that is based on a native content control in the document at run time. This is useful when you create a DatePickerContentControl at run time, and you want to recreate the same control the next time the document is opened. For more information, see Adding Controls to Office Documents at Run Time.

Applies to

AddDatePickerContentControl(Range, String)

Adds a new DatePickerContentControl at the specified range in the document.

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

Parameters

range
Range

A Range that provides the bounds for the new control.

name
String

The name of the new control.

Returns

The DatePickerContentControl that was added to the document.

Exceptions

name is null or has zero length.

A control with the same name is already in the ControlCollection.

Examples

The following code example adds a new DatePickerContentControl to the beginning of the document. The example also modifies the format in which the control displays dates.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project, and call the AddDatePickerControlAtRange method from the ThisDocument_Startup method.

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

This version is for an application-level add-in that targets the .NET Framework 4 or the .NET Framework 4.5. To use this code, paste it into the ThisAddIn class in your project, and call the AddDatePickerControlAtRange method from the ThisAddIn_Startup method.

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

Remarks

Use this method to add a new DatePickerContentControl at a specified range in the document at run time. For more information, see Adding Controls to Office Documents at Run Time.

Applies to