用户表单变量到电子邮件

时间:2015-04-18 03:59:41

标签: vba listview variables arguments userform

我有一个Userform,它上面有3个按钮,根据点击需要在电子邮件正文中插入相应的文本,对于这封电子邮件,To,CC,Subject,将从Listview框中获取在Sheet1中,inturn提取存储在Sheet2中的值并将其粘贴到电子邮件的To,CC,Subject中。

当我在buttonclick()命令中粘贴代码时,变量不会从主代码传递到userform代码,在那里它将To,CC和Subject显示为空白。

以下是代码:

Sub Worksheet_Activate()

Dim rngCell     As Range

ListView41.ListItems.Clear

For Each rngCell In Worksheets("MFRs Contacts").Range("A2:A400")
    If Not rngCell = Empty Then
        With ListView41.ListItems.Add(, , rngCell.Value)
            .ListSubItems.Add , , rngCell.Offset(0, 1).Value
            .ListSubItems.Add , , rngCell.Offset(0, 2).Value
        End With
    End If
Next rngCell

End Sub

Sub ListView41_DblClick()

Dim strName     As String
Dim strEmail    As String
Dim strEmail1   As String
Dim OutApp      As Object
Dim OutMail     As Object
Dim Singlepart  As String
Dim SigString   As String
Dim Signature   As String
Dim strbody As String
Dim SigFilename

strName = ListView41.SelectedItem.Text
strEmail = ListView41.SelectedItem.ListSubItems(1).Text
strEmail1 = ListView41.SelectedItem.ListSubItems(2).Text

check = MsgBox("Send e-mail, To : " & strName & " - " & strEmail & "?" & vbNewLine & _
"CC : " & strEmail1, vbYesNo)

If check <> vbYes Then Exit Sub

Singlepart = MsgBox("For Single Part or Multiple Parts ? " & vbNewLine & vbNewLine & _
"Single Part = Yes" & vbNewLine & _
"Multiple Parts = No", vbYesNo)

If Singlepart = vbYes Then

' For Single Part Numbers
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

    strbody = "<H3><B>Dear Customer Ron de Bruin</B></H3>" & _
              "Please visit this website to download the new version.<br>" & _
              "Let me know if you have problems.<br>" & _
              "<A HREF=""http://www.rondebruin.nl/tips.htm"">Ron's Excel Page</A>" & _
              "<br><br><B>Thank you</B>"

'Signature of User
SigString = Environ("appdata") & _
                "\Microsoft\Signatures\Rohith UTAS.htm"

    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If

    On Error Resume Next


Userform1.Show


'With Outlook
         With OutMail
            .Display
            .To = strEmail
            .CC = strEmail1
            .BCC = ""
            .Subject = strName & "_Request for Product Information"
            .HTMLBody = strbody & vbNewLine & Signature
            .Display 'or .Display if you want the user to view e-mail and send it manually
        End With

Else

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

你可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

您需要在表单上访问的变量(我假设strName,strEmail和strEmail1)只有Sub ListView41_DblClick()中的范围。如果你需要在表单中使用它们,你必须将它们作为参数传递(我喜欢的方式)或者给它们全局范围。

UserForm是一个类,因此您可以像任何其他类一样为其提供属性 - 即在UserForm1中:

Private mEmail As String

Public Property Let Email(inputVal As String)

    mEmail = inputVal

End Property

Public Property Get Email() As String

    Email = mEmail

End Property

然后你可以像任何其他对象一样调用它:

Dim nameless_form As UserForm1

Set nameless_form = New UserForm1
nameless_form.Email = strEmail
nameless_form.Show