How can I set the MoveDown method to move down Paragraphs/Headings?

8 次查看(过去 30 天)
Hi, I'm using actxserver('Word.Application') to copy data from Matlab to a Word template. And I'm having problems with the navigation in the document.
%Initialization
word=actxserver('Word.Application');
document=word.Documents.Open([pwd '\Report_Generation\name_of_report.docx']);
word.Visible=1;
selection=word.Selection;
If I write
selection.MoveDown
or
invoke(selection,'MoveDown');
Then it's working, the selection moves down a line in the Word doc.
But if I would like the selection to move down paragraphs (or headings), not lines, and I set the arguments to wdParagraph I get an error. (even if i set the unit to wdLine, which is the default value).
>> selection.MoveDown(wdParagraph)
Undefined function or variable 'wdParagraph'.
>> selection.MoveDown(Microsoft.Office.Interop.Word.WdUnits.wdParagraph, 1, Microsoft.Office.Interop.Word.WdMovementType.wdMove)
Undefined variable "Microsoft" or function "Microsoft.Office.Interop.Word.WdUnits.wdParagraph".
>> invoke(selection,'MoveDown',wdParagraph);
Undefined function or variable 'wdParagraph'.
Do You know how to set properly the arguments of these methods?
  1 个评论
Ivan
Ivan 2014-12-5
Also if I use NET.addAssembly it is working, but i have to stay with actxserver.
NET.addAssembly('microsoft.office.interop.word');
word = Microsoft.Office.Interop.Word.ApplicationClass;
document = word.Documents.Open([pwd '\Report_Generation\name_of_report.docx']);
word.Visible = true;
selection = word.Selection;
selection.MoveDown(Microsoft.Office.Interop.Word.WdUnits.wdParagraph, 1, Microsoft.Office.Interop.Word.WdMovementType.wdMove);

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2014-12-5
Matlab is hopeless with COM enumerations, they don't get imported. As a result instead of using the enumaration names, you have to use the values.
Unfortunately, as well, MSDN is not very forthcoming on these values. I had to use Visual Studio object browser to get them. They are:
WdUnits.
wdCell = 12
wdCharacter = 1
wdCharacterFormatting = 13
wdColumn = 9
wdItem = 16
wdLine = 5
wdParagraph = 4
wdParagraphFormatting = 14
wdRow = 10
wdScreen = 7
wdSection = 8
wdSentence = 3
wdStory = 6
wdTable = 15
wdWindow = 11
wdWord = 2
WdMovementType.
wdExtend = 1
wWdMove = 0
Thus:
selection.MoveDown(4, 1, 0);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by