Filter löschen
Filter löschen

How do I change styles in a Word through actxserver.

20 Ansichten (letzte 30 Tage)
Simon Aeschbacher
Simon Aeschbacher am 7 Aug. 2018
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']);
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');
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

Antworten (1)

Martijn am 8 Aug. 2018
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 Kommentare
Kanchibhotla Chandra Sekhar
Kanchibhotla Chandra Sekhar am 11 Aug. 2019
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
Kishankumar Dholariya
Kishankumar Dholariya am 2 Apr. 2020
Did you get the Solution?

Melden Sie sich an, um zu kommentieren.


Mehr zu Use COM Objects in MATLAB finden Sie in Help Center und File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by