VBA Cases with Listbox - Can you use Multi-Select?

Post number 4 has been selected as the best answer.

owensnw

Member
Outlook version
Outlook 2019 64-bit
Email Account
Office 365 Exchange
I currently have a VBA scrip that is running in Outlook where it pops up a listbox. The user selects the attachment, then it is added to the current email and unloads the user form. Now we are wondering if we can do multi-select and choose multiple attachments to go to the email. Here is the listbox:

1605125839237.png


Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

If CloseMode = 0 Then Cancel = True

End Sub
Private Sub CommandButton2_Click()
End
End Sub

Private Sub CommandButton1_Click()
lstNo = ListBox1.ListIndex
Unload Me
End Sub

Private Sub ListBox1_Change()

End Sub

Private Sub UserForm_Initialize()
With ListBox1
.AddItem "-----------Credit------------"
.AddItem "OPE W9"
.AddItem "OPE Credit References"
.AddItem "OPE Credit Application"
.AddItem "----------Brochures----------"
.AddItem "Vogelsang Municipal Brochure"
.AddItem "Vogelsang Wine Brochure"
.AddItem "Vogelsang XR Buyers Guide"
.AddItem "Barnes Sithe Brochure"
.AddItem "Barnes SH Brochure"
.AddItem "Barnes Razor Brochure"
.AddItem "Barnes Pressure Sewer Brochure"
.AddItem "Barnes Upgrade Core Brochure"
.AddItem "Barnes Blade Brochure"
.AddItem "Barnes FRP Engineered Basins Brochure"
.AddItem "Mencarelli Wine Pump Brochure"
.AddItem "WasteCorp Municipal Brochure"
.AddItem "WasteCorp Industrial Brochure"
.AddItem "WasteCorp Double-Disc Brochure"
.AddItem "WasteCorp Self-Priming Brochure"
.AddItem "WasteCorp Pre-Packaged Lift Station Brochure"
.AddItem "BioGill Tower Plus Datasheet"
.AddItem "BioGill Ultra Datasheet"
.AddItem "BioGill Brewery Overview"
.AddItem "BioGill Food/Bev Overview"
.AddItem "ARO FDA / Food Compliant Pumps"
.AddItem "ARO 1.5 Inch Diaphragm Pump Cart Flyer"
.AddItem "ARO 2.5 Inch Diaphragm Pump Cart Flyer"
.AddItem "ARO Metalic Diaphragm Pump Brochure"
.AddItem "ARO Non-Metalic Diaphragm Pump Brochure"
.AddItem "Cloacina Product Card"
.AddItem "Cloacina Wine Introduction & Advantages"
.AddItem "Cloacina Wine Brochure"
.AddItem "Cloacina Mempac Mini Package"
.AddItem "Cloacina CEMPAC Thickener Brochure"
.AddItem "OpenChannelFlow Parshall Flume Brochure"
.AddItem "OpenChannelFlow Single Piece Shelters Brochure"
.AddItem "OpenChannelFlow Grinder Manhole Brochure"
.AddItem "AK Industries SS 1.25 & 2 Inch Rail Systems"
.AddItem "AK Industries SS 1.25 - 6 Inch Rail Systems"
.AddItem "AK Industries FRP Basins & Accessories"
.AddItem "Prime Solution RFP Family Brochure"
.AddItem "Prime Solution Simple Dewatering Solutions"
.AddItem "Prime Solution RFP Brochure"
.AddItem "Prime Solution RFP2.0 Brochure"
.AddItem "Prime Solution RFSP Brochure"
.AddItem "Prime Solution INT Thickener Brochure"
.AddItem "FOGRod Level Control Brochure"
.AddItem "FOGRod Level Control Presentation"
End With
End Sub

Then here is the code that calls for the object and has all the cases:

Public Sub AddOPEAttachments()

Dim objItem As Object
Dim oMail As Outlook.MailItem
Dim myAttachments As Outlook.Attachments

If TypeName(Application.ActiveInspector.CurrentItem) = "MailItem" Then
Set objItem = Application.ActiveInspector.CurrentItem
End If

Set myAttachments = objItem.Attachments

OPEAttachmentMenu.Show

