-1

I dragged an outlook msg to a specific folder named "email temp folder" and would like to reply on that msg automatically.

The title name of the msg which I saved in "email temp folder" could be anything. It is not possible for me to get the file's title name. So I try to loop through the file in "email temp folder" and Set FileItemToUse = objFile

However, there is an error: object doesn't support this property or method on this line. .ReplyAll

How am I able to turn FileItemToUse into an outlook item?

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
pexpex223
  • 371
  • 4
  • 10
  • 25
  • Your code loops through a directory in your filesystem of disk C: You are picking the first file (and omit to leave the loop). Your misunderstanding is that you are mixing up files and Outlook MailItem objects. The properties and methods you are using only apply to a MailItem and not to a file.It should help to define variables with the expected types rather than as "Object". – Axel Kemper Feb 21 '15 at 22:41
  • As the file i dragged into the folder is a .msg file, is it possible to utilize it as an mailitem? – pexpex223 Feb 22 '15 at 05:30
  • Yes. You can accomplish this using `Application.CreateItemFromTemplate("mail.msg")`. See related post: http://stackoverflow.com/questions/19383290/how-do-i-open-an-outlook-msg-file-from-my-harddrive-that-is-not-in-outlook (= one of the first Google hits for ".msg file mailitem"). – Axel Kemper Feb 22 '15 at 09:07
  • possible duplicate of [VBA - select the first file from a specific folder and reply all](http://stackoverflow.com/questions/28645519/vba-select-the-first-file-from-a-specific-folder-and-reply-all) – Eugene Astafiev Feb 22 '15 at 09:59
  • It is a duplicate, see http://stackoverflow.com/questions/28645519/vba-select-the-first-file-from-a-specific-folder-and-reply-all – Eugene Astafiev Feb 22 '15 at 10:00
  • Why drag the mail to the folder first - I would go other way round, with one code first accomplish the reply to all and then the save-to-folder – Max Feb 22 '15 at 12:08

2 Answers2

0

Your code should look similar to the following:

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

This (untested) snipped was inspired by this article. Microsoft Scripting Runtime has to be included as project reference.

Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
0

So I try to loop through the file in "email temp folder" and Set FileItemToUse = objFile

It is not possible to get the job done that way.

When you drag a message file (.msg) file to a specific folder the ItemAdd event is fired. So, you need to handle the event to get a MailItem object which corresponds a dropped file. Then you can use the Reply or ReplyAll methods.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45