我有以下代码(由Ron de Bruin提供)并试图弄清楚如何自定义它以供我自己使用。
当我运行代码时,什么也没有发生(因为错误处理程序在错误“下标超出范围”时停止宏),
但是,当我换行时:
ActiveWorkbook.EnvelopeVisible = False
至:
ActiveWorkbook.EnvelopeVisible = True
可见的信封允许我进行选择并选择我要发送给谁等。
我想知道为什么它会遇到“超出范围”错误,以及是否可以在事件触发后自动完成该过程而无需我输入内容[它是从 Workbook_Open() 事件中触发的-如果这有什么不同,并且选择是在同一工作簿中的另一个工作表(Worksheet(“ValLog”))中进行的]
我正在运行的代码是:
Private Sub workbook_open()
Dim AWorksheet As Worksheet
Dim Sendrng, rng As Range
Dim answer As Integer
On Error GoTo StopMacro
answer = MsgBox("Do you want to send e-mail notifications of upcoming tours?", vbYesNo)
If answer = vbYes Then
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sendrng = Worksheets("ValLog").Range("B5:K12").Select
With Sendrng
'Select the range you want to mail
Range("B5:K12").Select
' Create the mail and send it
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
.Introduction = "Test Test Test"
With .Item
.To = "myemail@blahblah.com"
.CC = ""
.BCC = ""
.Subject = "Why, Error?"
.Send
End With
End With
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
End With
Else
'Do Nothing
End If
End Sub