Select Case lstNo
Case -1
'oMail.Subject = objItem.Subject
Case 0
myAttachments.Add "C:\OneDrive - Pump\Pump\Financial\W9 2020 NEW.pdf", _
olByValue, 1, "NOTHING"
' Pump Information
Case 1
myAttachments.Add "C:\OneDrive - Pump\ Pump\Financial\W9 2020 NEW.pdf", _
olByValue, 1, "Pump W9"
Case 2
myAttachments.Add "C:\OneDrive - Pump\Pump\Financial\Pump - Credit & Bank References.pdf", _
olByValue, 1, " Pump - Credit & Bank References"
Case 3
myAttachments.Add "C:\OneDrive - Pump\Pump\Forms DOC\General Information\Pump Credit Application.pdf", _
olByValue, 1, " Pump - Credit Application"
objItem.HTMLBody = "See the attached credit application and return, or complete it online at the link: " & vbNewLine & vbNewLine & "<a href=""http://credit.pump.com"">Click Here</a>"
Case 4
myAttachments.Add "C:\OneDrive - Pump\Pump\Financial\W9 2020 NEW.pdf", _
olByValue, 1, "NOTHING"
'Vogelsang Information
Case 5
myAttachments.Add "C:\OneDrive - Pump\Venders\Vogelsang\Brochures\Vogelsang Municipal Brochure.pdf", _
olByValue, 1, "Vogelsang Municipal Brochure"
Case 6
myAttachments.Add "C:\OneDrive - Pump\Venders\Vogelsang\Brochures\Vogelsang Wine Brochure-sm.pdf", _
olByValue, 1, "Vogelsang Wine Brochure"
Case 7
myAttachments.Add "C:\OneDrive - Pump\Venders\Vogelsang\Brochures\Vogelsang XRipper Buyers Guide 2019-sm.pdf", _
olByValue, 1, "Vogelsang XRipper Brochure"
'Barnes Information
Case 8
myAttachments.Add "C:\OneDrive - Pump\Venders\Crane Pump & Systems\Technical\Barnes Pump\Brochures\Barnes Sithe Brochure.pdf", _
olByValue, 1, "Barnes Sithe Brochure"
Case 9
myAttachments.Add "C:\OneDrive - Pump\Venders\Crane Pump & Systems\Technical\Barnes Pump\Brochures\Barnes SH Brochure.pdf", _
olByValue, 1, "Barnes SH Brochure"
Case 10
myAttachments.Add "C:\OneDrive - Pump\Venders\Crane Pump & Systems\Technical\Barnes Pump\Brochures\Barnes Razor Brochure.pdf", _
olByValue, 1, "Barnes Razor Brochure"
Case 11
myAttachments.Add "C:\OneDrive - Pump\Venders\Crane Pump & Systems\Technical\Barnes Pump\Brochures\2020 Pressure Sewer Market Brochure.pdf", _
olByValue, 1, "Barnes Pressure Sewer Brochure"
Case 12
myAttachments.Add "C:\OneDrive - Pump\Venders\Crane Pump & Systems\Technical\Barnes Pump\Brochures\2020 Upgrade Core Brochure.pdf", _
olByValue, 1, "Barnes Upgrade Cores Brochure"
Case 13
myAttachments.Add "C:\OneDrive - Pump\Venders\Crane Pump & Systems\Technical\Barnes Pump\Brochures\Barnes Blade Brochure XP sm.pdf", _
olByValue, 1, "Barnes Blade Brochure"
Case 14
myAttachments.Add "C:\OneDrive - Pump\Venders\Crane Pump & Systems\Technical\Barnes Pump\Brochures\Barnes Engineered Basins Brochurepdf", _
olByValue, 1, "Barnes FRP Engineered Basins Brochure"
'Mencarelli Information
Case 15
myAttachments.Add "C:\OneDrive - Pump\Venders\Mencarelli Pump & Valve\Brochure\Mencarelli Inox Pump Brochure - Wine OPE.pdf", _
olByValue, 1, "Mencarelli Wine Pump Brochure"
'WasteCorp Information
Case 16
myAttachments.Add "C:\OneDrive - Pump\Venders\WasteCorp Pumps\Brochures\WasteCorp Municipal Pumps Brochure.pdf", _
olByValue, 1, "WasteCorp Municipal Brochure"
Case 17
myAttachments.Add "C:\OneDrive - Pump\Venders\WasteCorp Pumps\Brochures\WasteCorp Industrial Pumps Brochure.pdf", _
olByValue, 1, "WasteCorp Municipal Brochure"
Case 18
myAttachments.Add "C:\OneDrive - Pump\Venders\WasteCorp Pumps\Brochures\WasteCorp Double-Disc Brochure.pdf", _
olByValue, 1, "WasteCorp Double-Disc Brochure"
Case 19
myAttachments.Add "C:\OneDrive - Pump\Venders\WasteCorp Pumps\Brochures\WasteCorp Trash Flow Self Priming Brochure.pdf", _
olByValue, 1, "WasteCorp Self-Priming Brochure"
Case 20
myAttachments.Add "C:\OneDrive - Pump\Venders\WasteCorp Pumps\Brochures\WasteCorp Pre-Packaged Lift Stations Brochure.pdf", _
olByValue, 1, "WasteCorp Pre-Packaged Lift Station Brochure"
'BioGill Information
Case 21
myAttachments.Add "C:\OneDrive - Pump\Venders\BioGill\Brochures\BioGill Tower Plus DataSheet.pdf", _
olByValue, 1, "BioGill Tower Plus Datasheet"
Case 22
myAttachments.Add "C:\OneDrive - Pump\Venders\BioGill\Brochures\BioGill Ultra DataSheet.pdf", _
olByValue, 1, "BioGill Ultra Datasheet"
Case 23
myAttachments.Add "C:\OneDrive - Pump\Venders\BioGill\Brochures\20180510BioGIllBreweryWastewaterTreatmentGuideVersion1.3.pdf", _
olByValue, 1, "BioGill Brewery Treatment Guide"
Case 24
myAttachments.Add "C:\OneDrive - Pump\Venders\BioGill\Brochures\20180606FoodBeverageOverview.pdf", _
olByValue, 1, "BioGill Food & Bev Overview"
'ARO Information
Case 25
myAttachments.Add "C:\OneDrive - Pump\Venders\ARO Pumps\Brochures & Flyers\ARO FDA Compliant AOD Pumps OPE.pdf", _
olByValue, 1, "ARO FDA & Food Diaphragm Pumps Brochure"
Case 26
myAttachments.Add "C:\OneDrive - Pump\Venders\Pumps\Brochures & Datasheets\1.5 TC AOD Wine Cart OP.pdf", _
olByValue, 1, "ARO 1.5 Inch Diaphragm Pump Cart Flyer"
Case 27
myAttachments.Add "C:\OneDrive - Pump\Venders\Pumps\Brochures & Datasheets\2.5 TC AOD Wine Cart OP.pdf", _
olByValue, 1, "ARO 2.5 Inch Diaphragm Pump Cart Flyer"
Case 28
myAttachments.Add "C:\OneDrive - Pump\Venders\ARO Pumps\Brochures & Flyers\EXP Metalic Pumps.pdf", _
End Select
End Sub

