excel - 如何从 Excel 生成一封电子邮件,其中考虑到选择了多少行

标签 excel vba

我正在尝试从数据输入到电子表格中生成一封电子邮件,以创建工作报价。我们有一份工作 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

非常感谢任何帮助。

最佳答案

请注意以下示例中的几件事...

  • (几乎)never use Select .您的代码是您要求用户选择一组报价以运行宏的一种情况。 Selection虽然只出现在一行上。其余代码仅使用已建立的范围变量offers .
  • 确保您的范围始终为 fully qualified .对你来说,这意味着不使用 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/

    相关文章:

    vba - 如何使用vba仅将单个工作表复制到另一个工作簿

    excel - 如果后两个字符等于这个,那么那个

    excel - 根据多个标准对 2 个表中的数据进行乘法和求和

    Excel:确定是否使用“保存”或“另存为”

    excel - 将具有唯一值的所有行复制到新工作表,包括标题行

    excel - 如何将单个单元格中的数据分离成多列和多行

    recursion - 自适应vba excel函数递归

    sql-server - Microsoft Access 错误 : "Because of your security settings and current security policy, this control is disabled"

    excel - 将 Excel 范围内的超链接传输到 Outlook 电子邮件

    excel - VBA Excel 的正则表达式模式