That's a horrible example there in the Help, and one that's completely
irrelevant to DistListItem.Delete(). It also is very badly written for
Outlook VBA, the instructions for it should have mentioned that you don't
create the Application object in either Outlook form code or Outlook VBA
code.
Here's something that works for deleting the DL "Tom" from the default
Contacts folder. Instead of hard- coding "Tom" the code could pass the DL
name to delete to the Sub to make it more general: Sub DeleteDL(DLToDelete
As String). In that case you'd replace the hard-coded "Tom" in the filter
with the DLToDelete variable.
Sub DeleteDL()
Dim oNamespace As Outlook.NameSpace
Dim oFolder As Outlook.MAPIFolder
Dim oItems As Outlook.Items
Dim oFiltered As Outlook.Items
Dim oDL As Outlook.DistListItem
Dim sFilter As String
Dim strPrompt As String
Set oNamespace = Application.GetNamespace("MAPI")
' get default Contacts folder
Set oFolder = oNamespace.GetDefaultFolder(olFolderContacts)
Set oItems = oFolder.Items
' now filter the Items collection, get DL items where name is "Tom"
' cannot use DLName in the filter, that returns an error. However, the
Subject
' is the same as the DLName, so use that trick.
sFilter = "[MessageClass] = 'IPM.DistList' And [Subject] = 'Tom'"
' to use a passed DLToDelete variable use this filter:
' sFilter = "[MessageClass] = 'IPM.DistList' And [Subject] = '" &
DLToDelete & "'"
Set oFiltered = oItems.Restrict(sFilter)
If oFiltered.Count = 1 Then ' we got the only "Tom" DL
Set oDL = oFiltered.Item(1)
End If
'Prompt the user for confirmation
strPrompt = "Are you sure you want to delete the DL " & oDL.Subject & "?"
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
oDL.Delete
MsgBox ("DL deleted")
End If
End Sub
Ian Millward said:
Sadly, I do need an explanation of that otherwise I wouldn't be asking
for assistance.
The syntax I can't get the hang of is how to select the particular
DistListItem I want to delete.
In simple terms, If I have three DLs: Tom, Dick and Harry and I want to
delete Tom, how do I make that the focus of your "myDistList.Delete".
I hadn't realised that trying to get a little help in understanding how
to code Outlook was going to be like drawing teeth.
Here is the code from the DistListItem.Delete help file which I am unable
to relate to the DistListItem Object.
Since I am just starting with Outlook VBA and haven't yet got very far,
this might as well be written in Serbo-Croat as far as I am concerned.
Sub DeleteTaskFolder()
Dim myolApp As New Outlook.Application
Dim oNamespace As Outlook.NameSpace
Dim oFolder As Outlook.MAPIFolder
Dim oOldFolder As Outlook.MAPIFolder
Dim strPrompt As String
Set oNamespace = myolApp.GetNamespace("MAPI")
Set oFolder = oNamespace.GetDefaultFolder(olFolderTasks)
Set oOldFolder = oFolder.Folders("PersonalTasks")
'Prompt the user for confirmation
strPrompt = "Are you sure you want to delete the folder?"
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
oOldFolder.Delete
MsgBox ("Folder deleted")
End If
End Sub
Kind regards,
Ian Millward
Edinburgh