Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Clearing Case statement value

Clearing Case statement value

Clearing Case statement value

I am fairly new to VBS but am pleased with the Outlook form I have created with one issue.

The script creates an Outlook form where a simple pull down menu is displayed along with an OK button. When selecting an entry from the pull down menu, I grab a previously saved email template where it gets displayed and can be easily updated and sent.

My problem is when I run the script a second time and simply close the window using the "X' in the upper right corner, the script continues to run, remembering the previously used case value, and proceeds to show an email message. I am trying to get the rotten thing to simply close after clicking the "X".

The Outlook user form (named UserForm1)is:
Private Sub CommandButton1_Click()
lstNum = ComboBox1.ListIndex
Unload Me
End Sub

Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "Patching"
.AddItem "Emergency"
.AddItem "Troubleshooting"
.AddItem "Unplanned"
.AddItem "Test 5"
.AddItem "no change"
End With
End Sub

....The Outlook module is:
Public lstNum As Long

Public Sub ChooseTemplate()

Dim oMail As Outlook.MailItem

Dim strTemplate As String
Set oMail = Application.CreateItem(olMailItem)

Select Case lstNum
Case -1
' -1 this is used if nothing is selected but the OK button is pressed
strTemplate = "C:\Users\Rob\AppData\Roaming\Microsoft\Templates\Nothing Picked.oft"
Case 0
strTemplate = "C:\Users\Rob\AppData\Roaming\Microsoft\Templates\System Outage for patching.oft"
Case 1
strTemplate = "C:\Users\Rob\AppData\Roaming\Microsoft\Templates\System Outage for emergency.oft"
Case 2
strTemplate = "C:\Users\Rob\AppData\Roaming\Microsoft\Templates\System Outage for troubleshooting.oft"
Case 3
strTemplate = "C:\Users\Rob\AppData\Roaming\Microsoft\Templates\System Outage for unplanned work.oft"
Case 4
strTemplate = "C:\Users\Rob\AppData\Roaming\Microsoft\Templates\System Outage for testing.oft"
Case 5
strTemplate = "C:\Users\Rob\AppData\Roaming\Microsoft\Templates\Nothing Picked.oft"
End Select
Set oMail = Application.CreateItemFromTemplate(strTemplate)

Set oMail = Nothing

End Sub

....any help would be greatly appreciated. I thought it wold be easy but I just need to clear the previous CASE selection value.

RE: Clearing Case statement value

Your problem is the scope of the variable lstNum. Because you have declared at at module level, it is available to the whole module, and retains it's value - whatever that may be - for the entire time the module is running.

Declare it inside the ChooseTemplate sub instead, so that it only exists (has scope) within that subroutine

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close