[VBA]使用mailto動態組成郵件內容
之前有同事在團購,練習一下VBA
程式架構
工作表1(商品型錄)
Option Explicit
Private Sub cmd_ClearOrder_Click()
Range("A4").Select
Selection.ClearContents
Range("A3:G3").Select
Selection.ClearContents
Label1.Visible = False
End Sub
Private Sub cmd_SendOrders_Click()
If CStr(Range("A3").Value) = "" Then
MsgBox "請輸入姓名!"
Exit Sub
End If
If CStr(Range("H3").Value) = "0" Then
MsgBox "請輸入正確數量!"
Exit Sub
End If
Dim myRange As Range
Dim myHyps As Hyperlinks
Set myRange = Range("A4")
With myRange
Set myHyps = .Hyperlinks
myHyps.Delete
myHyps.Add Anchor:=myRange, _
Address:=SendingMail.取得mailto內容(Range("K1").Value, 取得訂單內容), _
TextToDisplay:="訂單通知_" & DateTime.Now
End With
myRange.Activate
Label1.Visible = True
End Sub
Private Function 取得訂單內容()
Dim counter As Integer
Dim orderContext As String
'mailto的斷行符號用 %0d%0a
For counter = 0 To 8
orderContext = orderContext & Range("A2").Offset(0, counter).Value & _
":" & Range("A3").Offset(0, counter).Value & "%0d%0a"
Next
取得訂單內容 = orderContext
End Function
模組(SendingMail)
Option Explicit
Function 取得mailto內容(ByVal reciever As String, ByVal bodyContent As String)
取得mailto內容 = "mailto:" & reciever & " ?subject=訂單通知" & DateTime.Now & "&body=" & bodyContent
End Function
執行畫面
輸入完畢後點[產生訂單通知]
點選訂單通知內容即可寄信
郵件內容
重設請按[清除訂單內容]
參考資料