添加列表框项目作为电子邮件附件

我有一个表单,旨在让用户报告错误,并build议对表单进行改进。 我已经准备好了,但仍然遇到添加附件的问题。

Sub Submit() Dim OutApp As Object Dim OutMail As Object Dim Item Dim STR As String, AdminOnly As String, TruncBox As String, STRAttachments As String For Each cCont In Me.MultiPage1.SelectedItem.Controls Select Case TypeName(cCont) Case "TextBox" If cCont.value = "Please enter a short description here." Or _ cCont.value = "Please enter a short description here." Then MsgBox ("Please enter all information.") Exit Sub ElseIf cCont.value = "" Then MsgBox ("Please enter all information.") Exit Sub End If Case "ComboBox" If cCont.value = "" Then MsgBox ("Please enter all information.") Exit Sub ElseIf InStr(cCont.value, "Report") Then TruncBox = "BUG" Else TruncBox = "SUGGESTION" End If End Select Next STR = "{email address redacted}" If RecipientsListBox.ListCount = 0 Then AdminOnly = MsgBox("Only admin will receive updates!", _ vbOKCancel + vbExclamation, "No Users on Watch List") If AdminOnly = vbCancel Then Exit Sub Else STR = STR End If Else For Each Item In RecipientsListBox.List STR = STR & ";" & Item Next Item End If Application.ScreenUpdating = False Set OutApp = CreateObject("Outlook.Application") On Error GoTo cleanup Set OutMail = OutApp.CreateItem(0) On Error Resume Next With OutMail .to = STR Call .Recipients.resolveall .Subject = TruncBox & ": " & ActiveWorkbook.Name & ": " & ShortDescriptionTextBox .Body = LongDescriptionTextBox If AttachmentsListBox.ListCount = 0 Then Else For Each Item In AttachmentsListBox.List STRAttachments = Item .Attachments.Add STRAttachments Next Item End If '.Send 'Or use Display .Display End With On Error GoTo 0 Set OutMail = Nothing cleanup: Set OutApp = Nothing Application.ScreenUpdating = True End Sub 

我已经通过循环遍历AttachmentsListBox控件上的每个项目的各种尝试,并准备好要求帮助。 这个最新的尝试产生了Run-time error '94': Invalid use of Null的行使用STRAttachments = Item在高亮显示的部分返回null。 看看我已经有了什么,并在互联网上比较其他search,我不明白。 我在STR = STR & ";" & Item行中将variablesItem返回给STR STR = STR & ";" & Item上面的STR = STR & ";" & Item ,我已经看到作为附件返回的string的其他例子。 我错过了什么?

所以,这是我过去所发现的一个问题,但是我没有深入研究这个根本原因。 ListBox.List返回一个ListObjects的multidimensional array。 因此,即使您有一列ListBox,列数组有多个列。 当您循环使用For Each循环时,它将尝试访问这些其他列中的值,这只会导致Null值。 尝试使用For循环与计数器,如:

 Private Sub UserFormButton_Click() For i = 0 To Me.ListBox1.ListCount - 1 MsgBox Me.ListBox1.List(i) Next i End Sub