Capturing the heading via add-in

  • Thread starter msnews.microsoft.com
  • Start date
M

msnews.microsoft.com

We have a add-in in word 2007.

i want to know how to capture the heading in the selected page?

For examlple,:

============================
1 Heading 1

1.1 Heading 2
.......
.......

2 Heading 1

2.1 Heading 2

==============================

i want to capture the "Heading 1" when the cursor is moved to the first
line.


i try
Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text;


but it failed.



Any suggestion is very appreciated!!!
 
C

Cindy M.

Hi Msnews.microsoft.com,

"Headers" are what appears at the top of every page (if one has been defined for the
documetn). A Heading is something else (an outline structure, as you show). That's
why what you tried didn't work.

Given your requirement, I'd say your best bet would be to use the Range.Find.Execute
method and look for the style used to format Heading level 1 (usually this would be
Word's built-in style wdStyleHeading1). To restrict the search to just the current
page you can use Word's built-in \Page bookmark. Here's a bit of sample code to give
you an idea

Word.Application app = Globals.ThisAddIn.Application;
if (app.Documents.Count > 0)
{
object oPageBkm = @"\Page";
Word.Range rng = app.Selection.Bookmarks.get_Item(ref
oPageBkm).Range;
object oFindText = "";
object missing = System.Type.Missing;
object oFalse = false;
object oTrue = true;
object oWrapStop = Word.WdFindWrap.wdFindStop;
object oStyle = Word.WdBuiltinStyle.wdStyleHeading1;
rng.Find.ClearFormatting();
rng.Find.set_Style(ref oStyle);
rng.Find.Execute(ref oFindText, ref oFalse, ref oFalse, ref
oFalse, ref oFalse,
ref oFalse, ref oTrue, ref oWrapStop, ref oTrue, ref
missing, ref missing,
ref oFalse, ref oFalse, ref oFalse, ref oFalse);
MessageBox.Show(rng.Text);
}

We have a add-in in word 2007.

i want to know how to capture the heading in the selected page?

For examlple,:

============================
1 Heading 1

1.1 Heading 2
.......
.......

2 Heading 1

2.1 Heading 2

==============================

i want to capture the "Heading 1" when the cursor is moved to the first
line.


i try
Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Headers[Word.WdHeaderFooter
Index.wdHeaderFooterFirstPage].Range.Text;


but it failed.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in
the newsgroup and not by e-mail :)
 
S

silence

Great Thanks,Cindy!!!


Cindy M. said:
Hi Msnews.microsoft.com,

"Headers" are what appears at the top of every page (if one has been
defined for the
documetn). A Heading is something else (an outline structure, as you
show). That's
why what you tried didn't work.

Given your requirement, I'd say your best bet would be to use the
Range.Find.Execute
method and look for the style used to format Heading level 1 (usually this
would be
Word's built-in style wdStyleHeading1). To restrict the search to just the
current
page you can use Word's built-in \Page bookmark. Here's a bit of sample
code to give
you an idea

Word.Application app = Globals.ThisAddIn.Application;
if (app.Documents.Count > 0)
{
object oPageBkm = @"\Page";
Word.Range rng = app.Selection.Bookmarks.get_Item(ref
oPageBkm).Range;
object oFindText = "";
object missing = System.Type.Missing;
object oFalse = false;
object oTrue = true;
object oWrapStop = Word.WdFindWrap.wdFindStop;
object oStyle = Word.WdBuiltinStyle.wdStyleHeading1;
rng.Find.ClearFormatting();
rng.Find.set_Style(ref oStyle);
rng.Find.Execute(ref oFindText, ref oFalse, ref oFalse,
ref
oFalse, ref oFalse,
ref oFalse, ref oTrue, ref oWrapStop, ref oTrue,
ref
missing, ref missing,
ref oFalse, ref oFalse, ref oFalse, ref oFalse);
MessageBox.Show(rng.Text);
}

We have a add-in in word 2007.

i want to know how to capture the heading in the selected page?

For examlple,:

============================
1 Heading 1

1.1 Heading 2
.......
.......

2 Heading 1

2.1 Heading 2

==============================

i want to capture the "Heading 1" when the cursor is moved to the first
line.


i try
Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Headers[Word.WdHeaderFooter
Index.wdHeaderFooterFirstPage].Range.Text;


but it failed.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


This reply is posted in the Newsgroup; please post any follow question or
reply in
the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top