PDA

View Full Version : Solved: Make Each Section at Least 3 Pages?



clhare
05-28-2005, 10:34 AM
Is it possible using a Word macro to make sure each section in a document has at least 3 pages?

Cheryl
:think:

MOS MASTER
05-28-2005, 11:45 AM
Is it possible using a Word macro to make sure each section in a document has at least 3 pages?

Cheryl
:think:
Hi Cheryl, :yes

Never did this before but I like to think, about everything can be done!

That said I'm sure there's many Cave-at's on the way. The major one being the fact that if you redimension section's to 3 page you'll be altering the Section Collection. Which I think we need to loop to query the page count.

We could loop from back to front and maybe that will keep us out of trouble. (maybe!)

Further more
Let's say your last section is 2 pages and the previous one has 5. Logics would say you would delete the last section brake and that would leave you with the last section being 7 pages.

Well perhaps you might say put a break in the middle of those to even things out.

Then again I would question the need for this macro because those section ends are probably there for a reason.

The problems could also be Formatting loss/Header footer problems....and so on depending on how the documents are formatted.

So could you be a little more specific on why you need this and how I should determine what to do if a section has less than 3 pages....

BTW..I'm sure I've forgotten a lot more problems to be encountered. (Word loves his sections and bind lot's of formatting stuff in his section ends so.....)

Enjoy! :whistle:

clhare
05-28-2005, 01:38 PM
Here's the thing....I have 199 documents with a text box object in each footer. The macro updates the info in the text box object by the text box's name. The problem is that somewhere along the line, some of these text box objects got renamed and the macro isn't updating them.

The only way I can think of to check each text box's name is to make sure each section has 3 pages and then moving from footer to footer in design mode and checking the name in Properties. Then I need to compare the text box names I found with the names that are listed in the macro to make sure they match.

I was hoping to make things a little easier on myself if it was possible to create a macro to shove some extra page breaks in if a section was less than 3. It doesn't have to be pretty cuz I'll just undo after I check the text box names.

It's gonna be a very long weekend!!

Cheryl

MOS MASTER
05-28-2005, 01:44 PM
Hi, :yes

Hmmz never did like Textboxes in Headers. But with good code they are easily updated.

But like you said your code depends on the name of the textbox. What are they? Shapes textbox/formfield textbox/Active x etc....

I'm almost done for tonight because we're closing down.

Could you send me one of those documents so I can test on it? Send to: joost@webforums.nl

I'll do the code tomorrow! :whistle:

clhare
05-28-2005, 04:38 PM
I've sent you a file.

Cheryl

MOS MASTER
05-29-2005, 05:03 AM
I've sent you a file.

Cheryl
Hi Cheryl, :yes

Sorry didn't receive one...:dunno

clhare
05-29-2005, 04:45 PM
Hi Joost,

I don't know what happened... I resent the file. Let me know if you still don't receive it.
You won't be able to run the macro in it as it uses special fonts and a dll to actually update the barcode, but that shouldn't matter. What I really need to a macro that will search all the footers, find the text boxes, and find out what all their names are, then list them for me.

Your help is greatly appreciated!

Cheryl

fumei
05-30-2005, 08:17 AM
1. It is an EXCELLENT idea to always have a minimum of three pages in a Section. In fact even when a new Section has ONE page, I always make three pages anyway. Then I set the header/footer for each page, then delete the pages. This way the header footers are set. If new pages are made, the header footer are AUTOMATICALLY created and are correct. This is because header/footers are stored in the Section (the parent object). Therefore creating a Section (for me) is always done by code.

2. I agree with Joost in my dislike for textboxes - although you would have to be more precise by what you mean by textboxes.

That being said. Here is code to insert two pages (assuming that you have already done the Section break. It then deletes them.
Dim var
Selection.InsertBreak Type:=wdPageBreak
Selection.InsertBreak Type:=wdPageBreak

For var = 1 To 2
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^m"
.Replacement.Text = ""
.Forward = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Next

OK, so here is the code to put a Section breaks and SET the footers (as text in this example) WITHOUT puttong the empty pages. You can SET the footers for FUTURE pages, without actually creating those pages. As stated, you can set them directly, and when those pages are created, the footers will be whatever you set them to be, automatically.

It is this aspect of the Object Model - a subject rather sneered at by some - that really helps to be able to use header footers effectively.
Sub MakeNewSection()
With Selection
.InsertBreak Type:=wdSectionBreakNextPage
With .PageSetup
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = True
.DifferentFirstPageHeaderFooter = True
End With
.Sections(1).Footers.Item(wdHeaderFooterPrimary) _
.Range.Text = "This is Primary, and is the default " & _
" footer item. It will be the text for ALL the " & _
" footers if the above items are set to FALSE."
.Sections(1).Footers.Item(wdHeaderFooterFirstPage) _
.Range.Text = "This is FirstPage, and will be " & _
"visible if FirstPage is set to FALSE."
.Sections(1).Footers.Item(wdHeaderFooterPrimary) _
.Range.Text = "This is Odd/Even." & _
" It will be the EVEN page if Odd/Even is set " & _
"to TRUE. What is set for PRIMARY will become " & _
"the ODD pages footer."
End With
End Sub

While it is true I rant about the Object Model, but .....shrug...it is the best way to undertsand what is really going on.

MOS MASTER
05-30-2005, 09:40 AM
Hi Joost,

I don't know what happened... I resent the file. Let me know if you still don't receive it.
You won't be able to run the macro in it as it uses special fonts and a dll to actually update the barcode, but that shouldn't matter. What I really need to a macro that will search all the footers, find the text boxes, and find out what all their names are, then list them for me.

Your help is greatly appreciated!

Cheryl
Hi Cheryl, :hi:

Now I see you're using ActiveX Textboxes...Don't like those aswell! :rofl:

Ok your Main problem is you have textboxes with the wrong name in them.
That said you're asking for a way to give you all the names of the textboxes used in your document.

I can do better. The below code gives you the section nr the textbox is in. Then tells you in Which Footer (You're using them in the footer for the rest who's reading) type (There can be three different types used or all three together) and the gives you the name of the ActiveX textbox.

So now you have the tool to find your rogue Textboxes! :devil:
Sub ActiveXNamePerSection()
Dim oSection As Word.Section
Dim oFooter As Word.HeaderFooter
Dim oShape As Word.Shape
Dim oActiveX As TextBox
Dim sMsg As String
For Each oSection In ActiveDocument.Sections()

For Each oFooter In oSection.Footers

For Each oShape In oFooter.Shapes

If oShape.Type = msoOLEControlObject Then

Set oActiveX = oShape.OLEFormat.Object

sMsg = sMsg & "Section Nr: " & oSection.Index & Space(5) & _
"Footer type: " & oFooter.Index & Space(5) & _
"Textbox name: " & oActiveX.Name & vbCr

Set oActiveX = Nothing
End If

Next

Next

Next

With Application
.Documents.Add
.ActiveDocument.Range.InsertAfter Text:=sMsg
End With

End Sub


The code produces a new document with all the data in it.

Enjoy!

Gerry,

Love the code for the 3 section bit! :yes

clhare
05-30-2005, 06:54 PM
Joost,

I get an error when I run the macro that says "Compile error in hidden module". The error is on the following line of code:

Dim oActiveX As TextBox


What do I need to do so it will work for me?

_____________________

Gerry,

How would I adjust your macro to set up the 3 pages and put a specific AutoText entry into each one? I use an Odd Page Section Break with Different Odd/Even set and Different First Page set.

Cheryl

clhare
05-30-2005, 06:55 PM
Gerry,

I should have said.....put a different AutoText entry into the FOOTER of each of the 3 pages.

Cheryl

MOS MASTER
05-31-2005, 12:22 PM
Hi Cheryl, :yes

Could you tell which Word version you have?

I've tested this code with succes on 2002 and 2003...So I wouldn't know what other problem then version there could be.....

If you tell me I'll test on the version you have and have a workarround for you.

:whistle:

clhare
05-31-2005, 12:45 PM
I have Microsoft Word 2002.

MOS MASTER
05-31-2005, 12:50 PM
I have Microsoft Word 2002.

That's very strange...I've coded it on a 2002 machine if I recall well. And run it aswell on 2003...(Have used that sort of code for many times..so I don't understand)

Did you try to paste the code in a new blank document and complile and run it? Doesn't that work? (Could be something else of course)

On the other hand it could be something in the set-up of Office itself..

When I get home tonight I'll test again on XP machine.:whistle:

TonyJollans
05-31-2005, 12:54 PM
Hi Cheryl,

Just a small point - but if you get "Compile error in hidden module", how do you know the line of code it's on?

MOS MASTER
05-31-2005, 12:59 PM
Hi Tony,

Good point I missed the error!

This makes me believe as well that my code is not the problem but probably one of the references can not be made? (Cheryl one of your custom DLL's perhaps)

Please do try the code in a empty document. :whistle:

clhare
05-31-2005, 01:10 PM
When I had the macro in one of my Startup templates, it wouldn't run. When I put it in Normal.dot, then it did run!!! YEA! I don't know why it won't work from my other template.

clhare
05-31-2005, 01:16 PM
Joost,

This is the most incredible macro!!! It is gonna save me so much time, it's unbelievable!

You are the best!!!

Cheryl
:bow:

fumei
06-01-2005, 09:55 AM
OK....what am I doing wrong, and what is Cheryl doing right?

I have created a document with ActiveX textboxes in the footers.

Separate controls for each Footer in each Section. Two Section, for a total of SIX controls.

I have made separate names for each control, and separate contents for each control.

The code above produces a blank new document. The reason? The Shapes count = 0. There are no Shapes.

MOS MASTER
06-01-2005, 09:59 AM
When I had the macro in one of my Startup templates, it wouldn't run. When I put it in Normal.dot, then it did run!!! YEA! I don't know why it won't work from my other template.
Hi Cheryl, :yes

It's probably one of the references missing: DLL/Template/Addin

More insight at: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q328239 for one of the reasons

Later..:whistle:

MOS MASTER
06-01-2005, 10:01 AM
Joost,

This is the most incredible macro!!! It is gonna save me so much time, it's unbelievable!

You are the best!!!

Cheryl
:bow:
Hi Cheryl, :yes

Glad it wasn't the macro and even more glad your so happy with it!

You're welcome! :thumb

MOS MASTER
06-01-2005, 10:08 AM
OK....what am I doing wrong, and what is Cheryl doing right?

I have created a document with ActiveX textboxes in the footers.

Separate controls for each Footer in each Section. Two Section, for a total of SIX controls.

I have made separate names for each control, and separate contents for each control.


Hi Gerry, :yes

Hahaha you're almost there!

You are probably using the default Inline ActiveX controls that are part of the InlineShapes collection.

Cheryl uses the Flowted ActiveX shapes and they are part of the shapes collection and you have address them in the specific way I did.

Do you want me to send you a testdocument? :whistle:



The code above produces a blank new document. The reason? The Shapes count = 0. There are no Shapes.

Well yeah sure if you are looking for them let's say: Activedocument.Shapes.Count
then you won't have any because they are in the Footer story (And not main)

You could get the count like:
activedocument.Sections(1).Footers(1).Shapes.Count for the first footer...

Later...

fumei
06-01-2005, 11:37 AM
Yeah......

Oh, and Cheryl?? You were still asking about some code that makes three pages for a new Section?? Are you still asking? here it is.

Sub NewSection3Pages()
' for Cheryl
With Selection
.InsertBreak Type:=wdSectionBreakNextPage
.MoveRight Unit:=wdLine, Count:=1
.InsertBreak Type:=wdPageBreak
.InsertBreak Type:=wdPageBreak
With .PageSetup
.DifferentFirstPageHeaderFooter = True
.OddAndEvenPagesHeaderFooter = True
.SectionStart = wdSectionNewPage
End With
With .Sections(1)
.Footers(wdHeaderFooterPrimary) _
.LinkToPrevious = False
.Range.Text _
= "This is footer text for ODD pages"
.Footers(wdHeaderFooterFirstPage) _
.LinkToPrevious = False
.Range.Text _
= "This is footer text for First Page."
.Footers(wdHeaderFooterEvenPages) _
.LinkToPrevious = False
.Range.Text _
= "This is footer text for EVEN pages."
End With
End With
End Sub

MOS MASTER
06-01-2005, 11:44 AM
Yeah......
Sub NewSection3Pages()
' for Cheryl

Only for her!!? :(

Now tell me which part I told was the case for your test document? (Inlineshapes?) :yes

fumei
06-01-2005, 11:51 AM
Huh? No, of course it is not only for Cheryl. I just use that when I am writing a routine from scratch - so if I get interrupted before I finish, I can have an idea of why I started it in the first place.

As for:


Only for her!!? :(

Now tell me which part I told was the case for your test document? (Inlineshapes?) :yes

huh? I don't get it. But then...I am having a very stupid day.

MOS MASTER
06-01-2005, 11:55 AM
Huh? No, of course it is not only for Cheryl. I just use that when I am writing a routine from scratch - so if I get interrupted before I finish, I can have an idea of why I started it in the first place.
As allways I was trying to be funny! :devil: Guess I wasn't ...:rofl:

Wel you've asked why my activex control loop didn't work for you and you've explained how your document layout was...then you asked WHY it isn't working for you...

I answered rather lenghty with different reasons why it wouldn't work...so I was wondering which one of my answers made it work for you..if any? (just wondering)

A stupid day? Wel I'm having one aswell! :doh:

fumei
06-01-2005, 11:59 AM
Oh and....

if you are using:
For Each oShape In oFooter.Shapes - which you are, then would not
oFooter.Shapes.Count return a value? After all, you have declared oFooter as a headerfooter object. If you can get at it to do something, then methinks it is counted.

What the heck are "floated" ActiveX controls? If you are refering to those $%@#*()&^!%$#*($%^&&%#@^%$!* things that you go Insert > Textbox ........

I am outta here. I absolutely hate those things. They are a pain to do anything with by code.

fumei
06-01-2005, 12:00 PM
Hmmmm, our timing is off as well.

MOS MASTER
06-01-2005, 12:08 PM
Hmmmm, our timing is off as well.
You could say that again! :rofl:

I Hate them as well as Normal textboxes to by the way as stated above...

But Floated ActiveX is just:
oShape.Type = msoOLEControlObject

Inline ActiveX is:
oshape.Type = wdInlineShapeOLEControlObject


O well...lets leave it at that before you start putting more of this:

$%@#*()&^!%$#*($%^&&%#@^%$!* (I'm thinking of your wellbeing in the matter) :p

MOS MASTER
06-01-2005, 12:10 PM
Oh and....

if you are using:
For Each oShape In oFooter.Shapes - which you are, then would not
oFooter.Shapes.Count return a value? After all, you have declared oFooter as a headerfooter object. If you can get at it to do something, then methinks it is counted.

Well it does return the proper count over here. So yes of course it does.

Again would you like to receive a document so you can see it? :whistle:

fumei
06-01-2005, 03:39 PM
Sigh....yes, you may as well send the document. You have my email. I will look at it tomorrow. I'm going home. I need a drink. Actually, I am going to go take my kayak out on the lake and just float around for an hour.......WITH a drink!

clhare
06-01-2005, 06:23 PM
Hi Joost,

I'm not sure I understand. If I move the template your totally awesome template is in out of Startup, how would I be able to access the macro from any open file?

Cheryl
:doh:

clhare
06-01-2005, 07:17 PM
Gerry,

That's just what I needed. Thank you very much!

Cheryl

clhare
06-02-2005, 05:19 AM
Joost,

I went to the link, but it says to move the macro out of Startup. If I do that, how can I run the macro in any open file to check the text boxes?

I desparately need this macro, but I'm having problems getting Word to allow it to work.

Cheryl

fumei
06-02-2005, 07:25 AM
I am totally lost on what is happening.

Cheryl, what is this instruction to move out of Startup? Why? Huh? I am missing something significant. In any case, you CAN manually load a .DOT file to access its code. Or you CAN copy modules. I need to go back through this thread and figure what is going on. This is casuing me to have another stupid day.

MOS MASTER
06-02-2005, 09:35 AM
Sigh....yes, you may as well send the document. You have my email. I will look at it tomorrow. I'm going home. I need a drink. Actually, I am going to go take my kayak out on the lake and just float around for an hour.......WITH a drink!
Aaaoooo...poor Gerry...seams you needed a break! :yes

Well I wish I could take my kayak and float arround in it...I live in the big city so....:(

You've got mail!

MOS MASTER
06-02-2005, 09:36 AM
Hi Joost,

I'm not sure I understand. If I move the template your totally awesome template is in out of Startup, how would I be able to access the macro from any open file?

Cheryl
:doh:
Hi Cheryl, :yes

If you don't want it in your startup folder (or Office startup) then just copy it in Normal.dot so its always available to you! :whistle:

MOS MASTER
06-02-2005, 09:41 AM
Joost,

I went to the link, but it says to move the macro out of Startup. If I do that, how can I run the macro in any open file to check the text boxes?

I desparately need this macro, but I'm having problems getting Word to allow it to work.

Cheryl
Hi Cheryl, :yes

The link explains that you can have this error in hidden module if one of your addins is bugging you!

It's allready establisht that my code isn't the problem here. It's just you're a developper so it's very likely there be multiple templates loaded when you work in Word. (Press ALT+F11 and you see the loaded projects)

Further more you make custom DLL's those can also be referenced in the VBE Tools/Reference

All of those Addins/DLL (Code in Normal) are a potential source for the error you're receiving.

Have you allready tried the error menu/Compile project to see if the VBE helps you find the problem?

Basicly you should paste my code in Normal.dot to have it available all the time.

And there's nothing more to do for you then to get rid of all your references one by one until you find the source of this error. (DLL/Addin/Code)

Good luck! :whistle:

clhare
06-03-2005, 05:24 AM
Joost,

You're code is wonderful! I did get it to work when I put in in Normal.dot in Startup. But then when I tried it the next day, I got the compile error again. Now I'm not sure what I did, but it is once again letting me run the macro.

How does the compile project work? I've never done that before.

Cheryl

fumei
06-03-2005, 06:35 AM
Hooo boy...how many Startup templates do you have?????

Joost, I have mail?....nope, I don't....at least not from you. 4 000 000 from the rest of the planet, but not from you. It is going to have to wait until next week. I am going to talk to some bears up in the mountains...well, actually hopefully not the bear part. Or maybe a conversation from shouting distance.

I live in a reasonably big city ( 2 million) as well, but fortunately in an hour I can be in wilderness with nothing ahead of me (no roads, no towns, no trails - quite literally nothing but trees and mountains) for 400 kilometres. I am outta here...later, people.

MOS MASTER
06-03-2005, 09:13 AM
Joost,

You're code is wonderful! I did get it to work when I put in in Normal.dot in Startup. But then when I tried it the next day, I got the compile error again. Now I'm not sure what I did, but it is once again letting me run the macro.

How does the compile project work? I've never done that before.

Cheryl
Hi Cheryl, :yes

You say Normal.dot is in your Startup???? That's not the place for Normal? (Ussualy)

It should be in the default template directory of Word or the Network templates folder.

Your problem is simple. You have conflicting code in one of your addins/dll's or Normal.dot. (or anything referenced that's missing or not working probably)

These kind of things happen a lot if you build all sort's of things and you don't clean everything up. Throughout this topic I pointed you to various locations to look and clean everything out of there. (You could even start with a new Normal and remove everything that's loaded globally)

You have to do this to find the source of your problem.

This is all I can offer you because your problem is somewhere in your enviroment in one of the projects you're using.

Johnske has written a good article about the priciples of compiling your code:
http://www.vbaexpress.com/forum/articles.php?action=viewarticle&artid=38

Good luck! :whistle:

MOS MASTER
06-03-2005, 09:20 AM
Hooo boy...how many Startup templates do you have?????

Joost, I have mail?....nope, I don't....at least not from you. 4 000 000 from the rest of the planet, but not from you. It is going to have to wait until next week. I am going to talk to some bears up in the mountains...well, actually hopefully not the bear part. Or maybe a conversation from shouting distance.

Hi Gerry, :yes

I guess she has many (left over) templates in directories that are read by Word...

Sure you had mail I got the confirmation of you reading it at:

was read on Thu, 2 Jun 2005 10:18:02 -0700
But I'm sure you receive as much E-mail as I do and you could have deleted it. (and if automatic send received confirmation is on then its logic I receive the notification mail)

Please check your deleted Items?


I live in a reasonably big city ( 2 million) as well, but fortunately in an hour I can be in wilderness with nothing ahead of me (no roads, no towns, no trails - quite literally nothing but trees and mountains) for 400 kilometres. I am outta here...later, people.
Well your city is bigger then the one I live in...but again so is your country..ours is densly populated!

I wish I could be in that kind of wilderness nearby home....(o well got to move I guess) :rofl:

Later....Give my regards to Yoghi! :whistle:

clhare
06-03-2005, 01:50 PM
I do have several templates in my Startup folder (but only about 6) because of the various different groups I create templates for. I think I may have had this macro in more than one of them (why I don't know). I made sure there was only the one and today it's working again.

I'm sooooo happy!! It came in handy today for another project I'm working on that uses those dang text boxes in the footers.

Cheryl
:beerchug:
(Not a beer drinker, but if this macro doesn't call for a toast, I don't know what does!!!)

MOS MASTER
06-03-2005, 01:55 PM
Hi Cheryl, :yes

The problem with 6 templates with code in the startupfolder is the fact that they are all loaded into Word and are globally accessible!

So this means Word looks in all of these templates (Macro container) to find the macro to execute and yes dupe macro's or other things can cause the problem...


O well...the Macro works! You are happy...I am happy....O forgot...I am a Beer drinker..So let me have a couple more for you! :beerchug: