PDA

View Full Version : Solved: Section break counter



Sirfanta
07-09-2007, 05:28 AM
Hi! :hi:
Hello
I have 2 problems.

Nr1
I want to make page counter that counts pages in a section.
I have a tool that copys pages.
So when it copies a page, I need it to give it a new counter (1 of 3, 2 of 3, 3 of 3)
Is there a field code in word that I can use? (Like the page field code).

Nr2
Is there a easy way to get a page from a other document using a bookmark? :dunno

fumei
07-09-2007, 08:33 AM
#1 There are number of ways of going about this. You do not give details of the "tool" you are using, so it is hard to say.

You can get the number of pages in a Section by making a Range object of the Section, and then use CalculateStatistics. Like this:Sub SectionNumPages()
Dim r As Word.Range
Dim var
For var = 1 To ActiveDocument.Sections.Count
Set r = ActiveDocument.Sections(var).Range
MsgBox r.ComputeStatistics(wdStatisticPages)
Next
End SubThis displays the number of pages in each Section.

Again, as we do not know the details of the "tool", it is impossible to suggest possible ways to enhance its functionality.

#2 Yes, very easily. Bookmark the page, and then use the bookmark Range. You do not need to Select it, just use the range. Say you have two documents (Doc1 and Doc2), and for the sake of ease they are both open in Word. They do not have to be of course, but for simplicity sake. Also, lets assume the page you want IS bookmarked in Doc2, named "GetThisPage".Sub GetOtherPage()
Dim ThisDoc As Document
Dim ThatDoc As Document

Set ThisDoc = Documents("Doc1")
Set ThatDoc = Documents("Doc2")
' NOTE neither has to be the Active document

ThisDoc.Range.InsertAfter _
Text:=ThatDoc.Bookmarks("GetThisPage").Range.Text
Set ThisDoc = Nothing
Set ThatDoc = Nothing
End SubThe code inserts the text of the bookmark in Doc2 at the end of Doc1. It could, in fact, be placed anywhere you want. Again, neither document has to be active.

Sirfanta
07-09-2007, 11:24 PM
The tool is just a macro that I have put on the toolbar.



Dim r As Range
Set r = ActiveDocument.Bookmarks("\page").Range
With r
.Copy
.Collapse Direction:=wdCollapseEnd
'.InsertBreak Type:=wdPageBreak
.Paste
End With
Set r = Nothing



But the answer you gave me way great, I can work with this :).. thank you!:beerchug:

fumei
07-10-2007, 03:47 PM
Ah, when you said "tool" I was thinking of something more generically useful. The predefined bookmark "\page" only applies to the page the Selection is on.

I avoid using Selection.

Sirfanta
07-11-2007, 10:10 AM
The reason that I use Sections is because I use different type headers.
So when copying a page you make a new page in that section. So after copying the same page X times the user runs a macro that puts a page counter/number on the document in that section.
I so if there had been a field code that I coud use, like the page field code.
(Example: section 5 has 4pages. Then it would fill out a page number I selected area)


Set ThisDoc = Documents(Dok1)
Set ThatDoc = Documents(Dok2)


I want to give the user a way to get pages from the template. It works if both documents are running. Is there a way to do this without having document open?

fumei
07-11-2007, 11:40 AM
The reason that I use Sections is because I use different type headers.Indeed. That is the main reason for Sections.
So when copying a page you make a new page in that section. So after copying the same page X times the user runs a macro that puts a page counter/number on the document in that section. I am not following. You make multiple copies of the same page into the same (other) document?

There is a Field - SectionPages - that returns the total number of pages in a Section. However, it only works on the Section the Selection is in.

I am do not fully understand what it is you are trying to do. It looks like you want a Page 1 of 3, Page 2 of 3, where "3" is the number of pages in THAT Section.

This can be done easily IF - and only if - you re-start the numbering of the Section. That is, each Section starts at 1 again.

Is this what you are doing??? It is not clear. If it is what you are doing you can put this in the header using just Fields. Page and SectionPages.

Otherwise, you can not do this because you have only THREE possible variations of headers in any Section. So if you have FOUR pages, then two of them will be identical. IF you use Fields, and IF you do not re-start page numbering.

Given that, it is possible to do tricky things using section objects, and extracting information out of them. However, you are still stuck with the maximum of three different headers per Section.

You can do all sorts of things with Section objects though. For example, here is code that will display the number of pages in each Section of a document.Sub SectionCrap()
Dim var, var2
Dim SecObjects() As Section
Dim msg As String

For var = 0 To ActiveDocument.Sections.Count - 1
ReDim Preserve SecObjects(var)
Set SecObjects(var) = ActiveDocument.Sections(var + 1)
Next

