通过电子邮件发送 Excel/Outlook 中选定的范围
E-mailing selected Range in Excel/Outlook
我有以下代码(由 Ron de Bruin 提供),我正在尝试弄清楚如何自定义它以供我自己使用。
当我 运行 代码时,没有任何反应(因为错误处理程序在错误 "Subscript out of range" 时停止宏),
但是当我更改行时:
ActiveWorkbook.EnvelopeVisible = False
至:
ActiveWorkbook.EnvelopeVisible = True
可见的信封允许我进行选择并选择我要发送给谁等。
我想知道为什么它会遇到 "out of range" 错误,以及是否可以让该过程自动完成而无需我在事件触发后输入内容[它是从一个Workbook_Open() 事件 - 如果这有任何区别,并且正在同一工作簿内的另一个工作表(工作表("ValLog"))中进行选择]
我运行ning的密码是:
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
首先,您没有将 SendRange 声明为一个范围。该行:
Dim SendRange, rng As Range
将 SendRange 声明为变体,将 rng 声明为范围。将其更改为:
Dim SendRange As Range, rng As Range
二、换行:
Set Sendrng = Worksheets("ValLog").Range("B5:K12").Select
至:
Set Sendrng = Worksheets("ValLog").Range("B5:K12")
您试图在此处设置范围,而不是select。
然后,更改:
Range("B5:K12").Select
至:
.Select
你在代码中实际做的是select在活动sheet中设置范围(因为你没有在范围之前定义sheet)。通过将其放入 "With" 语句中,每个带有“.”的语句在它将继承 with 语句中的术语之前。因此,对于“.Select”,您实际上是 运行 "Sendrng.Select"
我有以下代码(由 Ron de Bruin 提供),我正在尝试弄清楚如何自定义它以供我自己使用。
当我 运行 代码时,没有任何反应(因为错误处理程序在错误 "Subscript out of range" 时停止宏),
但是当我更改行时:
ActiveWorkbook.EnvelopeVisible = False
至:
ActiveWorkbook.EnvelopeVisible = True
可见的信封允许我进行选择并选择我要发送给谁等。
我想知道为什么它会遇到 "out of range" 错误,以及是否可以让该过程自动完成而无需我在事件触发后输入内容[它是从一个Workbook_Open() 事件 - 如果这有任何区别,并且正在同一工作簿内的另一个工作表(工作表("ValLog"))中进行选择]
我运行ning的密码是:
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
首先,您没有将 SendRange 声明为一个范围。该行:
Dim SendRange, rng As Range
将 SendRange 声明为变体,将 rng 声明为范围。将其更改为:
Dim SendRange As Range, rng As Range
二、换行:
Set Sendrng = Worksheets("ValLog").Range("B5:K12").Select
至:
Set Sendrng = Worksheets("ValLog").Range("B5:K12")
您试图在此处设置范围,而不是select。
然后,更改:
Range("B5:K12").Select
至:
.Select
你在代码中实际做的是select在活动sheet中设置范围(因为你没有在范围之前定义sheet)。通过将其放入 "With" 语句中,每个带有“.”的语句在它将继承 with 语句中的术语之前。因此,对于“.Select”,您实际上是 运行 "Sendrng.Select"