【发布时间】:2013-09-10 20:52:36
【问题描述】:
我想在 Outlook 中粘贴一系列单元格。
这是我的代码:
Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
' Only send the visible cells in the selection.
Set rng = Selection.SpecialCells(xlCellTypeVisible)
Set rng = Sheets("Sheet1").RangeToHtml("D4:D12").SpecialCells(xlCellTypeVisible, xlTextValues)
On Error GoTo 0
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)
On Error Resume Next
With OutMail
.To = ThisWorkbook.Sheets("Sheet2").Range("C1").Value
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangeToHtml.rng
' In place of the following statement, you can use ".Display" to
' display the e-mail message.
.Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
我没有收到任何错误,只是没有在 Outlook 中粘贴范围。
我删除了On Error Resume Next。它给了我一个错误
对象不支持此属性或方法。
【问题讨论】:
-
这个
Set rng = Sheets("Sheet1").RangeToHtml("D4:D12").SpecialCells(xlCellTypeVisible, xlTextValues)看起来不对。RangeToHtml(假设这是 MS 网站上的函数)返回一个字符串,因此您不能在该字符串上调用SpecialCells。去掉那个On Error Resume Next,你会看到错误。 -
就像@TimWilliams 所说的删除
On Error Resume Next,除非你有非常具体的理由来包含它。 -
感谢您的回复 Tim 和 Enderland,我已经删除了 On Error Resume Next,就像您说的那样,它给了我一个错误,即 Object 不支持此属性或方法。你有什么办法可以摆脱这个错误吗?
-
那么错误会将你带到哪一行?