MS Office Forum / Word / Programming / March 2008
trouble in inserting a section to the report
|
|
Thread rating:  |
Associates - 19 Mar 2008 03:18 GMT 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
Doug Robbins - Word MVP - 19 Mar 2008 04:43 GMT 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
 Signature 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
> Hi, > [quoted text clipped - 92 lines] > I don't know how to stop it from starting at 1. Thank you very much in > advance Associates - 19 Mar 2008 05:54 GMT 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
> The following code will insert a section break after the section in which > the selection is currently located: [quoted text clipped - 100 lines] > > I don't know how to stop it from starting at 1. Thank you very much in > > advance Doug Robbins - Word MVP - 19 Mar 2008 10:01 GMT 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
 Signature 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
> Thank you, Doug for your reply. > [quoted text clipped - 151 lines] >> > I don't know how to stop it from starting at 1. Thank you very much in >> > advance Associates - 20 Mar 2008 02:38 GMT 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
> 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? [quoted text clipped - 157 lines] > >> > I don't know how to stop it from starting at 1. Thank you very much in > >> > advance Doug Robbins - Word MVP - 20 Mar 2008 04:01 GMT You are applying the style to the Selection which at that point is not in the new Section.
 Signature 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
> Hi Doug, > [quoted text clipped - 200 lines] >> >> > in >> >> > advance Associates - 24 Mar 2008 23:53 GMT 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
> You are applying the style to the Selection which at that point is not in > the new Section. [quoted text clipped - 203 lines] > >> >> > in > >> >> > advance Doug Robbins - Word MVP - 25 Mar 2008 10:13 GMT 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"
 Signature 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
> Hi Doug, > [quoted text clipped - 254 lines] >> >> >> > in >> >> >> > advance Associates - 26 Mar 2008 03:57 GMT 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
> > srange.InsertBreak wdSectionBreakNextPage > > [quoted text clipped - 5 lines] > > ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _ > > wdWord10ListBehavior
> Can you clarify what you mean by: > [quoted text clipped - 239 lines] > >> >> >> > ListTemplate:=ListGalleries( _ > >> >> >> > wdOutlineNumberGallery).ListTemplates(7), Doug Robbins - Word MVP - 26 Mar 2008 11:10 GMT 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?
 Signature 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
> Thank you Doug, for your reply. > [quoted text clipped - 354 lines] >> >> >> >> > ListTemplate:=ListGalleries( _ >> >> >> >> > wdOutlineNumberGallery).ListTemplates(7), Associates - 27 Mar 2008 01:50 GMT Hi Doug,
I think that your question below has just made me realise what's happening and causing this issue to me.
"Don't you have the numbering setup in the Section Heading 1 style?"
I did a few experiments here and concluded that the issue here is due to the fact that different styles have been used here.
I have a template .dot for "sections". It has got "Section Heading 1 style" with the numbering setup in it. Then, i highlighted them all and added to autotext called "section". The reason for this is as i mentioned earlier that each of those components in the report namely TOC, executive summary, section, bibliography and appendixes have their own different header and footer. For example, the footer of Executive Summary is different from that of Section. So, i thought if i could create template for each of these parts and added them to autotext, it'd save me a lot of pain from having to write VBA codes to make up headers and footers.
Anyway, in the actual myReport.dot, i have just created a new style "Section Heading 1 style" with the numbering setup in it and saved the template. It is the same style i created for template "sections". MyReport.dot has codes that would put in all these autotext to the report. The code is as follows ActiveDocument.AttachedTemplate.AutoTextEntries("executivesummary").Insert _ Where:=Selection.Range, RichText:=True ActiveDocument.AttachedTemplate.AutoTextEntries("sections").Insert _ Where:=Selection.Range, RichText:=True
After executing these lines of codes, I was hoping that there would be only one "Section Heading 1 style" but I was wrong. There are two styles now. One is called "Section Heading 1" and the other one called "Section Heading 1 Justified ...". No wonder the Section numbering doesn't work. "Section 1." from the template sections.dot has now become "Section Heading 1 Justified ..."
My question is whether there is a way of changing the style to "Section Heading 1" from "Section Heading 1 Justified..." for "Section 1." that is from template sections.dot or is there any other better ways to get around this?
Thank you in advance
> I don' understand why you would be using this code > [quoted text clipped - 241 lines] > >> >> >> > wdOutlineNumberGallery).ListTemplates(7), > >> >> >> > ContinuePreviousList:=True, _ Doug Robbins - Word MVP - 27 Mar 2008 12:46 GMT 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.
 Signature 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
> Hi Doug, > [quoted text clipped - 320 lines] >> >> >> >> > wdOutlineNumberGallery).ListTemplates(7), >> >> >> >> > ContinuePreviousList:=True, _ Associates - 31 Mar 2008 00:58 GMT Thanks Doug,
The sections now works fine. Thank you for your help.
> 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 [quoted text clipped - 244 lines] > >> >> >> in > >> >> >> the new Section.
|
|
|