简体   繁体   English

从Excel发送文件夹中的最新文件

[英]Send most recent file in folder from Excel

I am trying to send the most recent PDF files in a folder from Excel using VBA. 我正在尝试使用VBA从Excel发送文件夹中的最新PDF文件。

I have managed to do it in Outlook VBA - I am not sure what needs to change to do it in Excel. 我已经设法在Outlook VBA中做到这一点-我不确定在Excel中需要进行哪些更改。 The reason is because the Outlook macro conflicts with Excel macros that are running periodically. 原因是因为Outlook宏与定期运行的Excel宏冲突。

My code at the moment just attaches all the files in a folder that have been created in the last 30 secs - only ever one PDF. 目前,我的代码只是将过去30秒内创建的文件夹中的所有文件附加到文件中-只有一个PDF。

Please note that the code works perfectly in Outlook. 请注意,该代码可在Outlook中完美运行。

Sub SendFiles()
 Dim objMail As Outlook.MailItem
 Dim fso As Object
 Dim strFile As String
 Dim fsoFile
 Dim fsoFldr
 Dim dtNew As Date, sNew As String

Set fso = CreateObject("Scripting.FileSystemObject")

 strFile = "C:\temp\" 'path to folder

 Set fsoFldr = fso.GetFolder(strFile)
 dtNew = Now - TimeValue(00:00:30) '30 seconds ago

For Each fsoFile In fsoFldr.Files

If fsoFile.DateCreated > dtNew Then

sNew = fsoFile.Path

Set objMail = Application.CreateItem(olMailItem)

 With objMail
 .To = "email@address.com"
 .Subject = "Example"
 .BodyFormat = olFormatPlain
 .Attachments.Add sNew
 .Importance = olImportanceHigh
 .Send
 End With

End If
Next fsoFile

End Sub

some flaws: 一些缺陷:

  • you're not instantiating any Outlook application object 您没有实例化任何Outlook应用程序对象

    in an Excel environment, Application is pointing at Excel Application 在Excel环境中, Application指向Excel Application

  • TimeValue(00:00:30) should be TimeValue("00:00:30") TimeValue(00:00:30)应该为TimeValue("00:00:30")

and be sure you have added Outlook library to your VBA project references: 1) click Tools -> References 2) scroll list box till Microsoft Outlook X.XX Object Library entry and toggle its check mark to select it 3) click "OK" button 并确保已将Outlook库添加到您的VBA项目引用中:1)单击工具->引用2)滚动列表框直到Microsoft Outlook X.XX对象库条目并切换其复选标记以将其选中以3)单击“确定”按钮

then you could try this little refactoring of your code: 那么您可以尝试对代码进行以下重构:

Option Explicit

Sub SendFiles()
    Dim objOutLook As Object
    Dim fso As Object
    Dim strFile As String
    Dim fsoFile
    Dim fsoFldr
    Dim dtNew As Date, sNew As String
    Dim newOutlookInstance As Boolean

    Set fso = CreateObject("Scripting.FileSystemObject")

    If GetOutlook(objOutLook, newOutlookInstance) Then

        strFile = "C:\temp\" 'path to folder
        Set fsoFldr = fso.GetFolder(strFile)
        dtNew = Now() - TimeValue("00:00:30") '30 seconds ago

        For Each fsoFile In fsoFldr.Files
            If fsoFile.DateCreated > dtNew Then
                sNew = fsoFile.Path
                With objOutLook.CreateItem(olMailItem)
                    .To = "email@address.com"
                    .Subject = "Example"
                    .BodyFormat = olFormatPlain
                    .Attachments.Add sNew
                    .Importance = olImportanceHigh
                    .Send
                End With
            End If
        Next
        If newOutlookInstance Then objOutLook.Quit '<--| quit Outlook if an already running instance of it hasn't been found
        Set objOutLook = Nothing

    Else
        MsgBox "Sorry: couldn't get a valid Outlook instance running"
    End If

End Sub



Function GetOutlook(objOutLook As Object, newOutlookInstance As Boolean) As Boolean
    Set objOutLook = GetObject(, "Outlook.Application")
    If objOutLook Is Nothing Then
        Set objOutLook = New Outlook.Application
        newOutlookInstance = True
    End If
    GetOutlook = Not objOutLook Is Nothing
End Function

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

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