将单元格粘贴到Outlook中,然后将表格转换为Excel中的文本

时间:2015-02-05 22:07:12

标签: excel excel-vba text outlook excel-2010 vba

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

具体来说,我想将表格转换为文本,然后将字体更改为Arial大小为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

2 个答案:

答案 0 :(得分:0)

这对你有用,而不是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

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

答案 1 :(得分:0)

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

  1. Body - 表示Outlook项目的明文正文的字符串。
  2. HTMLBody - 表示指定项目的HTML正文的字符串。
  3. Word editor - 正在显示的消息的Microsoft Word文档对象模型。 Inspector类的WordEditor属性从Word对象模型返回Document类的实例,您可以使用它来设置消息体。

    您可以在Chapter 17: Working with Item Bodies中详细了解所有这些方式。我们由您决定选择哪种方式。