Azzera filtri
Azzera filtri

How to get output of eventhandler function to main function

3 visualizzazioni (ultimi 30 giorni)
I have written a code that is linked to Outlook via actxserver in order to get data from the latest mail i got. I wish to process the event function and then pass the output of it back to the main function, where i want to further process the data (parse it). But i get this error:
Error using registerevent
Too many output arguments.
My main function is:
outlook = actxserver('Outlook.Application');
[subject, body] = registerevent(outlook,{'NewMail', 'MailEvent'})
% some more code where i need subjct and body data
%...
and the MailEvent function is:
function [subject, body] = MailEvent(varargin)
outlook = varargin{1};
% Retrieving last email
mapi=outlook.GetNamespace('mapi');
INBOX=mapi.GetDefaultFolder(6);
count = INBOX.Items.Count; %index of the most recent email.
firstemail=INBOX.Items.Item(count); %imports the most recent email
INBOX.Items.Item(count).Unread=0;
subject = firstemail.get('Subject');
body = firstemail.get('Body');
end
If i leave the output away and use it this way it works, but the subject and body data is "captured" in the Mailevent workspace
outlook = actxserver('Outlook.Application');
registerevent(outlook,{'NewMail', 'MailEvent'})
How can i solve this ?

Risposte (1)

Vatsal
Vatsal il 13 Mar 2024
Hi,
The issue you are facing is due to the fact that the "registerevent" function in MATLAB does not support output arguments. It is designed to register a callback function (in given case, MailEvent) that gets executed when a specific event occurs. However, it does not return any values from the callback function.
A potential solution involves the use of global variables to transfer data from the MailEvent function to the main function.
The code could be adjusted in the following manner:
% Declare global variables
global subject body
% Your main function
outlook = actxserver('Outlook.Application');
registerevent(outlook,{'NewMail', 'MailEvent'})
% Now subject and body can be accessed here
% some more code where you need subject and body data
%...
% MailEvent function
function MailEvent(varargin)
% Make sure to declare the variables as global in this function as well
global subject body
outlook = varargin{1};
% Retrieving last email
mapi=outlook.GetNamespace('mapi');
INBOX=mapi.GetDefaultFolder(6);
count = INBOX.Items.Count; %index of the most recent email.
firstemail=INBOX.Items.Item(count); %imports the most recent email
INBOX.Items.Item(count).Unread=0;
subject = firstemail.get('Subject');
body = firstemail.get('Body');
end
I hope this helps!

Categorie

Scopri di più su Use COM Objects in MATLAB in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by