How do I change styles in a Word through actxserver.
8 次查看(过去 30 天)
显示 更早的评论
Hi I created a macro in word to get the code to change the style in my word. the returned code is:
Selection.Style = ActiveDocument.Styles("Heading 1")
I tried to use this code:
word = actxserver('Word.Application');
word.visible = 1;
word.Documents.Add([pwd, '\test.docx']);
pause(0.1);
word.Selection.GoTo(1,1); % go to top
word.Selection.Find.Forward = 1;
word.Selection.Find.Wrap = 1;
word.Selection.Find.Text = 'StrToFormat';
selectionFound = word.Selection.Find.Execute;
word.Selection.Style = word.ActiveDocument.Styles('Heading 1');
word.Quit(0);
Unfortunate I get this error.
Index exceeds array bounds.
Error in test (line 18)
word.Selection.Style = word.ActiveDocument.Styles('Heading 1');
Do any body knows how to change the style of selected text trough actxserver.
Im using Office 2016
Thanks S
0 个评论
回答(1 个)
Martijn
2018-8-8
In COM/VBA there is a concept called "default member", which is the member that gets called if you do not specify a specific method/property to be called when interacting with an object. In MATLAB that concept does not exist, and you need to explicitly call the correct method or property. For example, for a Styles collection object the default member is Item; see this screenshot taken from the Object Browser in the VBA editor in Word:
Meaning that if you write:
ActiveDocument.Styles("Heading 1")
What you are really calling is:
ActiveDocument.Styles.Item("Heading 1")
So, what you would need to write in MATLAB is also:
word.Selection.Style = word.ActiveDocument.Styles.Item('Heading 1');
2 个评论
Kanchibhotla Chandra Sekhar
2019-8-11
How can i add numbering to these Headings like -
1. Heading 1
1.1 Heading 2
1.1.1 Heading 3.... so on
I am able to create the Heading, but tried so many ways to invoke functions like List Level Number etc. but number to do the numbering to Headings.
Is there any way to provide numbering system for these Headings
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!