【问题标题】:Translate VBA syntax to Matlab for Activex control of Word document将 VBA 语法翻译成 Matlab 用于 Word 文档的 Activex 控制
【发布时间】:2021-09-03 03:58:25
【问题描述】:

我是在 matlab 中使用 activex 控件的新手。我试图控制一个word文档。我想我需要帮助在 VBA 语法和 Matlab 之间进行翻译。如何在 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

谢谢,我查看了帮助和来源,但我仍然感觉很紧张。我希望能够在自动报告中控制标题编号和标题文本。我正在使用表格和数字。我只是不太明白如何编写添加字幕的代码。

下面的代码让我走到了那一步。但我无法控制编号样式等。我试图弄清楚 activex 结构,但我无法理解它。尤其是上面VB子程序的第一位。

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

【问题讨论】:

    标签: matlab vba activex office-interop


    【解决方案1】:

    经过一番摆弄,我想我可以工作了:

    %# 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函数的参数顺序。

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

    最后,为了找出wd* 枚举的整数值,我使用ILDASM 工具来反汇编Office Interop 程序集(如this solution 建议的那样)。只需将整个内容转储到文本文件,然后搜索您要查找的字符串。

    【讨论】:

    • 非常感谢您的帮助。非常有用。
    【解决方案2】:

    查看基本 MATLAB 工具箱中actxserver 的帮助和xlsread.m 的源代码。如果您仍然遇到问题,请根据您的进度更新您的问题。

    编辑: 您需要查看 VBA 帮助,但第一部分应该可以通过以下方式实现:

    o = hdlWordDoc.CaptionLabels('Table');
    o.NumberStyle = <some number corresponding to wdCaptionNumberStyleArabic>;
    o.IncludeChapterNumber = true;
    o.ChapterStyleLevel = 1;
    o.Separator = <some number corresponding to wdSeparatorHyphen>;
    

    根据我的经验,您必须从枚举中获取值,例如来自 VBA 脚本的 wdCaptionNumberStyleArabic 和 wdSeparatorHyphen,然后对它们进行硬编码。您可以尝试以下方法,但我认为它不起作用:

    o.NumberStyle = 'wdCaptionNumberStyleArabic';
    o.Separator = 'wdSeparatorHyphen';
    

    【讨论】:

    • 感谢您的建议。这很有帮助。但是我仍然无法在 matlab 中通过 word 文档 activex 控件进行导航。似乎我应该能够将 VB 例程的第一部分编码为: hdlActiveX.CaptionLabels.Tables.Numberstyle = wdCaptionNumberStyleArabic 我尝试在 matlab 中导航 activex 控件,但找不到我的方式。如果这根本不清楚,我很抱歉。有时很难就我们难以理解的事情提出明确的问题。
    • 我试过你的建议:o = hdlWordDoc.CaptionLabels('Table');它返回一个错误。以下不会返回错误,但我无法破解关于我想要的控件埋在对象中的位置的代码。 o = hdlWordDoc.CaptionLabels;感谢所有的帮助。我现在需要切换项目,但会尽快返回。再次感谢。
    【解决方案3】:

    您可以使用枚举常量,而不是将文本值硬编码到代码中。这在安装不同语言的 Word 时会有所帮助。

    可以在此处找到枚举列表:https://docs.microsoft.com/en-us/office/vba/api/word(enumerations)

    所以而不是:

    Word.Selection.InsertCaption('Table', '', '', 0, false) %# wdCaptionPositionAbove

    你可以用这个:

    NET.addAssembly('Microsoft.Office.Interop.Word')
    Word.Selection.InsertCaption(...
        Microsoft.Office.Interop.Word.WdCaptionLabelID.wdCaptionTable.GetHashCode,...
         ' My custom table caption text', '', ...
      Microsoft.Office.Interop.Word.WdCaptionPosition.wdCaptionPositionAbove.GetHashCode, false)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      相关资源
      最近更新 更多