我有一个简单的功能来选择一个固定范围并准备电子邮件,它可以工作......但只有在第二次运行该功能之后。这个问题在我打开 Excel 电子表格后立即发生,然后我会“结束”脚本并再次运行它,然后它就像一个魅力一样工作。
非常感谢您的帮助,非常想了解错误发生的原因。
错误:运行时错误 1004:工作表类的选择方法失败。
在调试时,“.Parent.Select”行会从下面的脚本中突出显示。
Sub Select_Range_now()
Dim Sendrng As Range
Dim EndOfLine As Integer
EndOfLine = Find_First() - 1
Set Sendrng = Worksheets("Output").Range("B1:I" & EndOfLine)
ActiveWorkbook.EnvelopeVisible = True
With Sendrng
.Parent.Select
.Select
With .Parent.MailEnvelope
With .Item
.SentOnBehalfOfName = "groupemail@someemail.com"
.To = "someothergroupemail@someemail.com"
.CC = ""
.Subject = "Report"
End With
End With
End With
End Sub
编辑:新发现:
单击“邮件收件人”选项时,我得到这个 msgbox:msgbox 对话框
电子邮件:您可以将整个工作簿作为电子邮件的附件发送,也可以将当前工作表作为电子邮件的正文发送。
- 将整个工作簿作为附件发送
- 将当前工作表作为消息正文发送
再次单击该按钮将不会再次提示,并且脚本会立即运行。我猜在第一次运行时似乎无法处理这个对话框,或者什么!
如果有人需要知道 Find_First() 函数是什么,它用于查找文本 ENDOFLINE 以便我可以计算我的选择范围:
Function Find_First() As String
Dim FindString As String
Dim Rng As Range
FindString = "ENDOFLINE"
With Sheets("Output").Range("A:I")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
'Application.Goto Rng, True
'MsgBox "row number: " & Rng.Row
Find_First = Rng.Row
Else
'MsgBox "Nothing found"
End If
End With
End Function