Questions on my program 
Author Message
 Questions on my program

Hi,

After a couple of days of struggle, I've written a module that bring
up a user form with a list of the distribution lists, and, upon the
user's selection, fills in the To: field and the Subject field of a
new message. The subject field is based upon the "note" field of the
said distribution list.

The whole purpose of this module is to automate filling in the Subject
field of a mail, which should be the name of a project in my firm.

Now as i look at the end product, i have a few questions. These
questions arise, in part, because I've sort of borrowed codes from
here and there without knowing exactly what they mean.

1. When a distribution list has been deleted, the list box still shows
the deleted item. This problem resolves only after terminating Outlook
and re-starting it.

2. Is there any way to figure out the number of the item on the
listbox chosen by the user? I had to go all the way around by
comparing Listbox1 with every item in the distribution list again,
since something like myfolder.items.item(Listbox1) doesn't work.

3. In each code, i had to repeat "Dim"ming the same variables. Is this
really necessary?

4. Why is "Set" in use here? I've tried it without "set" and it works
fine. If variable such as myfolder doesn't vary throughout a module,
is there any need to set it, instead of just assigning a value to it?

Many thanks. Your answers and comments will help me greatly improve my
programming skills.

=====================================================================
Private Sub CommandButton1_Click()

Dim i As Integer, iItemNum As Integer
Dim msg As Object
Dim mynamespace, myfolder
Dim myolapp As New Outlook.Application

Set mynamespace = myolapp.GetNamespace("MAPI")
Set myfolder = mynamespace.GetDefaultFolder(olFolderContacts)
Set msg = Outlook.CreateItem(olMailItem)

Userform1.Hide

For i = 1 To myfolder.Items.Count
    If myfolder.Items.Item(i).DLName = ListBox1 Then iItemNum = i
Next

With msg
    For i = 1 To myfolder.Items.Item(iItemNum).MemberCount
        .Recipients.Add
(myfolder.Items.Item(iItemNum).GetMember(i).Name)
    Next
    .subject = myfolder.Items.Item(iItemNum).Body
    .Display
End With

Set msg = Nothing

End Sub

Private Sub UserForm_Initialize()

Dim mynamespace, myfolder, myitem
Dim myolapp As New Outlook.Application

Set mynamespace = myolapp.GetNamespace("MAPI")
Set myfolder = _
    mynamespace.GetDefaultFolder(olFolderContacts)

For Each myitem In myfolder.Items
    ListBox1.AddItem myitem.DLName
Next

End Sub
=====================================================================



Mon, 19 Jan 2004 14:04:09 GMT  
 Questions on my program
Answers inline.

--
Ken Slovak
[MVP - Outlook]
Lead Author, Professional Outlook 2000 Programming, Wrox Press
Co-author of "Programming Microsoft Outlook 2000", Chapters 8-13,
Appendices, Sams


Quote:
> Hi,

> After a couple of days of struggle, I've written a module that bring
> up a user form with a list of the distribution lists, and, upon the
> user's selection, fills in the To: field and the Subject field of a
> new message. The subject field is based upon the "note" field of the
> said distribution list.

> The whole purpose of this module is to automate filling in the
Subject
> field of a mail, which should be the name of a project in my firm.

> Now as i look at the end product, i have a few questions. These
> questions arise, in part, because I've sort of borrowed codes from
> here and there without knowing exactly what they mean.

> 1. When a distribution list has been deleted, the list box still
shows
> the deleted item. This problem resolves only after terminating
Outlook
> and re-starting it.

The listbox contents will still show the deleted DL unless you requery
the list of DLs and refresh your listbox.

Quote:

> 2. Is there any way to figure out the number of the item on the
> listbox chosen by the user? I had to go all the way around by
> comparing Listbox1 with every item in the distribution list again,
> since something like myfolder.items.item(Listbox1) doesn't work.

The ListIndex property will give you the currently selected item's row
in the listbox. -1 means nothing selected, first item selected would
give 0. Last item would give ListCount - 1.

Quote:

