使用用户表单在subs之间传递变量

时间:2014-07-03 15:32:49

标签: excel-vba vba excel

我有一个模仿电子邮件表单(To,CC,附件,正文,主题等)的用户表单,并添加了一个填充了名称列表的列表框。用户选择一个名称,并使用相应的电子邮件地址填充“收件人:”列表框。接下来弹出FileDialogOpen,用户选择附件(附件框popluates,其中包含所选文件的名称)。这是我变得棘手的地方。

选择附件后,用户可以填写“主题”和“正文”,然后单击调用SendEmail子的“确定”按钮,但是由于该子目录(GetFiles)已经完成,因此无法传递实际的附件路径。如何存储稍后为SendEmail子进行调用的文件路径?这是我已经拥有的片段。

Sub GetFiles() 'Multi File picker dialog box

Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vSelectedItems As Variant

Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = True
GetFile = Application.FileDialog(msoFileDialogOpen).Show

If GetFile <> 0 Then
    For i = 1 To Application.FileDialog(msoFileDialogOpen).SelectedItems.Count
        strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(i)
    Next i
End If
With fd
For Each vSelectedItems In .SelectedItems
    Items = vrtSelectedItems
Me.AttachBox.AddItem vSelectedItems

Next vSelectedItems
End With
用户验证了要通过电子邮件发送的信息后,

和附加到OK按钮的SendEmail是正确的。

Private Sub OKButton_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim sEmail As Variant
Dim myArray As Variant

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

myArray = ListBox1.List(ListBox1.ListIndex, 0) 'retrieving name selected and adding          email addresses
Set found = Cells.Find(What:=myArray, After:=ActiveCell, LookIn:=xlFormulas, Lookat:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False)

Set sEmail = Range(found.Offset(0, 1), found.End(xlToRight))
sEmail.Copy

sRecipient = ""
For Each Item In sEmail
       sRecipient = sRecipient & ";" & Item.Value
Next

On Error Resume Next
With OutMail
    .To = sRecipient
    .CC = CC.Value
    .BCC = ""
    '.FROM = ""
    .Subject = Subject.Value
    .Body = Body.Value
     For Each vSelectedItems In GetFile 'no passing of file paths :(
        .Attachments.Add Item
     Next vSelectedItems
    .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

Range("A1").Activate
Call CancelButton_Click
End Sub

答案可能是盯着我,但我不是专家也不是新人。提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试在GetFiles子项之外声明vSelectedItems变量。这将使变量在模块级别可用,因此OKButton_Click()子可以访问它。

Private vSelectedItems as Variant

Sub GetFiles()
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = True
    GetFile = Application.FileDialog(msoFileDialogOpen).Show

    If GetFile <> 0 Then
        ReDim vSelectedItems(Application.FileDialog(msoFileDialogOpen).SelectedItems.Count)
        For Each Item In Application.FileDialog(msoFileDialogOpen).SelectedItems
            vSelectedItems(i) = Item
            Me.AttachBox.AddItem vSelectedItems(i)
            i = i + 1
        Next Item
    End If

End Sub

Private Sub OKButton_Click()

    <... code ...>

    .Body = Body.Value
    For Each Item In vSelectedItems
        .Attachments.Add Item
    Next Item
    .Send

    <... code ...>

End Sub
相关问题