Code covered by the BSD License  

Highlights from
msopen

msopen

by

 

02 Mar 2004 (Updated )

Opens Microsoft Office files (Word, Excel, Power Point, Project, & Access).

msopen(varargin)
function msopen(varargin)

%MSOPEN Opens Microsoft Office files (Word, Excel, Power Point, Project & Access).
%
% msopen(file)
%
%       file:  Name of file with extension (ex. 'Doc1.doc')
%
% Examples:
%
%      msopen myfile.doc;
%      msopen myfile.xls;
%      msopen myfile.ppt my*.xls myfile.doc;

%   Copyright 2004 Fahad Al Mahmood
%   Version: 1.0 $  $Date: 2-Mar-2004
%            1.5 $  $Date: 7-Mar-2004    (Multiple Files Option Added)
%            1.6 $  $Date: 30-Mar-2004   (Fixed Path Problem)
%            1.7 $  $Date: 30-Mar-2004   (Project & Access Added)
%            2.0 $  $Date: 25-May-2004   (Support wildcard)
%            2.2 $  $Date: 25-May-2004   (Support Full or Partial path)
%            2.3 $  $Date: 01-Jul-2009   (Support Microsoft Office 2007 extensions)



EX=1;WD=1;PP=1;MSP=1;,AC=1;

files = wildfiles(varargin{1:end});

for i=1:length(files)
    file=files{i};
    [fpath,fname,fext] = fileparts(file);
    if isempty(fpath) out_path = pwd;
    elseif fpath(1)=='.' out_path = [pwd filesep fpath];
    else out_path = fpath;
    end
    switch fext
        case {'.xls','.xla','.xlt'}
            if EX==1
                Excel = actxserver('Excel.Application');
                set(Excel,'Visible',1);
                EX=EX+1;
            end
            try invoke(Excel.Workbooks, 'open', [out_path filesep fname fext]);
            catch error('Cannot open file and/or file does not exist!'); end
            
        case {'.docx','.docm','.dotx','.dotm','.doc','.dot','.htm','.html','.rtf','.mht','.mhtml','xml','.odt'}
            if WD==1
                Word = actxserver('Word.Application');
                set(Word,'Visible',1);
                WD=WD+1;
            end
            try invoke(Word.Documents, 'Open', [out_path filesep fname fext]);
            catch error('Cannot open file and/or file does not exist!'); end
            
        case {'.pptx','.pptm','.ppsm','.ppsx','.potx','.potm','.ppt','.pps','.pot','.odp'}
            if PP==1
                PPT = actxserver('PowerPoint.Application');
                set(PPT,'Visible',1);
                PP=PP+1;
            end
            try invoke(PPT.Presentations,'Open',[out_path filesep fname fext]);
            catch error('Cannot open file and/or file does not exist!'); end
            
        case {'.mpp','.mpt','.mpd','.mpw','.mpx'}
            if MSP==1
                MSProject = actxserver('MSProject.Application');
                set(MSProject,'Visible',1);
                MSP=MSP+1;
            end
            try invoke(MSProject,'FileOpen',[out_path filesep fname fext]);
            catch error('Cannot open file and/or file does not exist!'); end
            
        case {'.mdb','.adp','.mda','.mde','.ade','mdw','.accdb','.accda','.accde'}
            if AC==1
                Access = actxserver('Access.Application');
                set(Access,'Visible',1);
                AC=AC+1;
            end
            try invoke(Access,'OpenAccessProject',[out_path filesep fname fext]);
            catch error('Cannot open file and/or file does not exist!'); end
            
        otherwise
            error('File type is not supported or extension is not specified!');
    end
end

function files_names = wildfiles(varargin);

%WILDFILES converts a wildcard expression to a list of files in a cell array.  
%
% 
% NOTE:     * Tested only in (Windows) but not in (UNIX).
%           * If no files found, the function will return (0).
%
% Examples:
% 
%   wildfiles *.*
%   D = wildfiles('m*.*');
%   D = wildfiles('m*.*','s*.m');
%
%   Copyright 2004 Fahad Al Mahmood
%   Version: 1.0 $  $Date: 1-May-2004


k=1;
for n=1:nargin
    [pathstr,name,ext]=fileparts(varargin{n});
    if isempty(pathstr);
        pathstr=pwd;
    end
    if isempty(name) & ~isempty(ext)
        name = '*';
    end
    if strmatch(ext,'.','exact')
        ext = '.*';
    end
    full_name = [pathstr filesep name ext];
    if isempty(pathstr)
        full_name = [name ext];
    end
    if isdir([pathstr filesep name])
        full_name = [pathstr filesep name filesep '*.*'];
    end
    files = dir(full_name);
    
    % Assigning file names to a cell array after making sure they do not
    % contain directories.
    for m=1:length(files)
        if ~files(m).isdir
            files_names{k} = [pathstr filesep files(m).name];
            k=k+1;
        end
    end
    
    % Flag (0) is case no file is found.
    if ~exist('files_names','var')
        files_names = 0;
    end
    files_names = files_names';
end

Contact us