我将 VBA 代码附加到 Excel 按钮。按下按钮时,将出现 Outlook 邮箱,准备发送邮件。
工作簿未附在邮件中。我觉得
.Attachments.Add
有问题。
Dim xRg As Range
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("D7"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value > 200 Then
Call Mail_small_Text_Outlook
End If
End Sub
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"
On Error Resume Next
With xOutMail
.To = "Email Address"
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.Body = xMailBody
.Attachments.Add "W\Desktop\Files\Workbook1
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
下面这行代码不完整:
.Attachments.Add "W\Desktop\Files\Workbook1
Add
类的Attachments
方法在Attachments
集合中创建一个新附件。附件的来源可以是一个文件(由带文件名的完整文件系统路径表示),例如:
.Attachments.Add "C:\Test.xslx", olByValue, 1, "Test"
我还建议指定本地文件路径。