Downloading Attachments from .msg file

3 visualizaciones (últimos 30 días)
Alec Boyle
Alec Boyle el 30 de Jun. de 2017
Comentada: Image Analyst el 30 de Oct. de 2017
Hi,
I'm trying to write a script that will download the attachments of an outlook email item. The problem is, the attachments of the email are .msg files (emails) themselves. I'm able to download these .msg files no problem, but after that I haven't been able to access the attachments of the .msg file. To resolve this, I've thought of 2 solutions, neither of which I've written the correct code for: 1: find a way to access and download attachments to the .msg file from within a folder on the hard drive 2: download the attachments of the attachments in the original email in outlook.
Here's my code so far. It's able to access emails and download the attachments as .msg files into separate file folders
outlook = actxserver('Outlook.Application');
mapi=outlook.GetNamespace('mapi');
INBOX=mapi.GetDefaultFolder(6);
foldernum = 00001; %INPUT number of folder to save 1st case to
mainpath = 'E:\SaveAttachments';
count = INBOX.Items.Count;
for i = 1:count %1st email to last email in inbox
index = i; %first index is 1, second is 2, etc
email_i = INBOX.Items.Item(i); % parses through all emails in inbox
subject_i = email_i.get('Subject'); % get subject of email i
if (strcmp('Test',subject_i)) % search for emails that contain "Test" as subject line in inbox
C = 'Found one' % if subject contains "Test", diplay "found one"
attachments_i = email_i.get('Attachments'); % retrieve all attachments for email that you found
if attachments_i.Count >=1
for j = 1:attachments_i.Count %parse through all attachments
fname = attachments_i.Item(j).Filename; %name the file
foldernumSave = sprintf('%05d',foldernum); %keeps folder number as 5 integer number
savepath = strcat(mainpath,'\',foldernumSave,'\',fname);
attachments_i.Item(j).SaveAsFile(savepath);
foldernum = foldernum + 1;
end
end
end
end
From here, I'm lost. I have lots of folders with .msg files, and I don't know how to access their attachments

Respuestas (1)

Image Analyst
Image Analyst el 30 de Jun. de 2017
Can you record a macro in Outlook? If so, do the attachment saving and look at the macro to see what it did. Then implement those same commands, after some generalization, in MATLAB.
  2 comentarios
Alec Boyle
Alec Boyle el 30 de Oct. de 2017
Thanks for your reply, but no you can't run a macro in Outlook. Still trying to work out this problem, if you have any suggestions!
Image Analyst
Image Analyst el 30 de Oct. de 2017
Type "record macros" in the "Tell me what you want to do" field, and you'll find:
Show the Developer tab
The Developer tab is not displayed by default, but you can add it to the ribbon when you want to do or use the following:
Write macros.
Run macros that you previously recorded.
Use XML commands.
Use ActiveX controls.
Create applications to use with Microsoft Office programs.
Use form controls in Microsoft Excel.
Work with the ShapeSheet in Microsoft Visio.
Create new shapes and stencils in Microsoft Visio.
After you show the tab, it stays visible, unless you clear the check box or have to reinstall a Microsoft Office program.
Show the Developer tab in your Office application
1.Click the File tab.
2.Click Options.
3.Click Customize Ribbon.
4.Under Customize the Ribbon and under Main Tabs, select the Developer check box.

Iniciar sesión para comentar.

Categorías

Más información sobre Downloads en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by