• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Require help in changing the vba code -

Kumshan1988

New Member
Currently below vba code auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox
I want to do modifications in below code so that it only auto downloads email into a specific folder in local hard disk as and when any new email arrives in inbox/subfolder (For eg: ApprovalMails in my scenario). I dont know where to change the code to get the desired output. please help.

>>> use code - tags <<<
Code:
Public Sub SaveMsg(Item As Outlook.MailItem)
  Dim sPath As String
  Dim dtDate As Date
  Dim sName As String
  Dim enviro As String

 enviro = CStr(Environ("USERPROFILE"))
  
  sName = Item.Subject
  ReplaceCharsForFileName sName, "_"

  dtDate = Item.ReceivedTime
  sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
    vbUseSystem) & Format(dtDate, "-hhnnss", _
    vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg"

' use My Documents in older Windows.
    sPath = enviro & "\Documents\ApprovalMails\"
  Debug.Print sPath & sName
  Item.SaveAs sPath & sName, olMSG
End Sub

Private Sub ReplaceCharsForFileName(sName As String, _
  sChr As String _
)
  sName = Replace(sName, "/", sChr)
  sName = Replace(sName, "\", sChr)
  sName = Replace(sName, ":", sChr)
  sName = Replace(sName, "?", sChr)
  sName = Replace(sName, Chr(34), sChr)
  sName = Replace(sName, "<", sChr)
  sName = Replace(sName, ">", sChr)
  sName = Replace(sName, "|", sChr)
End Sub
Sub aagdsa()

End Sub
 
Last edited by a moderator:
The output folder is determined by this bit of code:
Code:
' use My Documents in older Windows.
    sPath = enviro & "\Documents\ApprovalMails\"
  Debug.Print sPath & sName
  Item.SaveAs sPath & sName, olMSG
Earlier in the program, the value of enviro is taken from your PC's USERPROFILE setting in the environment variables. Then the above statement adds "\Documents\ApprovalMails\" to that path, and that's where the emails are saved. So if you want to change the folder where the emails are saved, change that statement.

I'm ignoring the other part of your question ("...and when any new email arrives..."); I'm not sure yet what you want. Someone else may have a suggestion about that.
 
Hi,

I don't want to save email received in inbox. i want to save only specific emails. So i have creating new sub folder (Approvals) under inbox (outlook)
if i copy or drag relevant email to Approvals subfolder, that emails should auto download into "\Documents\ApprovalMails\" file path.
Below code saves email as and when new email is received in inbox. So i require help in changing the below VBA code so that only specific emails gets auto download in the required path.

>>> as You've already noted <<<
>>> use code - tags <<<

Code:
Public Sub SaveMsg(Item As Outlook.MailItem)
  Dim sPath As String
  Dim dtDate As Date
  Dim sName As String
  Dim enviro As String

enviro = CStr(Environ("USERPROFILE"))
 
  sName = Item.Subject
  ReplaceCharsForFileName sName, "_"

  dtDate = Item.ReceivedTime
  sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
    vbUseSystem) & Format(dtDate, "-hhnnss", _
    vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg"

' use My Documents in older Windows.
    sPath = enviro & "\Documents\ApprovalMails\"
  Debug.Print sPath & sName
  Item.SaveAs sPath & sName, olMSG
End Sub

Private Sub ReplaceCharsForFileName(sName As String, _
  sChr As String _
)
  sName = Replace(sName, "/", sChr)
  sName = Replace(sName, "\", sChr)
  sName = Replace(sName, ":", sChr)
  sName = Replace(sName, "?", sChr)
  sName = Replace(sName, Chr(34), sChr)
  sName = Replace(sName, "<", sChr)
  sName = Replace(sName, ">", sChr)
  sName = Replace(sName, "|", sChr)
End Sub
Sub aagdsa()

End Sub
 
Last edited by a moderator:
And how will you identify the emails you want to save? By the subject line, who it's from, certain dates and times, certain words in the body, the format (HTML/plain text/RTF), what?
 
I am going to identify email manually as the process is not standardized. Identified email will be moved onto specific folder. For eg. Approvals.
Whatever email gets moved into specific folder. For eg. Approvals. , should get auto saved into local hard disk:
"\Documents\ApprovalMails\"
 
Ah, I see: When you run the program, you want it to look at that folder and move all the emails in it to the new location.

Ok, so you have a subfolder that you've moved the emails to—let's call that the "From" folder—and you want the program to move all its contents (or maybe just the email items in it?) to another folder, call that the "To" folder. These folders, are they Outlook folders or hard-drive folders? I've been assuming that you want to move things from a folder in Outlook to a folder on your hard drive, but is that right?

And another question: If you're already manually moving mail items from an Outlook folder to another Outlook folder, why not manually move them to the Windows folder instead? It's no more work...or is it?
 
Ah, I see: When you run the program, you want it to look at that folder and move all the emails in it to the new location. (Yes correct)

Ok, so you have a subfolder that you've moved the emails to—let's call that the "From" folder—and you want the program to move all its contents (or maybe just the email items in it?) to another folder, call that the "To" folder. These folders, are they Outlook folders or hard-drive folders? I've been assuming that you want to move things from a folder in Outlook to a folder on your hard drive, but is that right? From Folder is an outlook folder and To Folder is a hard drive folders

And another question: If you're already manually moving mail items from an Outlook folder to another Outlook folder, why not manually move them to the Windows folder instead? It's no more work...or is it? I use QuickSteps (with shortcut key) to moving mail items from an Outlook folder to another Outlook folder. This is instant no effort required. In my scenario i am maintaining approval records in excel file and i want to link those records with corresponding approval. msg saved in hard drive folders for better tracking and reference purpose.
 
Ok, here's the thing: I do a lot of Excel programming but not so much Outlook programming. I know it's possible to go pick out a particular folder path in Outlook, and then run through all the mail items therein and save each one as a file in a particular hard-drive folder; but I'm not so used to Outlook that I know off-hand how to navigate Outlook folders. We can figure it out, but it might take some work. (Oh, the horror! I might be forced to learn something new! Ok, I wouldn't mind that much, I guess.)

It might take less work to come at it from another angle. I'm not familiar with this QuickSteps thingy, but I take it you've taught it to move a mail item to an Outlook folder; could you teach it to move it to a hard-drive folder instead? That I know how to do, and also it saves you the work of maintaining that intermediate Outlook folder. That's assuming you don't otherwise need the intermediate folder, of course.
 
Back
Top