簡體   English   中英

如何使用 Visual Basic 從 Excel 添加一系列單元格作為自定義 SMTP 電子郵件的正文?

[英]How can I add a Range of cells as the body of a custom SMTP email from Excel using Visual Basic?

單擊 Excel 中的按鈕后,我將在工作表上發送一系列單元格作為電子郵件正文。 電子郵件發送正確,但我無法弄清楚如何將實際的單元格范圍添加為正文。 這是我目前在 Excel 的 Visual Basic 中使用的代碼以及Windows 2000 庫Micrsoft CDO

Sub Email_Figures_Click()
Dim myMail As CDO.Message

Set myMail = New CDO.Message

myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "HIDDEN"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "HIDDEN"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "HIDDEN"
myMail.Configuration.Fields.Update

With myMail
.Subject = "HIDDEN"
.From = "HIDDEN"
.To = "HIDDEN"

    Dim myRng As Range
    Set myRng = Nothing

    'Only the visible cells in the selection
    Set myRng = Sheets("Monthly Figures").Range("A1:B29").SpecialCells(xlCellTypeVisible)

    If myRng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub

    End If

.HTMLBody = myRng
End With
On Error Resume Next
myMail.Send
MsgBox ("Email has been sent!")
Set myMail = Nothing
End Sub

任何帶有隱藏字樣的東西都是為了保護客戶。

任何幫助表示贊賞。

如果我理解正確,您不能簡單地將范圍分配給.HTMLBody 您可能必須“手動”構建 HTML-String 並具有類似於

html_text = "<table>" & vbCrLf & "<tr>"
For Each Row In myrng.Rows
    For Each cell In Row.Cells
        html_text = html_text & "<td>" & cell.Text & "</td>"
    Next cell
    html_text = html_text & "</tr>" & vbCrLf
Next Row
html_text = html_text & "</table>"
.HTMLBody=html_text

更換您的線路

.HTMLBody = myRng

.

暫無
暫無

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

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