trouble in inserting a section to the report

A

Associates

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
 
D

Doug Robbins - Word MVP

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
 
A

Associates

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
 
D

Doug Robbins - Word MVP

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
 
A

Associates

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
 
D

Doug Robbins - Word MVP

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
 
A

Associates

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
 
D

Doug Robbins - Word MVP

Can you clarify what you mean by:

" it added a new section but started from Section 1 (when it should be
"Section 2"). "

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

Definitely inserts a section break after the section in which the selection
is located.

However, to be really sure that your subsequent commands are executing on
the desired part of the document, you may need to use

Dim srange As Range
Dim ssection As Section
Dim i As Long
i = Selection.Information(wdActiveEndSectionNumber)
Set srange = Selection.Sections(1).Range
srange.Collapse wdCollapseEnd
srange.InsertBreak wdSectionBreakNextPage
Set srange = ActiveDocument.Sections(i + 1).Range
srange.Collapse wdCollapseStart
srange.Text = "Test"



--
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
 
A

Associates

Thank you Doug, for your reply.

Doug, there is nothing wrong with the code you wrote down there. It inserts
a section break and puts in "Test" text on the new page. The problem i'm
having is that as follows (sorry, not good at explaining so i'd try my best
to explain what i mean to you)

remember when i said in the earlier post that i use autotext method to
insert page for Coverpage, TOC, Executive Summary, Sections, and Bibliography
respectively to make up a complete report. Each of these pages have their own
header and footer, different from one another. So i decided to use autotext.
So imagine, when user double-clicks on the template dot file called
"Myreport", a userform comes up with a list of those components and "Generate
report" button. Once, the button is pressed, all these components are
inserted into the document as follows
Coverpage
TOC
Executive Summary
Section 1.
Bibliography

The problem i'm having here is the "Section 1. ". This "Section 1" is
created through Customised Outline Numbered List. So at the start, the
"Section 1. " is already there. Now, i have a button called "Insert New
Section". When user wants to click it it should add a new Section (which is
the next after whatever the previous Section is) on the next page. So in this
case, we have got "Section 1. " already. The new Section should be "Section
2." on the next page.

The code that you wrote there for me does insert the section break (a new
page). Then, i modified it as below to suit my need.

Dim srange As Range
Dim ssection As Section
Dim i As Long
i = Selection.Information(wdActiveEndSectionNumber)

Set srange = Selection.Sections(1).Range

srange.Collapse wdCollapseEnd
srange.InsertBreak wdSectionBreakNextPage

Set srange = ActiveDocument.Sections(i + 1).Range
srange.Collapse wdCollapseStart

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

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

srange.Text = "Test"

The result after running this code is that it inserts a new section break as
well as inserts "Section 1. Test". The problem here is it should be "Section
2. Test", not "Section 1. Test". Because "Section 1." is already there right
from the start.

It seems to be unable to track which Section number it's at.

Sorry for this lengthy explanation (can't think of a better way). I hope
this would help you understand my situation here.

Thank you once again in advance
 
D

Doug Robbins - Word MVP

I don' understand why you would be using this code

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

According to the Visual Basic Help file, the ListFormat property of a range
is Read Only. That however may not be correct as the examples in the Help
file would seem to indicate otherwise. Also, according to the Visual Basic
Help file in Word 2007, there are only two DefaultListBehaviours -
wdWord8ListBehaviour and wdWord9ListBehaviour

Don't you have the numbering setup in the Section Heading 1 style?

--
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
 
D

Doug Robbins - Word MVP

Sorry, I don't understand you question. I will say however that you should
the styles that you want to use defined in the template and then you should
just apply the applicable style.

--
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
 

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