0

I would like be able to download/extract .msg files from folders a subfolders in Outlook. And possibly keep the structure.

folder
     subfolder 1
                subfolder 1.A
                subfolder 1.B                    
     subfolder 2 
                subfolder 2.A
                subfolder 2.B 
          etc.

                     

Can you please show a direction how it can be done? Thank you

tststs
  • 3
  • 1
  • Does this answer your question? [Outlook using python win32com to iterate subfolders](https://stackoverflow.com/questions/40849742/outlook-using-python-win32com-to-iterate-subfolders) – Eugene Astafiev Dec 14 '21 at 15:44
  • You should explain what you have tried to do to solve the problem, including some code. – jssDev Dec 21 '21 at 12:48

1 Answers1

0

The Outlook using python win32com to iterate subfolders thread explains how to iterate over all subfolders. Using recursion you may go through each folder in Outlook. And with each iteration you can create a subfolder on a disk where you can save messages.

The MailItem.SaveAs method saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used.

Note, Outlook folders may contain different kind if items - appointments, tasks, notes, documents and etc. So, I'd suggest checking the Class property at runtime to make sure you deal with mail items only.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks for answer and suggestions, I will try to implement this. – tststs Dec 14 '21 at 20:13
  • so far, I have managed to write script for downloading .msg files from one specific folder `my_folder = mapi.Folders['folder'].Folders['sub1'].Folders['sub2'].Folders['sub3'].Folders['sub4'].Folders['sub5'] for msg in my_folder.Items: msgname = msg.subject msgname=str(msgname) msg.SaveAs(os.getcwd()+'//'+msgname)` – tststs Dec 15 '21 at 10:01
  • You can iterate over all folder in Outlook recursively, see thread mentioned in my post for more information on that. – Eugene Astafiev Dec 15 '21 at 13:11
  • yes, this answer worked, Thanks! https://stackoverflow.com/a/66709316/8956217 – tststs Dec 15 '21 at 14:02