How do I change styles in a Word through actxserver.

23 views (last 30 days)
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

Answers (1)

Martijn
Martijn on 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 Comments
Kanchibhotla Chandra Sekhar
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

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!