excel 打开工作簿时发送邮件:运行时错误13

zqdjd7g9  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(256)

我正在尝试创建一个宏来发送自动提醒。
我有两个宏:

Sub Auto_Open()
    Dim vResp As Variant, dTime As Date
    vResp = MsgBox("Inviare email ora?", vbYesNo)
    If vResp = 6 Then 'YES
        Call EmailReminder
    ElseIf vResp = 7 Then 'NO
        dTime = CDate(InputBox("Send email at:", , Time + TimeValue("00:00:10")))
        Do Until Time = dTime 'OR = #8:00:00 AM#
            DoEvents
        Loop
        Call EmailReminder
    End If
End Sub
    
Sub EmailReminder()
    Dim oOL As Outlook.Application, oMail As Outlook.MailItem, oNS As Outlook.Namespace
    Dim oMapi As Outlook.MAPIFolder, oExpl As Outlook.Explorer
    Dim sBody As String, dDate As Date
    Dim oWS As Worksheet, r As Long, i As Long, sStart As String
    
    Set oWS = Foglio1
    Set oOL = New Outlook.Application
    Set oExpl = oOL.ActiveExplorer
    
    If TypeName(oExpl) = "Nothing" Then
        Set oNS = oOL.GetNamespace("MAPI")
        Set oMapi = oNS.GetDefaultFolder(olFolderInbox)
        Set oExpl = oMapi.GetExplorer
    End If
    
    With oWS.Range("E1")
        r = .CurrentRegion.Rows.Count
        For i = 1 To r
            dDate = .Cells(i, 1)
            sBody = "Oggi è il compleanno di" & .Cells(i, 2) & dDate & .Cells(i, -4) & " " & .Cells(i, -3) & vbCrLf & "Facciamo i nostri auguri!"
            If Date = dDate Or Date = Int(dDate) Then ' Use INT to eliminate time info
                Set oMail = oOL.CreateItem(oIMailItem)
                With oMail
                    .Recipients.Add "umberto.roselli@openfiber.it" 'Indirizzo ricevente
                    .Subject = "Nuovo compleanno oggi:" & .Cells(i, -4) & " " & .Cells(i, -3) & .Body = sBody:  .Send
                End With
            End If
        Next i
    End With
    MsgBox "Messaggio email inviato correttamente!"
        
End Sub

然而,我在第二个宏上不断地得到错误
运行时间13:类型不匹配
但它没有给予我错误在哪里。

eoigrqb6

eoigrqb61#

菲伊

Private Sub Workbook_Open()
    
    Dim i As Long
    Dim OutApp, OutMail As Object
    Dim strto, strcc, strbcc, strsub, strbody As String
    
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    
    
    For i = 2 To Range("e65536").End(xlUp).Row
        If Cells(i, 8) <> "Y" Then
            If Cells(i, 5) - 7 < Date Then
                
                strto = Cells(i, 7).Value 'email address
                strsub = Cells(i, 1).Value & " " & Cells(i, 2).Value & " compleanno il " & Cells(i, 5).Value 'email subject
                strbody = "Il compleanno di " & Cells(i, 1).Value & " " & Cells(i, 2).Value & " sarà il " & Cells(i, 5).Value & vbNewLine 'email body
                
                With OutMail
                    .To = strto
                    .Subject = strsub
                    .Body = strbody
                    .Send
                   
                                    
                End With
                
                Cells(i, 8) = "Mail Sent " & Now()
                Cells(i, 9) = "Y"
                
            End If
        End If
    Next
    
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

相关问题