none
Find Section Breaks RRS feed

Answers

  • Assuming you want to operate on the page where the cursor is, then, for example, ...
     
        Selection.Bookmarks("\page").Range
     
    .. will get you the Range of the full page, and .....
     
        Selection.Bookmarks("\page").Range.Characters.Last
     
    .. will get the last character on the page, and ...
     
        Selection.Bookmarks("\page").Range.Characters.First.Previous
     
    .. will get the last character on the previous page.
     
    Checking a character for being equal to ChrW(12) will check for a section break - or a hard page break, so some further checking is needed - the Section Index of the characters would be one possibility.
     
    You know your own documents but you might need to check for a paragraph mark after a hard page break, and so on, and so on. This kind of manipulation is pretty involved if it is to be done properly for the general case.
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by dexter_cs Friday, May 27, 2011 11:24 AM
    Thursday, May 26, 2011 5:03 PM

All replies

  • Try

     

    Sub FindNextSection()
      Selection.Collapse Direction:=wdCollapseEnd ' or wdCollapseStart
      With Selection.Find
        .Text = "^b"
        .Replacement.Text = ""
        .Forward = True ' or False
        .Wrap = wdFindStop
        .Format = False
        If .Execute Then
          MsgBox "Found!", vbExclamation
        End If
      End With
    End Sub

    and

    Sub FindPreviousSection()
      Selection.Collapse Direction:=wdCollapseStart
      With Selection.Find
        .Text = "^b"
        .Replacement.Text = ""
        .Forward = False
        .Wrap = wdFindStop
        .Format = False
        If .Execute Then
          MsgBox "Found!", vbExclamation
        End If
      End With
    End Sub


    Regards, Hans Vogelaar
    Wednesday, May 25, 2011 5:41 AM
  • hi thanks for the reply

    can i get a range object which will fod the Find result..

    basically i am interested in the Location and the Type of Section break..

    is there any way we can find this information..

    Wednesday, May 25, 2011 5:47 AM
  • Hi "dexter"

    If you use a Range object, rather than Selection, with Find.Execute then if it's successful the Range object now reflects the found range.


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 25, 2011 9:05 AM
    Moderator
  • The type of the section break is a property of the section and you don't need to find the break to discover that. The location (if you need it) of the break(s) can be got from the section range.
     
    Perhaps you could tell us what you are trying to achieve, to enable us to provide the best solution for you.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Wednesday, May 25, 2011 12:10 PM
  • thank you  all,  for replying..

     

    Basically i have a Portected Document ,

    and in that i wannt to  giving the User Functionality like "Insert Portrait Page" "Insert Landscape Page" & "Delete Page" (these are the Buttons on the custom made ribbon )

    the Insert Page Logic is Adding the Section Breaks in Protected areas so that the User Cant delete them.(this is because the Layout of Each Page of Document is Important and we want to put some ristriction on that)

    the Issue that i am facing is in delete Page...

     

    So i Need to Know all this Information to delete pages\section breaks so that the Layout is not Disturbed. (Please correct me if am Wrong..)

    Information like.

    What are the Oriontation of pages before and After the selection(the page which is Under selection need to be deleted.)

    What are the Types of Section Breaks before and After the Seletion.

     

     

    Wednesday, May 25, 2011 2:34 PM
  • thank you  all,  for replying..

     

    Basically i have a Portected Document ,

    and in that i wannt to  giving the User Functionality like "Insert Portrait Page" "Insert Landscape Page" & "Delete Page" (these are the Buttons on the custom made ribbon )

    the Insert Page Logic is Adding the Section Breaks in Protected areas so that the User Cant delete them.(this is because the Layout of Each Page of Document is Important and we want to put some ristriction on that)

    the Issue that i am facing is in delete Page...

     

    So i Need to Know all this Information to delete pages\section breaks so that the Layout is not Disturbed. (Please correct me if am Wrong..)

    Information like.

    What are the Oriontation of pages before and After the selection(the page which is Under selection need to be deleted.)

    What are the Types of Section Breaks before and After the Seletion.

     

    Wednesday, May 25, 2011 2:34 PM
  • Hi "dexter"

    If I'm following correctly you should be able to get the required information for each section from the Sections collection. Something like

    foreach (Word.Section sec in doc.Sections)
    {
        Word.WdOrientation secOrientation = sec..PageSetup.Orientation;
        Word.WdSectionStart secType = sec.PageSetup.SectionStart;
    }


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 25, 2011 4:35 PM
    Moderator
  • I see, now, why you want to know where the breaks are. Deleting a page containing a Section Break is asking for trouble!
     
    A page is a fairly fluid concept in Word but it is rare to have section breaks within one - as opposed to at the end of one - unless there is a special reason (changing the number of text columns perhaps) and removing such a break without removing the whole section (and even then) is likely to disturb the layout of several other pages.
     
    Deleting a page with a section break at the end - in other words the last page in a section can be done - carefully, very carefully. Before deleting a page, check the last character on the page for being a section break - and, if it is, then delete all but it from the page. Having done that, you'll need to check the last character of the previous page for being a hard page break and, if so, delete that as well (otherwise you'll be leaving a blank page). Next, check the last character of the previous page for being a section break - if it is you'll be leaving an empty section (and probably a blank page), but, before deleting the section break you will need to check for any following headers and footers that are 'same as previous' in case you delete what was the 'previous'.
     
    I don't think page orientation should be a particular issue. It is a section property - defaulted on creation to that of the previous section, but thereafter a stand-alone section property and deleting a section won't affect the orientation of any other section, although deleting a section break without deleting the entire section (as per above) really is something to be avoided.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Wednesday, May 25, 2011 5:27 PM
  • hey this is amazing...thanks.. for the reply...

    how can we check what is a perticular Charatecter 

    i mean how can we check if the last character of the previous page is a section Break or not..?

    Thursday, May 26, 2011 1:55 PM
  • Assuming you want to operate on the page where the cursor is, then, for example, ...
     
        Selection.Bookmarks("\page").Range
     
    .. will get you the Range of the full page, and .....
     
        Selection.Bookmarks("\page").Range.Characters.Last
     
    .. will get the last character on the page, and ...
     
        Selection.Bookmarks("\page").Range.Characters.First.Previous
     
    .. will get the last character on the previous page.
     
    Checking a character for being equal to ChrW(12) will check for a section break - or a hard page break, so some further checking is needed - the Section Index of the characters would be one possibility.
     
    You know your own documents but you might need to check for a paragraph mark after a hard page break, and so on, and so on. This kind of manipulation is pretty involved if it is to be done properly for the general case.
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by dexter_cs Friday, May 27, 2011 11:24 AM
    Thursday, May 26, 2011 5:03 PM
  • can you please show how we can use this ChrW(12) from C#.
    Monday, June 6, 2011 6:16 AM
  • can you please show how we can use this ChrW(12) from C#.

    Could you please be more specific about what you mean by "use"?
    Cindy Meister, VSTO/Word MVP
    Monday, June 6, 2011 7:44 AM
    Moderator
  • How can i Check the the Text  with ChrW(12)

     

    in the reply from tony.. it is mentioned that 

    "Checking a character for being equal to ChrW(12) will check for a section break - or a hard page break, so some further checking is needed - the Section Index of the characters would be one possibility."

     

    Just wanted to Know the C# implementation of how we can Compare Chracters from Rage object.

    Monday, June 6, 2011 11:53 AM
  • ChrW in VBA means the character with value 12 (0xC), or more properly the wide (i.e 2-byte, UCS-2) character with value 12. I don't know C# -- '\x0012', perhaps, or char(12)?
     
     

    Enjoy,
    Tony
    www.WordArticles.com
    Monday, June 6, 2011 12:18 PM
  • Yes, char(12) will work.
    Cindy Meister, VSTO/Word MVP
    Monday, June 6, 2011 2:09 PM
    Moderator