> 3. In each code, i had to repeat "Dim"ming the same variables. Is
this
> really necessary?

Where you dim variables depends on the scope and visibility you want.
Items dimmed in a procedure are local to that procedure. It can lead
to unwanted modifications of variables if you declare them at module
level and then work with them in more than one procedure. It is
usually safe to instantiate only one Outlook object and use it
throughout. You have to decide for each object or variable.

Quote:

> 4. Why is "Set" in use here? I've tried it without "set" and it
works
> fine. If variable such as myfolder doesn't vary throughout a module,
> is there any need to set it, instead of just assigning a value to

it?
Set is required for instantiating objects. It isn't used for
variables. If you access an object without using Set you will either
get an error, a return of Nothing or if the object already exists then
you will access the default property of the object, not the object
itself. One example of the bad things that can happen when you do this
is accessing a MAPIFolder object that way, you end up renaming the
folder. People who have accidently renamed their Inbox that way learn
a nice lesson on the use of Set.

Quote:

> Many thanks. Your answers and comments will help me greatly improve
my
> programming skills.

=====================================================================

- Show quoted text -

Quote:
> Private Sub CommandButton1_Click()

> Dim i As Integer, iItemNum As Integer
> Dim msg As Object
> Dim mynamespace, myfolder
> Dim myolapp As New Outlook.Application

> Set mynamespace = myolapp.GetNamespace("MAPI")
> Set myfolder = mynamespace.GetDefaultFolder(olFolderContacts)
> Set msg = Outlook.CreateItem(olMailItem)

> Userform1.Hide

> For i = 1 To myfolder.Items.Count
>     If myfolder.Items.Item(i).DLName = ListBox1 Then iItemNum = i
> Next

> With msg
>     For i = 1 To myfolder.Items.Item(iItemNum).MemberCount
>         .Recipients.Add
> (myfolder.Items.Item(iItemNum).GetMember(i).Name)
>     Next
>     .subject = myfolder.Items.Item(iItemNum).Body
>     .Display
> End With

> Set msg = Nothing

> End Sub

> Private Sub UserForm_Initialize()

> Dim mynamespace, myfolder, myitem
> Dim myolapp As New Outlook.Application

> Set mynamespace = myolapp.GetNamespace("MAPI")
> Set myfolder = _
>     mynamespace.GetDefaultFolder(olFolderContacts)

> For Each myitem In myfolder.Items
>     ListBox1.AddItem myitem.DLName
> Next

> End Sub

=====================================================================


Tue, 20 Jan 2004 03:05:35 GMT  
 Questions on my program
Some follow-up questions.

1. How do you requery and refresh a listbox?
2. What is "instantiating"?

Thanks in advance.

Quote:
> The listbox contents will still show the deleted DL unless you requery
> the list of DLs and refresh your listbox.

> Where you dim variables depends on the scope and visibility you want.
> Items dimmed in a procedure are local to that procedure. It can lead
> to unwanted modifications of variables if you declare them at module
> level and then work with them in more than one procedure. It is
> usually safe to instantiate only one Outlook object and use it
> throughout. You have to decide for each object or variable.

> Set is required for instantiating objects. It isn't used for
> variables. If you access an object without using Set you will either
> get an error, a return of Nothing or if the object already exists then
> you will access the default property of the object, not the object
> itself. One example of the bad things that can happen when you do this
> is accessing a MAPIFolder object that way, you end up renaming the
> folder. People who have accidently renamed their Inbox that way learn
> a nice lesson on the use of Set.



Tue, 20 Jan 2004 13:50:20 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Questions regarding Outlook programming

2. Stupid question on BASIC programming

3. Misc questions about my program

4. Navig. among question on test program

5. question about vb5 programs and the system tray

6. Question about VB programming method

7. Newbie question about Visual Programming Jobs

8. Newbie has questions on database programming

9. Newbie question: VisualBasic for programming adaptive menus ???

10. Question about opening programs?

11. QUESTION: Compiled VB5 programs

12. A question about Setup Programs

 

 
Powered by phpBB® Forum Software