【Matlab创建word文档,插入图注或表注】

Matlab 创建word文档,网上都可以查询到相关的资料,但是怎么通过matlab插入图片后,在图片下方插入题注及图片说明,我查了相关的资料不太容易查询到,特别写这么一篇文章,和大家共同学习。

clc;clear;close all;fclose all;
picpath=[pwd,'\pic\'];
filespec_user = [pwd '\附件1.doc'];% 设定测试Word文件名和路径
if exist(filespec_user,'file')
    delete(filespec_user);
end
% 判断Word是否已经打开,若已打开,就在打开的Word中进行操作,否则就打开Word
try
    % 若Word服务器已经打开,返回其句柄Word
    Word = actxGetRunningServer('Word.Application');
catch
    % 否则,创建一个Microsoft Word服务器,返回句柄Word
    Word = actxserver('Word.Application');
end
Word.Visible = 0; % 或set(Word, 'Visible', 1);
Document = Word.Documents.Add;
Document.SaveAs2(filespec_user);
Content = Document.Content;
Selection = Word.Selection;
Word.CaptionLabels.Add('图') ;%题注
Word.CaptionLabels.Add('表');

Word.Selection.InsertCaption('表', '时域汇总表', '', 0, false);
Table=Document.Tables.Add(Selection.Range,5,8);
Dt1=Document.Tables.Item(1);

Selection.Start = Content.end;
Selection.TypeParagraph;

Filename=strcat(picpath,'*.jpg');
Fn=dir(Filename);
for k=1:length(Fn)
    PicName=strcat(Fn(k).folder,'\',Fn(k).name);
    TemPic=char(Fn(k).name);
    PicN=TemPic(1:end-4);
    Selection.Start = Content.end;
    Selection.InlineShapes.AddPicture(PicName);
    Selection.TypeParagraph;
    Word.Selection.InsertCaption('图', PicN, '', 0, false);
    %     Selection.Text=PicN;
    Selection.Start = Content.end;
    Selection.TypeParagraph;
    Selection.MoveDown;
    clear PicName TemPic PicN
end
Content.Paragraphs.Alignment = 'wdAlignParagraphCenter';
Content.Font.Name = '宋体' ;
Content.Font.Size=14;
Document.Save; % 保存文档
Document.Close; % 关闭文档
clear Document Content Dt1 Selection Table
Word.Quit; % 退出word服务器
clear;clc;

 Word.Selection.InsertCaption('图', PicN, '', 0, false);这句话就是加入图注语句。

语法

expression。 InsertCaption_Label_ , _Title_ , _TitleAutoText_ , _Position_ , _ExcludeLabel_ )

expression:必需。 表示 Selection 对象的变量。

参数

参数
名称必需/可选数据类型说明
Label必需Variant要插入的题注标签。 可以是 String  WdCaptionLabelID 常量 之一。 如果未定义标签,则会导致出错。 使用 CaptionLabels 对象的 Add 方法来定义新的题注标签。
Title可选Variant要紧接在题注标签之后插入的字符串(如果已指定 TitleAutoText,则忽略该参数)。
TitleAutoText可选Variant要紧接在题注标签之后插入其内容的"自动图文集"词条(将覆盖由 Title 指定的任何文本)。
Position可选Variant指定是否高于或低于所选内容插入题注。 可以是 WdCaptionPosition 常量之一。
ExcludeLabel可选Variant 没有文本标签,在标签参数中定义。 错误 包含指定的标签。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值