2011-09-27 312 views
1

我是在MATLAB中使用activex控件的新手。我試圖控制一個word文檔。我想,我需要幫助翻譯VBA語法和Matlab。如何在MATLAB中對以下代碼進行編碼?將VBA語法轉換爲Matlab,用於Word文檔的Activex控件

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); 

回答

2

一些擺弄之後,我覺得我得到它的工作:

%# 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,否則這個詞是印"Error! No text of specified style in document"字幕文本內...

最後,找出wd*枚舉的整數值,我使用ILDASM工具拆卸Office Interop程序集(如this solution建議)。只需將整個內容轉儲到文本文件中,然後搜索您正在查找的字符串。

ildasm

+0

非常感謝您的幫助。很有用。 –

0

看一看的幫助actxserver,並在基地MATLAB工具箱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'; 
+0

感謝您的意見。這很有幫助。但我仍然在matlab中通過單詞文檔activex控件進行導航。看起來我應該能夠編碼VB例程的第一部分,如下所示: hdlActiveX.CaptionLabels.Tables.Numberstyle = wdCaptionNumberStyleArabic 我已經嘗試通過matlab中的activex控件導航,但無法找到我的方式。我很抱歉,如果這根本不清楚。有時很難問清楚我們很難理解的事情。 –

+0

我試過你的建議: o = hdlWordDoc.CaptionLabels('Table'); 它返回一個錯誤。以下內容不會返回錯誤,但我無法破解關於我想要的控件埋在對象中的代碼。 o = hdlWordDoc。CaptionLabels; 感謝您的幫助。我需要現在切換項目,但會盡快返回到此項目。再次感謝。 –