How do I change styles in a Word through actxserver.

23 vues (au cours des 30 derniers jours)
Simon Aeschbacher
Simon Aeschbacher le 7 Août 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']);
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

Réponses (1)

Martijn
Martijn le 8 Août 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 commentaires
Kanchibhotla Chandra Sekhar
Kanchibhotla Chandra Sekhar le 11 Août 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 le 2 Avr 2020
Did you get the Solution?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Use COM Objects in MATLAB dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by