次の方法で共有


How to: Programmatically delete Outlook contacts

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

This example deletes a contact. The example assumes that a contact named "Armando Pinto" exists in the Contacts folder.

Applies to: The information in this topic applies to VSTO Add-in projects for Outlook. For more information, see Features available by Office application and project type.

Example

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Startup
    DeleteContact("Pinto", "Armando")
End Sub

Private Sub DeleteContact(ByVal lastName As String, _
ByVal firstName As String)
    Dim contact As Outlook.ContactItem = _
        TryCast(Me.Application.GetNamespace("MAPI").GetDefaultFolder( _
        Outlook.OlDefaultFolders.olFolderContacts).Items. _
        Find( _
        String.Format("[LastName]='{0}' AND [FirstName]='{1}'", _
        lastName, firstName)),  _
        Outlook.ContactItem)

    If (contact IsNot Nothing) Then
        contact.Delete()
    End If
End Sub
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    DeleteContact("Pinto", "Armando");
}

private void DeleteContact(string lastName, string firstName)
{
    Outlook.ContactItem contact =
        this.Application.GetNamespace("MAPI").
    GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).
    Items.
    Find(
    string.Format("[LastName]='{0}' AND [FirstName]='{1}'",
    lastName, firstName))
    as Outlook.ContactItem;

    if (contact != null)
    {
        contact.Delete();
    }
}

See also