将VBA语法转换为Matlab,以对Word文档进行Activex控件 [英] Translate VBA syntax to Matlab for Activex control of Word document

查看:237
本文介绍了将VBA语法转换为Matlab,以对Word文档进行Activex控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在matlab中使用Activex控件的新手.我正在尝试控制Word文档.我认为我需要在VBA语法和Matlab之间转换的帮助.如何在matlab中编写以下代码?

I am a newbie to using activex controls in matlab. Am trying to control a word document. I need help translating between VBA syntax and Matlab, I think. How would one code the following in matlab?

Sub macro()
With CaptionLabels("Table")
        .NumberStyle = wdCaptionNumberStyleArabic
        .IncludeChapterNumber = True
        .ChapterStyleLevel = 1
        .Separator = wdSeparatorHyphen
End With

Selection.InsertCaption Label:="Table", TitleAutoText:="", Title:="", _
        Position:=wdCaptionPositionAbove, ExcludeLabel:=0
End Sub

谢谢,我看着帮助和消息来源,但我仍然感到很困惑.我希望能够在自动报告中控制字幕编号和字幕文本.我正在使用表格和数字.我只是不太了解如何编写字幕的编码.

Thanks, I looked at the help and the source but I am still feeling dense. I want to be able to control caption numbering and caption text in an automated report. Am using Tables and figures. I just can't quite get my head around how to code the addition of the captions.

以下代码将我带到了那里.但是我无法控制编号样式等.我试图弄清楚activex结构,但我无法理解.特别是上面VB子例程的第一位.

The following code gets me part way there. But I don't have control over numbering style, etc,. I have tried to figure out the activex structure but I can't make sense of it. In particular, In particular the first bit the VB subroutine above.

% Start an ActiveX session with Word
hdlActiveX = actxserver('Word.Application');
hdlActiveX.Visible = true;
hdlWordDoc = invoke(hdlActiveX.Documents, 'Add');
hdlActiveX.Selection.InsertCaption('Table',captiontext);

推荐答案

经过一番摆弄之后,我认为我可以使用它:

After some fiddling, I think I got it to work:

%# open Word
Word = actxserver('Word.Application');
Word.Visible = true;

%# create new document
doc = Word.Documents.Add;

%# set caption style for tables
t = Word.CaptionLabels.Item(2); %# 1:Figure, 2:Table, 3:Equation
t.NumberStyle = 0;              %# wdCaptionNumberStyleArabic
t.IncludeChapterNumber = false;
t.ChapterStyleLevel = 1;
t.Separator = 0;                %# wdSeparatorHyphen

%# insert table caption for current selection
Word.Selection.InsertCaption('Table', '', '', 0, false) %# wdCaptionPositionAbove

%# save document, then close
doc.SaveAs2( fullfile(pwd,'file.docx') )
doc.Close(false)

%# quit and cleanup
Word.Quit
Word.delete

请参阅MSDN文档以了解如何使用此API.例如, InsertCaption 函数.

Refer to the MSDN documentation to learn how to use this API. For example, the order of arguments of the InsertCaption function used above.

请注意,我必须将IncludeChapterNumber设置为false,否则Word在标题文本内打印"Error! No text of specified style in document" ...

Note that I had to set IncludeChapterNumber to false, otherwise Word was printing "Error! No text of specified style in document" inside the caption text...

最后,要找出wd*枚举的整数值,我正在使用ILDASM工具来反汇编Office Interop程序集(如

Finally, to find out the integer values of the wd* enums, I am using the ILDASM tool to disassemble the Office Interop assemblies (as this solution suggested). Simply dump the whole thing to text file, and search for the strings you are looking for.

这篇关于将VBA语法转换为Matlab,以对Word文档进行Activex控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