如何格式化电子邮件正文

时间:2019-09-15 18:58:27

标签: excel vba outlook

我们运行一个SQL查询并将结果粘贴到sheet1中, 然后将结果数据转存到sheet2,然后通过电子邮件将sheet2数据发送给某人。

我已经为上述任务编写了vba代码,但没有得到以下所需结果:

  • 不需要像下面的屏幕截图中所示的换行符
  • 需要删除电子邮件正文中两个句子之间的空格vbtab
  • 电子邮件格式应为纯文本

是否可以通过VBA代码自动运行SQL查询?如果是,请提出建议。

下面是我的代码

Option Explicit

Sub sendemail_excel()

Sheet2.Cells.Clear
Worksheets("sheet1").Activate

    Range("A1").Activate  

Do Until ActiveCell.Value = ""
    Sheet1.Activate
    ActiveCell.Offset(1, 0).Select
    Range(ActiveCell, ActiveCell.End(xlToRight)).Copy
    Worksheets("Sheet2").Activate
    Range("B" & Rows.Count).End(xlUp).Select
    ActiveCell.Offset(2, 0).PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, Transpose:=True, skipblanks:=True
    'ActiveCell.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, Transpose:=True, skipblanks:=True

   'ActiveCell.Offset(1, 0).Activate
    'Range("B1").Select
    'ActiveCell.End(xlDown).Activate
    'ActiveCell.Offset(2, 0).Activate

    Range("B" & Rows.Count).End(xlUp).Offset(-2, 0).NumberFormat = "M/D/YYYY H:MM:SS AM/PM"
    Range("B" & Rows.Count).End(xlUp).Select
    ActiveCell.Offset(-4, -1).Value = "FEEDFILENAME="
    ActiveCell.Offset(-3, -1).Value = "RECIPIENT="
    ActiveCell.Offset(-2, -1).Value = "PROCESSEDTIME="
    ActiveCell.Offset(-1, -1).Value = "PROCESSSTATUS="
    ActiveCell.Offset(0, -1).Value = "NUMBEROFROWS="
     Worksheets("sheet1").Activate
    Loop

    Columns.AutoFit
    Range("B:B").HorizontalAlignment = xlLeft
    Application.ScreenUpdating = False
    Application.CutCopyMode = False
    Range("B" & Rows.Count).End(xlUp).Select
    ActiveCell.Select^
    Range(Selection, Cells(3, 1)).HorizontalAlignment = xlLeft

    Sheet2.Activate 
Range("B" & Rows.Count).End(xlUp).Select
    ActiveCell.Select

    Range(Selection, Cells(3, 1)).Select
ActiveWorkbook.EnvelopeVisible = True

    With ActiveSheet.MailEnvelope
      .Introduction = "Hi abc Team - This is the confirmation mail regarding the files, which xyz" & " " & Date - 1
      .Item.To = "chinnolamanohar@gmail.com"
      .Item.CC = "chinnolamanohar@gmail.com"
      .Item.Subject = "Confirmation for earnings feed from abc equity research to xyz"
      .Item.Send
   End With

    End Sub

1 个答案:

答案 0 :(得分:0)

您可以直接对数据库运行查询,在线有大量文档。 您的连接字符串将因数据库类型而异-MSSQL,MySQL等。

Accessing SQL Database in Excel-VBA https://www.ptr.co.uk/blog/using-excel-vba-query-sql-server-database