我正在尝试打开一个Excel文件,选择一个需要作为电子邮件正文发送的范围。我正在使用Excel Sheet的MailEnvelope对象执行此操作。
运行以下vbscript代码片段时出现错误。有时,此代码在注释中提到的行上引发错误“未知的运行时错误”。由于错误的性质不一致,我感到很困惑。
运行代码时,我发现即使我有objBook.EnvelopeVisible = True
也看不到邮件信封。
代码:
set objExcel = CreateObject("Excel.Application")
objExcel.visible = true
objexcel.DisplayAlerts = false
set objBook = objExcel.Workbooks.open(strOutputFilePath)
set objSheet = objBook.Sheets("TestResults")
objSheet.Range("A1:T60").Select
objBook.EnvelopeVisible = True '<-------- Getting the error here
With objSheet.MailEnvelope.Item '<-------- If I comment the line above, I get the same error on this line
.To = strToList
.cc = strCCList
.subject = "Investment Platform - Test Execution Summary - "&strExecEnv&" - " & date
.attachments.add strOutputFilePath
.send
End With
截屏:
我意识到我要打开的Excel工作簿在系统驱动器(C:Drive)中,我没有足够的特权。我将文件移到了我具有完全访问权限的另一个本地驱动器上,并运行了代码。其他人提到的答案也有效。
答案 0 :(得分:2)
这是使用信封的Excel代码示例。
Sub EmAiLtoDave()
'Working in Excel 2002-2013
Dim Sendrng As Range, s As String, msg As String
On Error GoTo StopMacro
Set Sendrng = Range("A3:O23")
Sendrng.Select
With Sendrng
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
' Set the optional introduction field thats adds
' some header text to the email body.
'.Introduction = msg
With .Item
.To = "david.morrison@somewhere.com"
.CC = ""
.BCC = ""
.Subject = "Hi"
.Send
End With
End With
End With
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
End Sub
答案 1 :(得分:0)
如Davesexcel所述,以下内容可以为您提供帮助:
objSheet.Range("A1:T60").Select
With Selection
ActiveWorkbook.EnvelopeVisible = True
End With
我创建了此测试,并且在这里可以正常工作(Excel 2010):
Sub test()
Dim objExcel As Object
Dim objBook As Object
Dim objSheet As Object
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objBook = objExcel.Workbooks.Open("pathtotestfile")
Set objSheet = objBook.Sheets("testtable")
With Selection
objBook.EnvelopeVisible = True
End With
End Sub
您可以使用新的测试文件测试此plz吗?