[英]VBA runtime error : Why am i getting an error while sending mail through outlook using mailenvelope
[英]Runtime Error while using the Excel Sheet's MailEnvelope object
我正在嘗試打開一個Excel文件,選擇一個我需要作為電子郵件正文發送的范圍。 我正在使用Excel Sheet的MailEnvelope對象執行此操作。
運行以下vbscript代碼段時出現錯誤。 有時,此代碼在注釋中提到的行上引發錯誤“未知的運行時錯誤”。 由於錯誤性質不一致,我感到很困惑。
當我運行代碼時,即使我有objBook.EnvelopeVisible = True
我仍然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)中,該驅動器沒有足夠的權限。 我將文件移到了我具有完全訪問權限的另一個本地驅動器上,並運行了代碼。 其他人提到的答案也有效。
這是使用信封的一個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
如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嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.