deleting distribution list

I

Ian Millward

I'm just starting with Outlook vba and I have created a dl OK but can't get
the hang of how to delete it after I have finished using it to populate the
Bcc field with my news letter group members from my access db.

I created it with this snippet:

Public Sub CreateDL()
Dim myolApp As New Outlook.Application
Dim myDistList As Outlook.DistListItem
Set myDistList = myolApp.CreateItem(olDistributionListItem)
myDistList.DLName = "MyDL"
myDistList.Save
myDistList.Display
End Sub

would someone point me in the right direction please.

Many thanks.
 
K

Ken Slovak

In Outlook VBA code you never should use New or CreateObject for the
Outlook.Application object. You are supplied with an Application object
already that is trusted and won't trigger the Outlook security. So the first
lines should read:

Dim myolApp As Outlook.Application
Set myolApp = Application

If you want to delete the object have you tried the object's Delete()
method? Learn to use the Object Browser (F2). It will show you every method,
property and event for any Outlook object and most of the time you can call
up Help that also shows code snippets for the object.
 
I

Ian Millward

Ken,

Thanks for that definitive answer to a question which I didn't ask.

The help file for the 'Delete' method uses a MAPI Folder as an example and
it is not clear to me what is required to delete a DistListItem.

Is there anybody else who can be a little less Delphic in showing me a
simple snippet of code to write a sub to delete a Distribution List?

Many thanks.
 
K

Ken Slovak

Try:

myDistList.Delete

to delete the item.

Is that a short enough piece of code for you, or do you need an explanation
for that?
 
I

Ian Millward

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
 
K

Ken Slovak

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
 
I

Ian Millward

Ken,

Thanks for that.

I was beginning to despair that I would ever come to terms with it but I can
follow your code fine. I hope I am now off to a flying start with Outlook
VBA.

Regards,

Ian Millward

Ken Slovak said:
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
 
K

Ken Slovak

Successful Outlook development can only be done during certain phases of the
moon, and after sacrificing a chicken in a midnight ritual. Easy it's not :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top