Excel 向VBA添加附件

Excel 向VBA添加附件,excel,vba,Excel,Vba,这是最新消息。代码工作正常,但一旦我进入路径,它就会停止工作,这让我觉得路径不正确?我不知道还能做些什么,我应该改变路径到它在我的文档或桌面中的位置吗 Sub BLS() Dim wkbReminderList As Workbook Dim wksReminderList As Worksheet Dim lngNumberOfRowsInReminders As Long Dim i As Long Dim strEmail As String, strSubject As String,

这是最新消息。代码工作正常,但一旦我进入路径,它就会停止工作,这让我觉得路径不正确?我不知道还能做些什么,我应该改变路径到它在我的文档或桌面中的位置吗

Sub BLS()
Dim wkbReminderList As Workbook
Dim wksReminderList As Worksheet
Dim lngNumberOfRowsInReminders As Long
Dim i As Long
Dim strEmail As String, strSubject As String, strBody As String
Dim sAttcmnt1 As String, sAttcmnt2 As String, sAttcmnt3 As String
Set wkbReminderList = ActiveWorkbook
Set wksReminderList = ActiveWorkbook.ActiveSheet

lngNumberOfRowsInReminders = _
         wksReminderList.Cells(Rows.Count, "A").End(xlUp).Row

For i = 2 To lngNumberOfRowsInReminders

    If wksReminderList.Cells(i, 7) = "" And _
       wksReminderList.Cells(i, 3) <= Date Then

            strEmail = wksReminderList.Cells(i, 6).Value
            strSubject = "Your BLS Certification is Expiring within 60 Days"
            strBody = "Hello," & vbCrLf & _
      " Your BLS Certification is expiring within 60 days."

        sAttcmnt1 = "C:\Keycodes.pdf"

           If SendAnOutlookEmail(strEmail, _
                      strSubject, _
                      strBody, _
                      sAttcmnt1, _
                      sAttcmnt2, _
                      sAttcmnt3) Then
                wksReminderList.Cells(i, 7) = Date
            End If

    ElseIf wksReminderList.Cells(i, 8) = "" And _
           wksReminderList.Cells(i, 4) <= Date Then

            strEmail = wksReminderList.Cells(i, 6).Value
            strSubject = "BLS is Expiring in 30 Days!!!"
            strBody = "other text here..."
            If SendAnOutlookEmail(strEmail, strSubject, strBody) Then
                wksReminderList.Cells(i, 8) = Date
            End If
    End If

Next i

End Sub

Private Function SendAnOutlookEmail(strAddress As String, _
                                    strSubject As String, _
                                    strBody As String, _
                                    Optional sAtt1 As String, _
                                    Optional sAtt2 As String, _
                                    Optional sAtt3 As String) As Boolean
SendAnOutlookEmail = False

Set OutApp = CreateObject("Outlook.Application")
OutApp.session.Logon "Outlook"
Set OutMail = OutApp.CreateItem(0)
On Error GoTo ErrorOccurred
 With OutMail
    .To = strAddress
    .Subject = strSubject
    .Body = strBody
    If sAtt1 <> "" Then .Attachments.Add = sAtt1
    If sAtt2 <> "" Then .Attachments.Add = sAtt2
    If sAtt3 <> "" Then .Attachments.Add = sAtt3
    .Send
End With
SendAnOutlookEmail = True

Continue:
    On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Exit Function

ErrorOccurred:
    Resume Continue
End Function
Sub-BLS()
Dim wkbReminderList作为工作簿
Dim wksReminderList作为工作表
昏暗的LGNNUMBEROFROWSINREMINERMINERS长度
我想我会坚持多久
Dim strEmail作为字符串,strSubject作为字符串,strBody作为字符串
将尺寸sAttcmnt1设置为字符串,将sAttcmnt2设置为字符串,将sAttcmnt3设置为字符串
设置wkbReminderList=ActiveWorkbook
设置wksReminderList=ActiveWorkbook.ActiveSheet
LGNNumberOfRowsInReminders=_
wksReminderList.Cells(Rows.Count,“A”).End(xlUp).Row
对于i=2到LGNnumberOfRowsInReminders
如果wksReminderList.Cells(i,7)=“和_

wksReminderList.Cells(i,3)这就是你正在尝试的吗?(疲劳且经过测试

子样本()
Dim strEmail作为字符串,strSubject作为字符串,strBody作为字符串
将尺寸sAttcmnt1设置为字符串,将sAttcmnt2设置为字符串,将sAttcmnt3设置为字符串
'
“~~>在此处设置变量值
'
strEmail=”Sid@sid.com"
strSubject=“诸如此类”
strBody=“诸如此类”
sAttcmnt1=“C:\Test.txt”
'
“~~>等等
'
如果发送一封OutlookEmail(strEmail_
主题_
斯特博迪_
SATCMNT1_
卫星MNT2_
sAttcmnt3)然后
'
“~~>做点什么
'
如果结束
端接头
私有函数SendAnOutlookEmail(字符串形式)_
strSubject作为字符串_
作为字符串的strBody_
可选sAtt1作为字符串_
可选sAtt2作为字符串_
可选sAtt3(作为字符串)作为布尔值
'
“~~>代码的其余部分
'
发邮件
.穿
.Subject=strSubject
.车身=车身
如果是sAtt1“”,则为.Attachments.Add sAtt1
如果sAtt2为“”,则.Attachments.Add sAtt2
如果是sAtt3“”,则为.Attachments.Add sAtt3
.发送
以
'
“~~>代码的其余部分
'
端函数

除了前面的代码外,该代码还可以正常工作,但是一旦我添加附件sAtt1“C:\…”的路径,它就会停止工作。一旦我添加路径,您为
sAttcmnt1
设置的确切值是多少。这就是我的路径。使用OutMail.To=strAddress.Subject=strSubject.Body=strBody,如果sAtt1“C:\Keycodes.pdf”,则使用.Attachments.Add=sAtt1,您可以看到子示例(),并在其中设置变量值。这就是你必须设置变量的地方。这是否意味着“它起作用了”:P
Sub Sample()
    Dim strEmail As String, strSubject As String, strBody As String
    Dim sAttcmnt1 As String, sAttcmnt2 As String, sAttcmnt3 As String

    '
    '~~> Set values of variables here
    '
    strEmail = "Sid@sid.com"
    strSubject = "Blah Blah"
    strBody = "Blah Blah"
    sAttcmnt1 = "C:\Test.txt"
    '
    ' ~~> and so on
    '

    If SendAnOutlookEmail(strEmail, _
                          strSubject, _
                          strBody, _
                          sAttcmnt1, _
                          sAttcmnt2, _
                          sAttcmnt3) Then
        '
        '~~> Do Something
        '
    End If
End Sub

Private Function SendAnOutlookEmail(strAddress As String, _
                                    strSubject As String, _
                                    strBody As String, _
                                    Optional sAtt1 As String, _
                                    Optional sAtt2 As String, _
                                    Optional sAtt3 As String) As Boolean
    '
    '~~> Rest of the code
    '
    With OutMail
        .To = strAddress
        .Subject = strSubject
        .Body = strBody
        If sAtt1 <> "" Then .Attachments.Add sAtt1
        If sAtt2 <> "" Then .Attachments.Add sAtt2
        If sAtt3 <> "" Then .Attachments.Add sAtt3
        .Send
    End With

    '
    '~~> Rest of the code
    '
End Function