For var2 = 0 To UBound(SecObjects)
Select Case var2
Case 0 ' for Section 1 ONLY
msg = msg & "Section" & var2 + 1 & _
" has " & SecObjects(var2).Range _
.ComputeStatistics(wdStatisticPages) - 1 & _
" pages." & vbCrLf
Case Else
msg = msg & "Section" & var2 + 1 & _
" has " & SecObjects(var2).Range _
.ComputeStatistics(wdStatisticPages) & _
" pages." & vbCrLf
End Select
Next
MsgBox msg
End SubNote the Select Case on the var2 variable. The returned value of ComputeStatistics(wdStatisticPages) for Section 1 of ANY document will always be +1 above the true value.

There is a reason for this within the Object Model...but I have done enough long winded stuff on that.

First, lets be clear, this is a value attained for a Section. Any Section. SECTIONPAGES returns the value of the Section the Field is being placed into. So....to get the true value for Section 1 (ONLY!), you either have to subtract 1, OR make a Range object and use .MoveEnd -1.

Sorry, I am blathering again.

So, ummm, what are you trying to do exactly???? This also applies to:
I want to give the user a way to get pages from the template. It works if both documents are running. Is there a way to do this without having document open? "Template". Why am I thinking you are not making reference to what I would call a template? However, if I understand you correctly...no. But then, as I am not clear on what you ARE saying, you may want to look at the INCLUDETEXT field.

I would like to repeat something though. Your code seems to be using the page the Selection (the cursor) is on. Are you sure this is what you want to be doing?

Sirfanta
07-12-2007, 01:04 AM
Thank you for replying
I haven?t used my English skill sins school, so I try again.
The user opens a new doc, with template ?ThatDoc.dot?.
The template has 80pages. The macro deletes pages that the user isn?t going to use in this project. Some of the pages are going to be used ?x? times, the pages are forms, every form has 5 points that are unique (form number, page number, form name. etc).
The macro can be run several times, so the ?ThatDoc.dot? will always be in the same directory. ?ThisDoc.doc? will be changed and adjusted by the user.
So when the user runs the macro again, the macro will get pages from the original template.
I use bookmarks to get right pages from the template.
So the code you gave me works it I have 2 docs that are active. I need the code to work whit only 1 active doc.

Back to ?Section?.
My goal is to get same forms in ?ThisDoc.doc? to have page numbers.
All the forms are in different sections, so when I get a new page from the template the page is placed in the sane section. In the header I only have the name of the forms, icon, etc. so the header is not dynamic.


I am do not fully understand what it is you are trying to do. It looks like you want a Page 1 of 3, Page 2 of 3, where "3" is the number of pages in THAT Section.
This is correct.


This can be done easily IF - and only if - you re-start the numbering of the Section. That is, each Section starts at 1 again.
Is this what you are doing???


I?m not doing that, but maybe that is the solution to my problem?
So then I can use the Field in the form to fill out the page number if I don?t get the code to work. Thanks for the code I think I will get a working code by modifying it! =)

fumei
07-13-2007, 07:29 AM
OK. Yes, you will need to do a Section start at Page 1, for each Section.

If I understand correctly, you are using the template (and its macro) to create ThisDoc.doc using an additive processs. You are taking preexisting pages abd adding them to the created document.

Is it possible to post the template file?

Usually, the best way to make unique documents from a template is by a reductive process. You remove the parts not applicable. This is also what you are doing.

However, again if I understand correctly, you have the issue of multiple SAME pages. The problem is the word "page". Page is a funny thing in Word.

So, on one hand hand you are removing the chunks the user does not need, but you (I think) are also ADDING copies of chunks.

All do-able, if somewhat unusual. The page number issue MUST be resolved within your use of Sections. Page numbers are child objects of the header/footer - which are child objects of the Section.

Sirfanta
07-13-2007, 09:19 AM
Thank you for replying fumei

I will post the template on Monday, I don?t have it on this computer(I just have to strip it for company ?secrets?).

Here are the steps:

- The user runs the template
- fills out the UserForm.
- macro runs
- Deletes pages that are not wanted.
- Macro makes a toolbar.
- Toolbar:
-Delete Page (Thisdoc)
-Copy Page (Thisdoc)
- Macro to add input/pages
- A userform that gets a page from the template if deletet the first time. The macro writs(serial nr, comments, name) inn to the page in the template that is going to be copied, then it copies it to thisDoc. it adds serial nr, comments, and name to several forms in ThisDoc. May not be the smartest way to do this.

But in word you have to use bookmarks or pages to navigate. In excel its easy, u have cells and rows.(I know you are going to bust me on this sentence)