我正在尝试从数据输入到电子表格中生成一封电子邮件,以创建工作报价。我们有一份工作 list 并将其分配给某人。
目前,使用下面的代码,我可以通过选择包含工作的行并按下命令按钮,为每封电子邮件发送一个报价。
但是,我可能会为某人提供最多 4 件工作,理想情况下,我希望能够编辑此代码以包含所有选定的行。
有没有人有什么建议?
Private Sub Generate_offer()
Dim strFile As String
Dim OutApp As Object
Dim objOutlookMsg As Object
Set OutApp = CreateObject("Outlook.Application")
Set objOutlookMsg = OutApp.CreateItem(0)
With objOutlookMsg
.SentOnBehalfOfName = ""
.to = ""
.Subject = ""
.HTMLBody = "<p style='font-family:arial;font-size:16'> Dear <br/><br/>
[Body of email - CUT]
& "<p style='font-family:arial;font-size:14'><b>Offer</b>: " & Cells(ActiveCell.Row, "C").Value & "<br/>" _
& "<b>Dates</b>: " & Cells(ActiveCell.Row, "L").Value & " - " & Cells(ActiveCell.Row, "M").Value & "<br/>" _
& "<b>Approx. duration</b>: " & Cells(ActiveCell.Row, "P").Value & " weeks" & "<br/>" _
& "<b>Detils</b>: xxxxx - " & Cells(ActiveCell.Row, "F").Value & "; xxxxx - " & Cells(ActiveCell.Row, "G").Value & "; xxxxx - " & Cells(ActiveCell.Row, "H").Value & "<br/><br/>" & vbNewLine _
[Body of email - CUT]
.display
End With
'objOutlookMsg.Send
Set OutApp = Nothing
End Sub
非常感谢任何帮助。
最佳答案
请注意以下示例中的几件事...
Select
.您的代码是您要求用户选择一组报价以运行宏的一种情况。 Selection
虽然只出现在一行上。其余代码仅使用已建立的范围变量offers
. Cells
完全独立,但设置一个完全限定的范围变量(在我的示例中为 offers
)并将其用作所有 Cells
的基础引用。 它看起来像这样:
Private Sub Generate_offer()
Dim strFile As String
Dim OutApp As Object
Dim objOutlookMsg As Object
Set OutApp = CreateObject("Outlook.Application")
Set objOutlookMsg = OutApp.CreateItem(0)
With objOutlookMsg
.SentOnBehalfOfName = ""
.To = ""
.Subject = ""
.HTMLbody = "<p style='font-family:arial;font-size:16'> Dear <br/><br/> "
.HTMLbody = .HTMLbody & "[Body of email - CUT]"
'--- assumes that the active selection is a set of rows,
' each row with unique offer details
Dim offers As Range
Set offers = ActiveSheet.Range.Selection
Dim i As Long
For i = 1 To offers.Rows.Count
.HTMLbody = .HTMLbody & "<p style='font-family:arial;font-size:14'><b>Offer</b>: "
.HTMLbody = .HTMLbody & offers.Cells(i, "C").Value & "<br/>"
.HTMLbody = .HTMLbody & "<b>Dates</b>: " & offers.Cells(i, "L").Value
.HTMLbody = .HTMLbody & " - " & offers.Cells(i, "M").Value & "<br/>"
.HTMLbody = .HTMLbody & "<b>Approx. duration</b>: " & offers.Cells(i, "P").Value
.HTMLbody = .HTMLbody & " weeks" & "<br/>"
.HTMLbody = .HTMLbody & "<b>Details</b>: xxxxx - " & offers.Cells(i, "F").Value
.HTMLbody = .HTMLbody & "; xxxxx - " & offers.Cells(i, "G").Value
.HTMLbody = .HTMLbody & "; xxxxx - " & offers.Cells(i, "H").Value
.HTMLbody = .HTMLbody & "<br/><br/>" & vbNewLine
Next i
.HTMLbody = .HTMLbody & [Body of email - CUT]
.display
End With
'objOutlookMsg.Send
Set OutApp = Nothing
End Sub
关于excel - 如何从 Excel 生成一封电子邮件,其中考虑到选择了多少行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55707888/