简体   繁体   中英

Create Email macro stopped working after upgrade to Office 365 from Office 2013

My team recently upgraded to Office 365 from Office 2013. We have an Excel document that we use for auto-generating an email, and attaching the invoices to said emails. After the upgrade, it stopped working.

    Dim rng As Range
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    Dim StrBody As String
    Dim Bundle As Variant, Group As Variant
        Bundle = Split(Worksheets("Extra").Range("H2").Value, ",")
        StrBody = Range("D5").Value & "<br>" _
                  & Range("D6").Value & "<br>" _
                  & Range("D7").Value & "<br>" _
                  & Range("D8").Value & "<br>" _
                  & Range("D9").Value
        mola = Cells(2, 2).Value
        maybe = Format(mola, "mm")
        real = Format(mola, "mmmm yyyy")
        nope = Format(mola, "yyyy")
        InvPath = ("Path omitted for security")
        
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(olMailItem)

    On Error Resume Next
    With OutMail
        .To = Cells(2, 3).Value
        .CC = Cells(2, 4).Value
        .Subject = Cells(5, 3).Value
        .HTMLBody = StrBody
        For Each Group In Bundle
            .Attachments.Add InvPath & "Group" & Group & " " & real & ".pdf"
        Next
        On Error GoTo 0
    End With
    .Display
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With 

Thank you in advance --Jsmalls

You've not explicitly stated what the issue is so I'm assuming it's stopping as soon as you try to run the code. If this is the case, check your references in the VB Editor screen (Tools, References) and you need to make sure you have Microsoft Outlook 16.0 Object Library ticked.

Sub test()
    Dim rng As Range
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    Dim StrBody As String
    Dim Bundle As Variant, Group As Variant
        Bundle = Split(Worksheets("Extra").Range("H2").Value, ",")
        StrBody = Range("D5").Value & "<br>" _
                  & Range("D6").Value & "<br>" _
                  & Range("D7").Value & "<br>" _
                  & Range("D8").Value & "<br>" _
                  & Range("D9").Value
        mola = Cells(2, 2).Value
        maybe = Format(mola, "mm")
        real = Format(mola, "mmmm yyyy")
        nope = Format(mola, "yyyy")
        InvPath = ("Path omitted for security")
        
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(olMailItem)

    On Error Resume Next
    With OutMail
        .To = Cells(2, 3).Value
        .CC = Cells(2, 4).Value
        .Subject = Cells(5, 3).Value
        .HTMLBody = StrBody
        For Each Group In Bundle
            .Attachments.Add InvPath & "Group" & Group & " " & real & ".pdf"
            .Display
        Next
        On Error GoTo 0
    End With

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

You also have .display a bit all over the place with it on the same line as the Next for the For Each loop, and outside any With to End With grouping. I put it in the For Each loop and deleted the one outside the With grouping, but you might want to check that's where it should be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM