运行时错误424:对象未定义" - 使用Outlook 2013发送电子邮件时

时间:2017-08-16 15:20:03

标签: vba excel-vba outlook-2013 excel

我使用下面的代码通过outlook发送多封电子邮件,当我使用Excel 2007和Outlook 2007时,此代码工作正常但是当我尝试在Excel 2013和Outlook 2013中运行相同的代码时,它会抛出错误&#34 ;运行时错误424:对象未定义"在下面的代码中:

Set Doc = olMail.GetInspector.WordEditor

有人可以查看以下编码,如果我想在2013版本中使用相同的宏,请告诉我需要更改哪些内容?

Sub Msmail()

Dim otlApp As Object  
Dim olMail As Object


Set otlApp = CreateObject("Outlook.Application")   
Set olMail = otlApp.CreateItem(olMailItem)

 Set mainWB = ActiveWorkbook

    Worksheets("Mail").Select
    ActiveSheet.Calculate

    Total_Site = Range("Total_Site")
    For Site_Count = 1 To Total_Site

    Application.StatusBar = False
    ActiveSheet.Calculate
    Range("Site_Count") = Site_Count
    ActiveSheet.Calculate

    If Range("Send_Email") = "Y" Then

    Set mainWB = ActiveWorkbook
    Set olMail = otlApp.CreateItem(olMailItem)
    Set Doc = olMail.GetInspector.WordEditor   '<~ ERROR 424 HERE

    SendID = mainWB.Sheets("Mail").Range("To_List").Value
    CCID = mainWB.Sheets("Mail").Range("Cc_List").Value
    Subject = mainWB.Sheets("Mail").Range("Subject_Line").Value
    Body = mainWB.Sheets("Summary").Range("Mail_Body").Value
    AttachFile = mainWB.Sheets("Mail").Range("StrPath").Value
    StrPath = ActiveSheet.Range("StrPath").Value

    With olMail

        .To = SendID
        If CCID <> 0 Then
          .CC = CCID
        End If
        .Subject = Subject

        mainWB.Sheets("Summary").Range("Mail_Body").Copy

        Set WrdRng = Doc.Range
       .Display
       WrdRng.Paste

           'StrPath = Range("StrPath").Value

           StrFile = Range("StrFile").Value & "*.*"

            StrFile = Range("StrFile").Value
           .Attachments.Add StrPath & "\" & StrFile

            .Send

    End With
    End If
    Next Site_Count

End Sub

1 个答案:

答案 0 :(得分:0)

好的 - 所以当前的代码似乎存在很多问题;你有重复的代码和非尺寸变量。我试图压缩代码,添加直接引用,这将有希望使它更具可读性(并且应该)工作,但它没有经过测试......我没有相关的范围工作表,没有数据来测试它

Sub msMail()

    Dim olApp As Object: Set olApp = CreateObject("Outlook.Application")
    Dim olMail As Object

    Dim Total_Site As Range, Doc As Variant, WrdRng As Range
    Dim StrPath As String, StrFile As String, Site_Count As Long

    Dim wbMain As Workbook: Set wbMain = ThisWorkbook
    Dim wsMail As Worksheet: Set wsMail = wbMain.Worksheets("Mail")
    Dim wsSumm As Worksheet: Set wsSumm = wbMain.Worksheets("Summary")

    With wsMail
        .Calculate ' Not sure why this is included...
        Set Total_Site = Range("Total_Site")
        For Site_Count = 1 To Total_Site.Count
            ''Application.StatusBar = False
            Range("Site_Count") = Site_Count
            If Range("Send_Email") = "Y" Then
                Set olMail = olApp.CreateItem(olMailItem)
                Set Doc = olMail.GetInspector.WordEditor
                If Not Doc Is Nothing Then
                    With olMail
                        .To = wsMail.Range("To_List")
                        .CC = IIf(wsMail.Range("Cc_List") <> 0, wsMail.Range("Cc_List"), "")
                        .Subject = wsMail.Range("Subject_Line")
                        wsSumm.Range("Mail_Body").Copy
                        Set WrdRng = Doc.Range
                        .Display
                        WrdRng.Paste
                        StrPath = wsMail.Range("StrPath")
                        StrFile = wsMail.Range("StrFile")
                        .Attachments.Add StrPath & "\" & StrFile
                        ' .Send
                    End With
                End If
            End If
        Next Site_Count
    End With
End Sub