使用VBA将所有可见(格式化文本)从Excel复制到Outlook?

时间:2014-03-05 13:36:48

标签: excel vba outlook

使用VBA将所有可见(格式化文本)从Excel复制到Outlook?

请在下面找到代码,通过outlook发送电子邮件。但是,我想使用代码发送包含我在下面复制的选项的电子邮件。 我不希望创建一个表格作为HTML,而只是复制所有可见的?

Sub EmailRep()


Dim Mailbody As Range

Application.DisplayAlerts = False

Dim Outlook As Outlook.Application
Set Outlook = CreateObject("Outlook.Application")
Dim outmail As MailItem
Set outmail = Outlook.CreateItem(0)

Set Mailbody = ActiveWorkbook.Worksheets("Dashboard").Range("A1:F30")
Mailbody.Copy

With outmail

.To = "abc@xyz.com"
.Subject = "All Open"
.Body = "This is Test Email"
.Display
.Send
End With


Set Outlook = Nothing
Set outmail = Nothing
Set Mailbody = Nothing


End Sub

1 个答案:

答案 0 :(得分:-1)

如果我理解正确改变你的行:

Set Mailbody = ActiveWorkbook.Worksheets("Dashboard").Range("A1:F30")

Set Mailbody = ActiveWorkbook.Worksheets("Dashboard").Range("A1:F30").SpecialCells(xlCellTypeVisible)

虽然您的代码中没有将范围放入电子邮件正文中。起初以为你手工粘贴了这个范围,但后来我注意到你的代码中有.Send,它会在你有机会粘贴之前发送电子邮件。

无论哪种方式,上述方法都只会复制可见范围。

如果您对通过电子邮件发送电子邮件的快速方式感兴趣而无需复制,则以下内容非常短而且很好:

Sub EmailRep()
With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
End With

Range("A1:F30").SpecialCells(xlCellTypeVisible).Select

ActiveWorkbook.EnvelopeVisible = True

With ActiveSheet.MailEnvelope
    .Introduction = "This is Test Email"
    .Item.To = "abc@xyz.com"
    .Item.Subject = "All Open"
    .Item.Send
End With

ActiveWorkbook.EnvelopeVisible = False

With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
End With
End Sub