如何使用vba Excel宏为Outlook电子邮件添加或删除文本行之间的空格?

时间:2019-05-08 22:09:14

标签: excel vba outlook

我正在编写一个宏来创建Outlook电子邮件,但我的段落间距存在问题。

我尝试使用vbNewLine以及“”,但都无法创建空行。

此外,在我的for循环创建的句子的每个项目符号点之后都有一个很大的空间,我对于如何删除该空间一无所知。它不是一个空行,而是将句子之前和之后的间距(从“段落”菜单中查看)设置为“自动”,我希望两者都为“ 0”。

Sub Email_Budget()

   Dim objOutlook As Object
   Set objOutlook = CreateObject("Outlook.Application")

   Dim objEmail As Object
   Set objEmail = objOutlook.CreateItem(olMailItem)

   Dim CaseCount As Long
   CaseCount = WorksheetFunction.CountA(Range("B6:B500"))
   'Debug.Print CaseCount

   Dim i As Integer

   With objEmail
      .To = "abc@xyz.com"
      .Subject = "TEST1: May 2019 Budget"
      .HTMLBody = "Karen,"
      .HTMLBody = .HTMLBody & vbNewLine
      .HTMLBody = .HTMLBody & "The potential " & MonthName(Month(ActiveSheet.Range("A2"))) & " invoices are below."
      .HTMLBody = .HTMLBody & vbNewLine
      For i = 1 To CaseCount
        If ActiveSheet.Cells(i + 5, 4).Value = "Yes" Then
            .HTMLBody = .HTMLBody & "<ul style='list-style-type:disc;'>" & "<li>" & ActiveSheet.Cells(i + 5, 2).Value & " - " & Format(ActiveSheet.Cells(i + 5, 6).Value, "Currency") & " (" & Format(ActiveSheet.Cells(i + 5, 8).Value, "Currency") & " without budget or invoicing)." & "</li>" & "<ul style = 'list-style-type:circle;'>" & "<li>" & "Last billed " & ActiveSheet.Cells(i + 5, 10) & "." & "</li>" & "</ul>" & "</ul>"
        End If
      Next i
      .HTMLBody = .HTMLBody & vbNewLine
      .HTMLBody = .HTMLBody & vbNewLine
      .HTMLBody = .HTMLBody & "Thank you,"
      .HTMLBody = .HTMLBody & vbNewLine
      .HTMLBody = .HTMLBody & "Kurt"
      .Display
   End With
End Sub

2 个答案:

答案 0 :(得分:1)

是否使用换行符(br)?

Line 1<br>
Line 2

答案 1 :(得分:0)

这应该可以满足您的需求:

Sub Email_Budget()

   Dim objOutlook As Object
   Set objOutlook = CreateObject("Outlook.Application")

   Dim objEmail As Object
   Set objEmail = objOutlook.CreateItem(olMailItem)

   Dim CaseCount As Long
   CaseCount = WorksheetFunction.CountA(Range("B6:B500"))
   'Debug.Print CaseCount

   Dim i As Integer

   With objEmail
      .To = "abc@xyz.com"
      .Subject = "TEST1: May 2019 Budget"
      .HTMLBody = "Karen,<br><br>"
      .HTMLBody = .HTMLBody & "The potential " & MonthName(Month(ActiveSheet.Range("A2"))) & " invoices are below.<br>"
      For i = 1 To CaseCount
        If ActiveSheet.Cells(i + 5, 4).Value = "Yes" Then
            .HTMLBody = .HTMLBody & Application.Trim("<ul style='list-style-type:disc;'><li style=""Margin: 0;"">" & ActiveSheet.Cells(i + 5, 2).Value & " - " & Format(ActiveSheet.Cells(i + 5, 6).Value, "Currency") & " (" & Format(ActiveSheet.Cells(i + 5, 8).Value, "Currency") & " without budget or invoicing).</li><ul style = 'list-style-type:circle;'><li style=""Margin: 0;"">Last billed " & ActiveSheet.Cells(i + 5, 10) & ".</li></ul></ul>")
        End If
      Next i
      .HTMLBody = .HTMLBody & "<br>Thank you,<br>Kurt"
      .Display
   End With
End Sub