【问题标题】:Outlook attachments send then moveOutlook 附件发送然后移动
【发布时间】:2015-03-08 05:54:36
【问题描述】:

文件成功发送到c:\complete

后如何移动

我可以将附件限制为每封电子邮件 10 个附件吗? 每个文件大小为 300kb

Option Explicit

Sub SendMessage(Optional AttachmentPath)
    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment
    Dim objOutlookFile As String


    '// Attachment Path
    AttachmentPath = "C:\Reports\"

    '// Create the Outlook session.
    Set objOutlook = CreateObject("Outlook.Application")

    '// Create the message.
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

    With objOutlookMsg
        '// Add the To recipient(s) to the message.
        Set objOutlookRecip = .Recipients.Add("omar")
        Set objOutlookRecip = .Recipients.Add("omar")
            objOutlookRecip.Type = olTo

        '// Add the CC recipient(s) to the message.
        Set objOutlookRecip = .Recipients.Add("omar")
            objOutlookRecip.Type = olCC

        '// Set the Subject, Body, and Importance of the message.
        .Subject = "Reports"
        .Body = "the Attached reports are complete !" & vbCrLf & vbCrLf
        .Importance = olImportanceHigh  '//High importance

        '// Add attachments to the message.
        objOutlookFile = Dir(AttachmentPath & "*.*")

        Do While Len(objOutlookFile) > 0
            .Attachments.Add AttachmentPath & objOutlookFile
            objOutlookFile = Dir
        Loop

        '// Resolve each Recipient's name.
        For Each objOutlookRecip In .Recipients
            objOutlookRecip.Resolve
            If Not objOutlookRecip.Resolve Then
            objOutlookMsg.Display
        End If
        Next
        '//.DeleteAfterSubmit = True
        '//.Send
        .Display

    End With
    Set objOutlookMsg = Nothing
    Set objOutlook = Nothing
End Sub

【问题讨论】:

    标签: vba outlook outlook-2010


    【解决方案1】:

    不清楚您在哪里运行 VBA 宏代码(Outlook、Word、Excel 等)。

    无论如何,无需在 Outlook VBA 宏中创建新的 Outlook 应用程序实例:

    '// Create the Outlook session.
     Set objOutlook = CreateObject("Outlook.Application")
    

    相反,您可以使用 Application 属性,例如:

    '// Create the message.
    Set objOutlookMsg = Application.CreateItem(olMailItem)
    

    您可以使用FileSystemObject 来管理磁盘上的文件。有关更多信息,请参阅Accessing Files with FileSystemObject

    此外,Outlook 对象模型为 Outlook 项目提供 BeforeAttachmentAdd 事件,该事件在将附件添加到父对象的实例之前触发。它提供了要添加的 Attachment 类的实例和可用于取消操作的 Cancel 参数。只需设置为true即可取消操作;否则,设置为 false 以允许添加附件。

    抱歉,再问一个问题,如果 c:\reports\ 中没有文件,我可以停止发送电子邮件吗?

    最好的方法是在运行 VBA 宏之前检查文件夹。您可以使用 FileSystemObject 来完成工作。

    来自 Outlook 对象模型的 Application 类提供了 ItemSend 事件,只要用户通过检查器发送 Microsoft Outlook 项目,就会触发该事件(在检查器关闭之前,但在用户单击发送按钮之后) ) 或在程序中使用 Outlook 项目(如 MailItem)的 Send 方法时。它提供正在发送的项目引用和 Cancel 参数。如果事件过程将 Cancel 参数设置为 true,则发送操作未完成并且检查器保持打开状态。

    您可以使用这两个事件来检查您需要的任何内容。

    最后,您可能会发现 MSDN 中的 Getting Started with VBA in Outlook 2010 文章很有帮助。

    【讨论】:

    • 感谢@Eugene Astafiev
    猜你喜欢
    • 2013-11-17
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2016-10-07
    相关资源
    最近更新 更多