【问题标题】:Formatting a table using VBA that is being copied to an email使用正在复制到电子邮件的 VBA 格式化表格
【发布时间】:2021-04-27 23:55:47
【问题描述】:

我已经为这个问题苦苦挣扎了一段时间,如果能提供任何帮助,我将不胜感激。所以我有从我拥有的 excel 文件生成电子邮件的代码。问题是当电子邮件粘贴在表格上时格式不正确。我附上了输出的截图,代码如下。

Sub Send_Email()

'Updated by Extendoffice 20200119
    Dim xRg As Range
    Dim I, J As Long
    Dim xAddress As String
    Dim xEmailBody As String
    Dim xMailOut As Outlook.MailItem
    Dim xOutApp As Outlook.Application
    On Error Resume Next
    xAddress = ActiveWindow.RangeSelection.Address
    Set xRg = Range("A9:E32")
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailOut = xOutApp.CreateItem(olMailItem)
    For I = 1 To xRg.Rows.Count
        For J = 1 To xRg.Columns.Count
            xEmailBody = xEmailBody & "  " & xRg.Cells(I, J).Value
        Next
        xEmailBody = xEmailBody & vbNewLine
    Next
    xEmailBody = "" & vbLf & vbLf & "" & vbLf & vbLf & xEmailBody & vbNewLine
    With xMailOut
        .Subject = Worksheets("TDN Generator").Range("A6").Value
        .To = ""
        .Body = xEmailBody
        .Display
        '.Send
    End With
    Set xMailOut = Nothing
    Set xOutApp = Nothing
    Application.ScreenUpdating = True
End Sub

这就是它粘贴到电子邮件中的方式:

  Hello, 
   
  Check it out:        
          
  Trade 2        
  Trade Type  Grant Number  Security Type  Shares Sold  Shares Exercised 
  Sell To Cover  12345  Restricted Stock  200  
  Sell To Cover  12346  Restricted Stock  220  
  Sell To Cover  12347  Restricted Stock  240  
  Sell To Cover  12348  Restricted Stock  260  
  Sell To Cover  12349  Restricted Stock  280  

我希望他们都在他们所说的列中正确对齐。

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    您可以根据需要使用空格函数向左或向右填充值。

    Option Explicit
    
    Public Sub Test()
    
        Dim colwidth(1 To 5) As Integer
        Dim rg As Range
        Dim row As Range
        Dim col As Integer
        Dim val As String
        Dim strout As String
        
        ' adjust the column widths as required
        ' you will get an error if the value is
        ' wider than the column width
        colwidth(1) = 13
        colwidth(2) = 12
        colwidth(3) = 16
        colwidth(4) = 12
        colwidth(5) = 16
        
        ' function is in the worksheet module for convenience
        With Me
        
            Set rg = .Range("A1:E6")
            
            For Each row In rg.Rows
            
                ' pad left
                col = 1
                val = row.Cells(1, col)
                strout = val & Space(colwidth(col) - Len(val) + 1)
            
                ' pad right
                col = 2
                val = row.Cells(1, col)
                strout = strout & Space(colwidth(col) - Len(val)) & val & Space(1)
            
                ' pad left
                col = 3
                val = row.Cells(1, col)
                strout = strout & val & Space(colwidth(col) - Len(val) + 1)
            
                ' pad right
                col = 4
                val = row.Cells(1, col)
                strout = strout & Space(colwidth(col) - Len(val)) & val & Space(1)
            
                ' pad left
                col = 5
                val = row.Cells(1, col)
                strout = strout & val & Space(colwidth(col) - Len(val) + 1)
                
                Debug.Print strout
                
            Next row
        
        End With
        
    End Sub
    

    数据范围

    输出

    Trade Type    Grant Number Security Type     Shares Sold Shares Exercised 
    Sell To Cover        12345 Restricted Stock          200                  
    Sell To Cover        12346 Restricted Stock          220                  
    Sell To Cover        12347 Restricted Stock          240                  
    Sell To Cover        12348 Restricted Stock          260                  
    Sell To Cover        12349 Restricted Stock          280                  
    

    当然,这仅适用于固定间距字体。如果邮件客户端支持,最好将数据写成 html 表格。

    【讨论】:

      【解决方案2】:

      使用 HTML 格式:

      Sub Send_Email()
      
          Dim xRg As Range
          Dim I, J As Long
          Dim xAddress As String
          Dim xEmailBody As String
          Dim xMailOut As Outlook.MailItem
          Dim xOutApp As Outlook.Application
      
          On Error Resume Next
          
          Set xRg = Range("A9:E32")
          xEmailBody = "Take a look:<br><br>" & HtmlTable(xRg)
          
          Set xOutApp = CreateObject("Outlook.Application")
          Set xMailOut = xOutApp.CreateItem(olMailItem)
          
          With xMailOut
              .Subject = Worksheets("TDN Generator").Range("A6").Value
              .To = ""
              .HTMLBody = xEmailBody
              .Display
              '.Send
          End With
          Set xMailOut = Nothing
          Set xOutApp = Nothing
          Application.ScreenUpdating = True
      End Sub
      
      Function HtmlTable(rng As Range) As String
          Dim s As String, rw As Range, c As Range
          s = "<table border=1>"
          For Each rw In rng.Rows
              s = s & "<tr>"
              For Each c In rw.Cells
                  s = s & "<td>" & c.Value & "</td>"
              Next c
              s = s & "</tr>"
          Next rw
          HtmlTable = s & "</table>"
      End Function
      

      【讨论】:

      • 嗨蒂姆,感谢您的帮助。我想知道如果表格中的行数波动,这种 html 格式是否会起作用?另外,我会在哪里将此代码插入现有代码?提前谢谢你。
      • 表中包含的内容仅取决于传递给函数的范围:您可以传递您想要的任何范围(在大小方面的合理范围内) 至于集成 - 如上所示?这几乎就是您的问题,但表生成是在一个单独的函数中。
      • 嗨,蒂姆,这非常有帮助,让我走上了正轨。我想知道两件事。首先,是否可以用两个不同的范围来做到这一点?因此,例如,有时会有一个额外的列需要包含在该表中,如“其他”。第二个可能变化的因素是,如果商品以不同的价格出售,我想提供更多详细信息,因此在底部我会包括日期和编号。有时这不需要包括在内,因为它们都是相同的价格。不确定这是否要求太多。不过我真的很感激。
      • 最后,是否可以让表格看起来像我最初截屏的那样?
      • 几乎anything 都是可能的,但是您想做的每一件事都需要编码,因此您需要尝试理解逻辑以及它们如何组合在一起。如果您想为邮件添加样式,那么您需要了解一点 CSS 以及如何将其与 HTML 一起使用。这本身可能是一个很大的话题,在这里不容易涵盖。如果您有具体的后续问题,则可能需要发布一个包含确切要求的新帖子。
      【解决方案3】:

      你试过粘贴特殊命令吗?

      通常是 CTRL+ALT+V 或 ALT+E+S 打开粘贴专用对话框。

      【讨论】:

      • 我无法使用特殊粘贴。不确定这是否只是我的做法,或者这是否不适用于我的特定问题。
      • 这不是问题的答案,它与 VBA 代码有关。如果您拥有评论特权,那么它可能会对原始问题做出很好的评论,而此时您还没有。保留此空间的唯一目的是回答上述问题。
      猜你喜欢
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 2023-03-28
      • 2018-06-23
      • 2011-06-02
      • 2020-05-09
      • 1970-01-01
      相关资源
      最近更新 更多