Function GetCurrentItem() As Object
Dim objApp As Outlook.Application

Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select

Set objApp = Nothing
End Function
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This worked here - I used this for the button click

Code:
Private Sub CommandButton1_Click()
Dim i As Long
Dim lngCount As Long
Dim strPicks As String
  lngCount = 0
  For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) = True Then
      lngCount = lngCount + 1
      If lngCount = 1 Then
        strPicks = ListBox1.List(i)
      Else
        strPicks = strPicks & "," & ListBox1.List(i)
      End If
    End If
  Next i
    lstText = strPicks
'MsgBox lstText

    Unload Me

lbl_Exit:
  Exit Sub
End Sub

these were my additems:
.AddItem "head2.csv"
.AddItem "head3.csv"
.AddItem "head4.csv"

I used the additem text as the select cases - split the array then got each file. If you can get the index #, of the Additem's you can use the case # instead -the command button code sample I had used the names, not the index # and I was too lazy to tweak it.

Code:
Public lstText As String
Public Sub AddOPEAttachments()

Dim objItem As Object
Dim oMail As Outlook.MailItem
Dim myAttachments As Outlook.Attachments

Dim StrArr() As String

If TypeName(Application.ActiveInspector.currentItem) = "MailItem" Then
Set objItem = Application.ActiveInspector.currentItem
End If

Set myAttachments = objItem.Attachments

UserForm1.Show
StrArr = Split(lstText, ",")

