用户表单变量到电子邮件

Userform variables to E-mail

我有一个用户表单,上面有 3 个按钮,根据点击需要在电子邮件正文中插入相应的文本,对于这封电子邮件,收件人、抄送、主题将从Sheet1 中的列表视图框依次提取 Sheet2 中存储的值并将其粘贴到电子邮件的“收件人”、“抄送”、“主题”中。

当我将代码粘贴到 buttonclick () 命令中时,变量没有从主代码传递到用户窗体代码,其中它将收件人、抄送和主题显示为空白。

代码如下:

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

你能帮我解决这个问题吗?

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

用户窗体是一个 class,因此您可以像任何其他 class 一样为其赋予属性 - 即在用户窗体 1 中:

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