【问题标题】:Paste cells into Outlook then convert table to text in Excel将单元格粘贴到 Outlook 中,然后将表格转换为 Excel 中的文本
【发布时间】:2015-02-05 22:07:12
【问题描述】:

我正在尝试构建一个宏,该宏从 Excel 电子表格中抓取选定的单元格,将单元格粘贴到新的 Outlook 电子邮件中,然后更改单元格的格式。

具体我想把表格转成文字,然后把字体改成Arial size 10。

下面的代码做了上面的,但是我一直没搞清楚怎么把表格转成文字,然后再改文字字体。

有人可以帮忙吗?

Sub Email_test()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
Set rng = Sheets("Master").Range("A1:B99").SpecialCells(xlCellTypeVisible)
If rng Is Nothing Then
    MsgBox "The selection is not a range or the sheet is protected. " & _
           vbNewLine & "Please correct and try again.", vbOKOnly
    Exit Sub
End If
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
    .To = "User@company.com"
    .CC = ""
    .BCC = ""
    .Subject = "Cells as text "
    .HTMLbody = RangetoHTML(rng)

    ' In place of the following statement, you can use ".Display" to
    ' display the e-mail message.

    .Display
End With
End Sub

【问题讨论】:

    标签: excel text outlook excel-2010 vba


    【解决方案1】:

    这对你有用,而不是 HTMLbody 使用 body 也删除了你的范围到 html 函数

    Sub Email_test()
        Dim rng As Range
        Dim OutApp As Object
        Dim OutMail As Object
        Set rng = Nothing
        Set rng = Sheets("Master").Range("A1:B99").SpecialCells(xlCellTypeVisible)
        If rng Is Nothing Then
            MsgBox "The selection is not a range or the sheet is protected. " & _
                   vbNewLine & "Please correct and try again.", vbOKOnly
            Exit Sub
        End If
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        Dim v As Variant: v = rng.Value
        Dim tempStr As String: tempStr = ""
        For i = LBound(v, 1) To UBound(v, 1)
                For j = LBound(v, 2) To UBound(v, 2)
                    If j = 2 Then
                        tempStr = tempStr & v(i, j) & vbCrLf
                    Else
                        tempStr = tempStr & v(i, j) & " "
                    End If
                Next j
            Next i
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = "User@company.com"
            .CC = ""
            .BCC = ""
            .Subject = "Cells as text "
            .body = tempStr
    
            ' In place of the following statement, you can use ".Display" to
            ' display the e-mail message.
    
            .Display
        End With
    End Sub
    

    如果您对回复满意,请标记为回答

    【讨论】:

    • 谢谢,有帮助。唯一的问题是,现在我已经丢失了单元格中的所有格式。
    • 您说您想将表格转换为文本 :) 。你的意思是你想要一张无边框的桌子吗?
    • 我想将表格转换为文本,但我想保留格式,例如某些区域的粗体和下划线
    【解决方案2】:

    Outlook 对象模型为工作项正文提供了三种主要方式:

    1. Body - 表示 Outlook 项目的明文正文的字符串。

    2. HTMLBody - 表示指定项的 HTML 正文的字符串。

    3. Word editor - 正在显示的消息的 Microsoft Word 文档对象模型。 Inspector 类的 WordEditor 属性从 Word 对象模型返回 Document 类的实例,您可以使用它来设置消息正文。

      您可以在Chapter 17: Working with Item Bodies 中阅读有关所有这些方式的更多信息。选择哪种方式由您决定。

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多