Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2006

Tip: Looking for answers? Try searching our database.

Place cursor at the end of a section

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Faulhaber - 10 May 2006 12:15 GMT
Hi All,

I have a document with multiple sections. The first page is a TOC and the
last one is the INDEX.

The user can add sections in the document. VBA code is working fine. The VBA
code is searching for the end of section and add a section break

BUT when a user want to add a section after INDEX, the section is added
after the TOC?

The VBA code is
Sub Sectie_Einde()
' Opzoek naar het einde van de sectie

   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = "^b"
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindContinue
       .CorrectHangulEndings = True
   End With
   
   Selection.Find.Execute
   
   Selection.MoveLeft Unit:=wdCharacter, Count:=1
   Selection.TypeParagraph
   Selection.InsertBreak Type:=wdSectionBreakNextPage
   
End Sub
Jezebel - 10 May 2006 12:32 GMT
Forget all that nonsense. All you need is --

ActiveDocument.Sections(ActiveDocument.Sections.Count - 1).Range.InsertBreak

> Hi All,
>
[quoted text clipped - 28 lines]
>
> End Sub
Greg Maxey - 10 May 2006 13:04 GMT
Jezebel,

Wouldn't he need a bit more than that?  First that errors if there is
only one section.  If there are two sections it deletes one and puts in
a page break.

Perhaps:

Sub Test()
With Selection.Sections(1).Range
 .Collapse wdCollapseEnd
 .InsertBreak wdSectionBreakNextPage
End With
End Sub
Dave Lett - 10 May 2006 13:14 GMT
Hi Peter,

Maybe this will be useful:

Dim iSec As Integer
Dim oRng As Range
Set oRng = Selection.Range
oRng.Start = ActiveDocument.Range.Start
iSec = oRng.Sections.Count

If iSec = ActiveDocument.Sections.Count Then
   MsgBox "You cannot add a section after " & _
   "the index. Place the cursor in a section " & _
   "that comes before the index and run the " & _
   "routine again.", vbInformation
   End
End If

Selection.EndOf _
   Unit:=wdSection, _
   Extend:=wdMove
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.TypeText Text:="New Section"

HTH,
Dave
> Hi All,
>
[quoted text clipped - 28 lines]
>
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.