【问题标题】:vba outlook - reply from a file from a foldervba Outlook - 从文件夹中的文件回复
【发布时间】:2015-04-23 10:43:13
【问题描述】:

我将 Outlook 消息拖到名为“电子邮件临时文件夹”的特定文件夹中,并希望自动回复该消息。

我保存在“电子邮件临时文件夹”中的消息的标题名称可以是任何内容。我无法获得文件的标题名称。所以我尝试遍历“电子邮件临时文件夹”和Set FileItemToUse = objFile中的文件

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

我怎样才能将FileItemToUse 变成一个outlook 项目?

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

【问题讨论】:

  • 您的代码循环通过磁盘 C 文件系统中的目录:您正在选择第一个文件(并省略退出循环)。您的误解是您混淆了文件和 Outlook MailItem 对象。您使用的属性和方法仅适用于 MailItem 而不是文件。它应该有助于定义具有预期类型而不是“对象”的变量。
  • 由于我拖入文件夹的文件是.msg文件,是否可以将其用作邮件项?
  • 是的。您可以使用Application.CreateItemFromTemplate("mail.msg") 完成此操作。请参阅相关帖子:stackoverflow.com/questions/19383290/…(=“.msg 文件 mailitem”的第一批 Google 点击之一)。

标签: vba outlook reply fso


【解决方案1】:

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

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

这个(未经测试的)剪辑受到这个article 的启发。 Microsoft Scripting Runtime 必须作为项目参考。

【讨论】:

    【解决方案2】:

    所以我尝试遍历“电子邮件临时文件夹”中的文件并设置 FileItemToUse = objFile

    那样做是不可能的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 2018-11-08
      • 2014-11-18
      相关资源
      最近更新 更多