Search code examples
c#ms-wordoffice-interop

how to move to the next section in word using c#


I want to know how can I move to the section in my document using C#.

I have 3 or more sections in my document. I know how to move one page at a time using:

object lineCount = Word.WdUnits.wdLine;

object countPage = 1;

wordApp.Selection.MoveDown(ref lineCount, ref countPage, ref MISSING);

I'll try to loop until I found a new section with this:

while() //condition to check if found a new section

{

    wordApp.Selection.MoveDown(ref lineCount, ref countPage, ref MISSING);    

}

But I don't know what condition should I use.

I hope someone here can help me, I you have better solution please let me know.tnx


Solution

  • In VBA code it is

    Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Count:=1
    

    or even simpler

    Selection.GoToNext wdGotoSection
    

    In C# you could use

    wordApp.Selection.GoToNext(Word.WdGoToItem.wdGoToSection);
    

    You don't have to create an object for Word.WdGoToItem.wdGoToSection.