Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearing Case statement value

Status
Not open for further replies.

Mindlfurry

Technical User
Jan 23, 2022
1
0
0
CA
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)
UserForm1.Show

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)

oMail.Display
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.
 
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
 
> am fairly new to VBS

Oh, and the language used in Outlook is VBA, not VBS - so if you have any further questions about coding in Outlook, probably best to ask in forum707
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top