vba outlook - 从文件夹中的文件回复

时间:2015-02-21 20:58:17

标签: vba outlook-vba reply fso

我将outlook msg拖到名为“email temp folder”的特定文件夹中,并希望自动回复该消息。

我在“email temp folder”中保存的msg的标题名称可以是任何内容。我无法获取文件的标题名称。所以我尝试遍历“email temp folder”和Set FileItemToUse = objFile

中的文件

但是,有一个错误:对象在此行上不支持此属性或方法。 .ReplyAll

如何将FileItemToUse转换为展示项?

Sub outlookActivate1()

  Dim OutApp As Outlook.Application
  Dim OutMail As Outlook.MailItem
  Dim fso As New FileSystemObject
  Dim objFolder As Object
  Dim objFile As Object
  Dim FileItemToUse As Object
  Dim i As Long

  Set OutApp = CreateObject("Outlook.Application")

  strPath = "C:\Users\admin\Desktop\email temp folder" & "\"
  strFiles = Dir(strPath & "*.*")
  Set objFolder = fso.GetFolder(strPath)

  For Each objFile In objFolder.Files 

    If i = 0 Then    
      Set FileItemToUse = objFile           
    End If

  Next objFile


  With FileItemToUse

    .ReplyAll
    .BCC = ""
    .Subject = "Hi"
    .HTMLBody = "testing"
    .BodyFormat = olFormatHTML
    .display

  End With

  Set OutMail = Nothing
  Set OutApp = Nothing

End Sub

2 个答案:

答案 0 :(得分:0)

您的代码应类似于以下内容:

Sub ReplyToFilesInFolder(SourceFolderName As String)
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
    Dim FileItem As Scripting.File
    Dim strFile
    Dim strFileType
    Dim openMsg As MailItem     
    Dim strFolderpath As String

    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)

    For Each FileItem In SourceFolder.Files    
       strFile = FileItem.name

       ' This code looks at the last 4 characters in a filename
       ' If we wanted more than .msg, we'd use Case Select statement
       strFileType = LCase$(Right$(strFile, 4))
       If strFileType = ".msg" Then
           Debug.Print FileItem.Path

           Set openMsg = Application.CreateItemFromTemplate(FileItem.Path)

           ' do whatever is needed to reply

           openMsg.Close olDiscard   
           Set openMsg = Nothing

           ' end do whatever
      End If
    Next FileItem

    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing      
End Sub

这个(未经测试的)剪辑的灵感来自于这个articleMicrosoft Scripting Runtime必须包含在项目参考中。

答案 1 :(得分:0)

  

所以我尝试循环浏览"电子邮件临时文件夹"并设置FileItemToUse = objFile

不可能以这种方式完成工作。

将消息文件(.msg)文件拖到特定文件夹时,会触发ItemAdd事件。因此,您需要处理事件以获取对应于已删除文件的MailItem对象。然后,您可以使用ReplyReplyAll方法。