简体   繁体   English

宏 excel 未在 email 上附加电源点演示

[英]Macro excel not attaching power point presentation on email

I´ve been trying to attach a power point presentation to a macro excel code to send mails, but when I run the code it only sends the body of the email and not the attached document.我一直在尝试将 power point 演示文稿附加到宏 excel 代码以发送邮件,但是当我运行代码时,它只发送 email 的正文而不是附加的文档。

The document is saved in a local folder so it shouldn't be an issue...该文档保存在本地文件夹中,因此应该不是问题...


Sub sendEmailsToMultiplePersonsWithMultipleAttachments()



Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

Set sh = Sheets("Hoja1")

Set OutApp = CreateObject("Outlook.Application")

For Each cell In sh.Columns("A").Cells.SpecialCells(xlCellTypeConstants)

    'path/file names are entered in the columns D:M in each row
    Set rng = sh.Cells(cell.Row, 1).Range("D1:M1")
    
    If cell.Value Like "?*@?*.?*" And _
    Application.WorksheetFunction.CountA(rng) > 0 Then
        Set OutMail = OutApp.CreateItem(0)
        
        With OutMail
            .To = sh.Cells(cell.Row, 1).Value
            .CC = sh.Cells(cell.Row, 2).Value
            .Subject = "Boarder Logistics Corporations CHILE"
            .Body = sh.Cells(cell.Row, 3).Value
            
            For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                
                If Trim(FileCell.Value) <> "" Then
                    If Dir(FileCell.Value) <> "" Then
                         .Attachments.Add FileCell.Value
                    End If
                End If
            Next FileCell
            '.Send
            .Display
        End With
        
        Set OutMail = Nothing
    End If
Next cell

Set OutApp = Nothing

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


End Sub

when I run the code it only sends the body of the email and not the attached document.当我运行代码时,它只发送 email 的正文,而不是附加的文档。

You need to check conditions under which the attachment may not be added to the mail item.您需要检查可能无法将附件添加到邮件项目的条件。 In the code I see the following loop which iterates over cells and check conditions whether to add a file as an attachment or not:在代码中,我看到以下循环遍历单元格并检查是否将文件添加为附件的条件:

For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                
  If Trim(FileCell.Value) <> "" Then
     If Dir(FileCell.Value) <> "" Then
       .Attachments.Add FileCell.Value
     End If
  End If
Next FileCell

The first point is that loop never iterates over cells.第一点是循环永远不会遍历单元格。

Second, the if conditions could be false, so you will never get a file attached.其次, if条件可能为假,因此您永远不会获得附加文件。

Third, the Attachments.Add method creates a new attachment in the Attachments collection.第三, Attachments.Add方法在 Attachments 集合中创建一个新附件。 The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.附件的来源可以是一个文件(由带有文件名的完整文件系统路径表示)或构成附件的 Outlook 项。 So, make sure that file path specified in the cell is valid and such file exists on the disk.因此,请确保单元格中指定的文件路径有效并且该文件存在于磁盘上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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