将MATLAB 11 activex代码(用于创建MS-Word 2007文档)自定义为MATLAB 2015和MS-Word 2010

时间:2015-11-24 11:40:51

标签: matlab ms-word activex

我编写了下一个代码来创建一个Word文档,其中的数字排列在几个表格中。它使用MATLAB 2011和MS-Word 2007在我的笔记本电脑上完美运行。但是当我尝试在MATLAB 2015和MS-Word 2010的大学计算机上运行它时,生成的文档被搞砸了。有人可以告诉我如何解决它吗?以及我如何编写一个与平台无关的程序?

function finalWriteToWord

    WordFileName='MyTestDoc2.doc';
    CurDir=pwd;
    FileSpec = fullfile(CurDir,WordFileName);
    if exist(FileSpec,'file')
        delete(FileSpec)
    end
    word_server=actxserver('Word.Application');
    word_document=invoke(word_server.Documents,'Add');
    CM_IN_POINTS=28.3464567;         %1cm=28.3464567
    word_document.Pagesetup.LeftMargin=0.7*CM_IN_POINTS;
    word_document.Pagesetup.RightMargin=0.7*CM_IN_POINTS;
    word_document.Pagesetup.TopMargin=0.5*CM_IN_POINTS;
    word_document.Pagesetup.BottomMargin=0.5*CM_IN_POINTS;


    for table_ind=1:3
        nr_rows_p=4;
        nr_cols_p=2;
        range=word_document.Content;
        range.Collapse(0);
        table=invoke(word_document.Tables,'add',range,nr_rows_p,nr_cols_p,1,0);
        %write the data into the table
        for row_ind=1:nr_rows_p
            for col_ind=1:nr_cols_p
                    fig=figure;plot([1:10]);title(['Figure: table-' num2str(table_ind)  ' row-' num2str(row_ind) ' col-' num2str(col_ind)]);xlabel('Time [s]');ylabel('Amplitude [A]')  
                    print(fig,'-dmeta');
                    table.Cell(row_ind,col_ind).Select;
                    invoke(word_server.Selection,'Paste');
                    numShapes=word_document.InlineShapes.Count;
                    word_document.InlineShapes.Item(numShapes).Height=98;
                    word_document.InlineShapes.Item(numShapes).Width=270;
            end
        end
        word_server.Selection.ParagraphFormat.Alignment='wdAlignParagraphCenter'; 
        %word_server.Selection.InsertBreak; %pagebreak
    end

    CloseWord(word_server,word_document,FileSpec);    
    close all;
return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function CloseWord(actx_word_p,word_handle_p,word_file_p)

    invoke(word_handle_p,'SaveAs',word_file_p,1);

    % Close the word window:
    invoke(word_handle_p,'Close');            
    % Quit MS Word
    invoke(actx_word_p,'Quit');            
    % Close Word and terminate ActiveX:
    delete(actx_word_p);            
return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

0 个答案:

没有答案