For i = LBound(StrArr) To UBound(StrArr)
MsgBox StrArr(i)
Select Case StrArr(i)
Case -1
'oMail.Subject = objItem.Subject
Case "head2.csv"
myAttachments.Add "D:\OneDrive - Cdolive LLC dba Slipstick Systems\0 - Word\head2.csv", _
olByValue, 1, "head2.csv"
' Pump Information
Case "head3.csv"
myAttachments.Add "D:\OneDrive - Cdolive LLC dba Slipstick Systems\0 - Word\head3.csv", _
olByValue, 1, "head3.csv"
Case "head4.csv"
myAttachments.Add "D:\OneDrive - Cdolive LLC dba Slipstick Systems\0 - Word\head4.csv", _
olByValue, 1, "head4.csv"

End Select
Next i

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Ah heck with it... this will use the case # - the additem list needs to be in the same order and you need to make sure the numbers are right. Using the same itemadd list in my other post, my 3 test files are #5, 6, 7

Code:
Private Sub btnOK_Click()
Dim i As Long
Dim lngCount As Long
Dim strPicks As String
  lngCount = 0
  For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) = True Then
      lngCount = lngCount + 1
      If lngCount = 1 Then
        strPicks = i 'ListBox1.List(i)
      Else
        strPicks = strPicks & "," & i
      End If
    End If
  Next i
    lstText = strPicks
MsgBox lstText

    Unload Me

lbl_Exit:
  Exit Sub
End Sub
 

owensnw

Member
Outlook version
Outlook 2019 64-bit
Email Account
Office 365 Exchange
Thank you!! Working perfectly!
Ah heck with it... this will use the case # - the additem list needs to be in the same order and you need to make sure the numbers are right. Using the same itemadd list in my other post, my 3 test files are #5, 6, 7

Code:
Private Sub btnOK_Click()
Dim i As Long
Dim lngCount As Long
Dim strPicks As String
  lngCount = 0
  For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) = True Then
      lngCount = lngCount + 1
      If lngCount = 1 Then
        strPicks = i 'ListBox1.List(i)
      Else
        strPicks = strPicks & "," & i
      End If
    End If
  Next i
    lstText = strPicks
MsgBox lstText

    Unload Me

lbl_Exit:
  Exit Sub
End Sub
 
Similar threads
Thread starter Title Forum Replies Date
S Add VBA save code Using Outlook 0
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
V Modifying the built in forms with VBA Outlook VBA and Custom Forms 4
S Excel VBA and shared calendar issue Outlook VBA and Custom Forms 4
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 2
L VBA unknown character Outlook VBA and Custom Forms 2
G Move tasks up/down todo list by VBA Outlook VBA and Custom Forms 1
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
K Use VBA to find Sender and Recipient from Microsfot 365 Journaled Email Items Outlook VBA and Custom Forms 3
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
C VBA to prompt for Sent folder destination Outlook VBA and Custom Forms 3
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
B Change Font and Font size using VBA Outlook VBA and Custom Forms 9
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
D.Moore VBA script fail after Office 365 update Using Outlook 8
R Limiting length of saved attachment in VBA Outlook VBA and Custom Forms 2
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
C How to use VBA to show only items x days old or more Outlook VBA and Custom Forms 1
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
S Reference Custom Fields with VBA Outlook VBA and Custom Forms 2
PGSystemTester VBA To Change AppointmentItem.BusyStatus From MeetingItem Before Send Using Outlook 0
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
G VBA Macro Calendar Printing Assistant 4
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
B VBA to Collapse Task Folder Groups Outlook VBA and Custom Forms 1
R Expand VBA Permanent Delete Code Outlook VBA and Custom Forms 6
shrydvd vba to secure zip attachments Outlook VBA and Custom Forms 3
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
N VBA to delete duplicates by message-id on common pst for 2 or more emails Outlook VBA and Custom Forms 0
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
A Custom VBA to sort emails into folders Outlook VBA and Custom Forms 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
R VBA for copying sent email to current folder under a shared mailbox Outlook VBA and Custom Forms 17
A Edit subject - and change conversationTopic - using VBA and redemption Outlook VBA and Custom Forms 2
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
N Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
B Clear Offline Items (Mail Folder) via VBA Outlook VBA and Custom Forms 1
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57
F VBA to ensure a code is entered in Subject title Outlook VBA and Custom Forms 1
B Vba to monitor time to respond to emails using a shared mailbox Outlook VBA and Custom Forms 5
N VBA Script to Open highlighted e-mail and Edit Message Outlook VBA and Custom Forms 5
G Outlook VBA and Google Calendar ("Events") Outlook VBA and Custom Forms 1

Similar threads

Top