function ceshi_Word
%
设定测试
Word
文件名和路径
filespec_user=[pwd '\
测试
.doc'];
%
判断
Word
是否已经打开,若已打开,就在打开的
Word
中进行操作,
%
否则就打开
Word
try
Word = actxGetRunningServer('Word.Application');
catch
Word = actxserver('Word.Application');
end;
%
设置
Word
属性为可见
set(Word, 'Visible', 1);
%
返回
Word
文件句柄
documents = Word.Documents;
%
若测试文件存在,打开该测试文件,否则,新建一个文件,并保存,文件名为测试
.doc
if exist(filespec_user,'file');
document = invoke(documents,'Open',filespec_user);
else
document = invoke(documents, 'Add');
document.SaveAs(filespec_user);
end
content = document.Content;
selection = Word.Selection;
paragraphformat = selection.ParagraphFormat;
%
页面设置
(
上下左右边距
)
document.PageSetup.TopMargin = 60;
document.PageSetup.BottomMargin = 45;
document.PageSetup.LeftMargin = 45;
document.PageSetup.RightMargin = 45;
%
设定内容起始位置和标题
set(content, 'Start',0);
title='
测
试
文
件
';
set(content, 'Text',title);
set(paragraphformat, 'Alignment','wdAlignParagraphCenter');
%
居中
% set(paragraphformat, 'Alignment','wdAlignParagraphLeft');
%
居左
% set(paragraphformat, 'Alignment','wdAlignParagraphRight');
%
居右
%
设定标题字体格式
rr=document.Range(0,10);
rr.Font.Size=18;
%
字体大小设置
rr.Font.Bold=4;
%
设置字体加粗
%
设定下面内容的起始位置(将光标放在最后边)
end_of_doc = get(content,'end');
set(selection,'Start',end_of_doc);