Outlook 附件发送然后移动

Outlook attachments send then move

文件成功发送到 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、Word、Excel 等)的位置。

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

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

相反,您可以使用应用程序 属性,例如:

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

您可以使用 FileSystemObject for managing files on the disk. See Accessing Files with FileSystemObject 获取更多信息。

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

sorry one more question, can I stop outgoing email if there is no files in c:\reports\

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

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

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

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