簡體   English   中英

用戶表單變量到電子郵件

[英]Userform variables to E-mail

我有一個用戶窗體,上面有3個按鈕,基於單擊,需要在電子郵件正文中插入相應的文本,對於此電子郵件,“收件人”,“抄送”,“主題”將從“ Sheet1”的“列表視圖”框中獲取。依次提取存儲在Sheet2中的值並將其粘貼到電子郵件的“收件人”,“抄送”和“主題”中。

當我將代碼粘貼到buttonclick()命令中時,變量不會從主代碼傳遞到用戶窗體代碼,在該窗體中,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

你能幫我這個忙嗎?

您需要在表單上訪問的變量(假設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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM