How do I change styles in a Word through actxserver.

조회 수: 8 (최근 30일)
Simon Aeschbacher
Simon Aeschbacher 2018년 8월 7일
댓글: Kishankumar Dholariya 2020년 4월 2일
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

답변 (1개)

Martijn
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
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
Kishankumar Dholariya
Kishankumar Dholariya 2020년 4월 2일
Did you get the Solution?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by