Group: microsoft.public.word.vba.general
From: =?Utf-8?B?QXNzb2NpYXRlcw==?=
Date: Monday, March 24, 2008 5:53 PM
Subject: Re: trouble in inserting a section to the report

Hi Doug,

yes, i didn't apply the style to the selection. sorry

I have modified the code as follows

Dim srange As Range
Set srange = Selection.Sections(1).Range
srange.Collapse wdCollapseEnd
srange.InsertBreak wdSectionBreakNextPage

srange.Style = ActiveDocument.Styles("Section Heading 1")

srange.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _
wdOutlineNumberGallery).ListTemplates(7),
ContinuePreviousList:=True, _
ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
wdWord10ListBehavior

srange.Text = InputBox("Please enter:", "Enter Section Heading")

Everything works fine except that there is still a problem with the
continuation of the Sections. it added a new section but started from Section
1 (when it should be "Section 2"). Not sure if this is to do with the
autotext. I actually used autotext to perform the insertion of "Section 1" at
the start and then, i use this code to allow user to insert more sections to
the report.

Thank you in advance

"Doug Robbins - Word MVP" wrote:

> You are applying the style to the Selection which at that point is not in
> the new Section.
>
> --
> Hope this helps.
>
> Please reply to the newsgroup unless you wish to avail yourself of my
> services on a paid consulting basis.
>
> Doug Robbins - Word MVP
>
> "Associates" wrote in message
> news:96C7EF98-C38A-4EC7-8979-633DC71D80F9@microsoft.com...
> > Hi Doug,
> >
> > No, the break was inserted in the right place which is in the next page
> > (that's what i expected). However, what it did not do was it didn't put
> > the
> > "Section 2" in the next page. What i want to see is that when user clicks
> > on
> > "add new section button", he/she is prompted for the heading. As soon as
> > he
> > hits the "OK" button, it should insert a new page break as well as putting
> > whatever the next Section number to that new page.
> >
> > this is the code i've got so far.
> > Sub InsertSection()
> > Dim srange As Range
> > Set srange = Selection.Sections(1).Range
> > srange.Collapse wdCollapseEnd
> > srange.InsertBreak wdSectionBreakOddPage
> >
> > Selection.Style = ActiveDocument.Styles("Section Heading 1")
> >
> > Selection.TypeText Text:=InputBox("Please enter the section heading:",
> > "Enter Section Heading")
> > Selection.TypeParagraph
> > End Sub
> >
> > Your help is greatly appreciated.
> >
> > Thank you in advance
> >
> >
> >
> >
> >
> > "Doug Robbins - Word MVP" wrote:
> >
> >> Are you saying that the break was inserted in the wrong place? That is
> >> not
> >> at the end of the section in which the selection was located?
> >>
> >> If you do not want the new section to start on a new page, change the
> >> wdSectionBreakNextPage to wdSectionBreakContinuous
> >>
> >> --
> >> Hope this helps.
> >>
> >> Please reply to the newsgroup unless you wish to avail yourself of my
> >> services on a paid consulting basis.
> >>
> >> Doug Robbins - Word MVP
> >>
> >> "Associates" wrote in message
> >> news:6C8E1D13-65C3-4416-94AE-0E13BAE79E74@microsoft.com...
> >> > Thank you, Doug for your reply.
> >> >
> >> > I tested it but it still didn't solve the problem. However, it did put
> >> > a
> >> > new
> >> > page break to it. I might have done it wrong.
> >> >
> >> > This is what i did
> >> > Sub InsertNewSection()
> >> >
> >> > Dim srange As Range
> >> > Set srange = Selection.Sections(1).Range
> >> > srange.Collapse wdCollapseEnd
> >> > srange.InsertBreak wdSectionBreakNextPage
> >> >
> >> > 'Selection.InsertBreak Type:=wdSectionBreakOddPage
> >> > 'Selection.Style = ActiveDocument.Styles("Section Heading 1")
> >> >
> >> > 'ListGalleries(wdOutlineNumberGallery).ListTemplates(7).Name = ""
> >> > 'Selection.Range.ListFormat.ApplyListTemplate
> >> > ListTemplate:=ListGalleries( _
> >> > wdOutlineNumberGallery).ListTemplates(7),
> >> > ContinuePreviousList:=True, _
> >> > ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
> >> > wdWord10ListBehavior
> >> >
> >> > Selection.TypeText Text:=InputBox("Please enter the section
> >> > heading:",
> >> > "Enter Section Heading")
> >> > Selection.TypeParagraph
> >> >
> >> > Selection.Font.Name = "Tahoma"
> >> > End Sub
> >> >
> >> > It didn't put the page break before "Section 2" is added hence causing
> >> > Section 2 sits on the same page as Section 1.
> >> >
> >> > Your help is appreciated.
> >> >
> >> > Thank you in advance
> >> >
> >> >
> >> >
> >> > "Doug Robbins - Word MVP" wrote:
> >> >
> >> >> The following code will insert a section break after the section in
> >> >> which
> >> >> the selection is currently located:
> >> >>
> >> >> Dim srange As Range
> >> >> Set srange = Selection.Sections(1).Range
> >> >> srange.Collapse wdCollapseEnd
> >> >> srange.InsertBreak wdSectionBreakNextPage
> >> >>
> >> >>
> >> >> --
> >> >> Hope this helps.
> >> >>
> >> >> Please reply to the newsgroup unless you wish to avail yourself of my
> >> >> services on a paid consulting basis.
> >> >>
> >> >> Doug Robbins - Word MVP
> >> >>
> >> >> "Associates" wrote in message
> >> >> news:887BAB99-C0D7-46BE-9BB1-59DC50EE9229@microsoft.com...
> >> >> > Hi,
> >> >> >
> >> >> > I was wondering if anyone might be able to help me here with the
> >> >> > insertion
> >> >> > of a new section in VBA. This is for word 03.
> >> >> >
> >> >> > My aim here is to make it easier for our staff to write up a report
> >> >> > document
> >> >> > for their client. Because so far we have a lot of inconsistencies in
> >> >> > our
> >> >> > staff's reports. One may use a different style or font size and so
> >> >> > on.
> >> >> > So
> >> >> > i
> >> >> > was asked to write a program in word VBA to mitigate the problem. I
> >> >> > got
> >> >> > some
> >> >> > advice from this forum a while ago that some suggested me to use
> >> >> > autotext
> >> >> > (for it's easier than programming it from scratch). so i have used
> >> >> > it
> >> >> > and
> >> >> > must confess that it does make it easier to achieve what i wanted
> >> >> > without
> >> >> > using extensive programming here.
> >> >> >
> >> >> > So the report consists of as follows in that order
> >> >> > Covering page
> >> >> > TOC
> >> >> > Executive summary (optional)
> >> >> > Sections
> >> >> > Bibliography (optional)
> >> >> > Glossary (optional)
> >> >> > Appendixes (optional)
> >> >> >
> >> >> > Anyway, i made up a userform that would allow user to choose which
> >> >> > of
> >> >> > those
> >> >> > sections they need to make up their report. so that works fine.
> >> >> >
> >> >> > The issue i'm having here is in "sections" section. In here, this is
> >> >> > what
> >> >> > it
> >> >> > looks like
> >> >> > Section 1 Inventory Re-structuring
> >> >> > whereby Section 1 is automatically generated by word and Inventory
> >> >> > Re-structuring is what user types in.
> >> >> >
> >> >> > Now, i have a macro program here called "insert a section". This
> >> >> > will
> >> >> > add
> >> >> > a
> >> >> > new section to the report in the "sections" section.
> >> >> >
> >> >> > The problem is when clicking on "insert a section". It put in the
> >> >> > section
> >> >> > but not continuation.
> >> >> > For example. we have Section 1 (at the start), then add a new
> >> >> > section -
> >> >> > it
> >> >> > should be Section 2 ... But it doesn't, it puts in Section 1 ... and
> >> >> > of
> >> >> > course this causes a problem in the page numbering in the footer.
> >> >> > Instead
> >> >> > of
> >> >> > getting page 2, it says page 1. How do i work around this?
> >> >> >
> >> >> > Here is the code for inserting a section
> >> >> > Sub InsertNewSection()
> >> >> > Selection.InsertBreak Type:=wdSectionBreakOddPage
> >> >> > Selection.Style = ActiveDocument.Styles("Section Heading 1")
> >> >> >
> >> >> > ListGalleries(wdOutlineNumberGallery).ListTemplates(7).Name = ""
> >> >> > Selection.Range.ListFormat.ApplyListTemplate
> >> >> > ListTemplate:=ListGalleries( _
> >> >> > wdOutlineNumberGallery).ListTemplates(7),
> >> >> > ContinuePreviousList:=True, _
> >> >> > ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
> >> >> > wdWord10ListBehavior
> >> >> >
> >> >> > Selection.TypeText Text:=InputBox("Please enter the section
> >> >> > heading:",
> >> >> > "Enter Section Heading")
> >> >> > Selection.TypeParagraph
> >> >> >
> >> >> > Selection.Font.Name = "Tahoma"
> >> >> > End Sub
> >> >> >
> >> >> > Sub marginstyle()
> >> >> > StyleName = "Section Heading 1"
> >> >> >
> >> >> > For Each oStyle In ActiveDocument.Styles
> >> >> > If oStyle.NameLocal = StyleName Then GoTo Setup
> >> >> > Next oStyle
> >> >> > ActiveDocument.Styles.Add Name:=StyleName,
> >> >> > Type:=wdStyleTypeParagraph
> >> >> >
> >> >> > Setup:
> >> >> > With ActiveDocument.Styles(StyleName)
> >> >> > .AutomaticallyUpdate = False
> >> >> > .BaseStyle = ActiveDocument.Styles(wdStyleHeading1)
> >> >> > .NextParagraphStyle = "Normal"
> >> >> > .Font.Name = "Tahoma"
> >> >> > End With
> >> >> >
> >> >> > With ActiveDocument.Styles(StyleName).Font
> >> >> > .Size = 20
> >> >> > .ColorIndex = wdGreen
> >> >> > .Name = "Tahoma"
> >> >> > End With
> >> >> >
> >> >> > Selection.Font.Name = "Tahoma"
> >> >> > End Sub
> >> >> >
> >> >> > I don't know how to stop it from starting at 1. Thank you very much
> >> >> > in
> >> >> > advance
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>
>