Log in

View Full Version : macro to create new sections and change their layout



irisbu
05-27-2008, 04:05 AM
Hello everyone.

I need to create a macro that:
1. open a new section in the middle of the document, so the first section will be section 1, the second section section 2, and the third section section 3.

2. Changes the layout of section2 to - landscape.
3. Deletes the footers of section 2 and section 3 (and not of section1).

I have succeeded to create this manually, but when I'm trying to make a macro from this, I cant get the footers right...

Please Help me!!

Iris

gwkenny
05-27-2008, 05:02 AM
Remember, after you insert a section break, the section default is to have "same as previous" headers and footers. Before you can go deleting and/or changing headers/footers, you need to change this property. I'm sure that is where you are having problems.

If you are designing a full blown presentation system and have questions about the architecture of such, you can ask them on the board or query me direcly. I've built such animals for a number of investement banks and marketing oriented companies/institutions.

irisbu
05-27-2008, 05:55 AM
I have changed this property. It works great manually, but when I try to convert this action to a macro.... for some reason, it doesn't work...
Thank you for your reply and I hope you have a good solution to this problem

fumei
05-27-2008, 09:33 AM
Then post your code. This is a common, and fairly easily fixed, issue. It is best when using header/footer objects.

gwkenny
05-27-2008, 10:27 AM
I have changed this property... for some reason, it doesn't work... I hope you have a good solution to this problem

Umm, I have a good solution. I gave it to you. It works. Really! It does! You just didn't do what I said. I know this cause you still have the problem! lol

Let me reiterate: Remember, after you insert a section break, the section default is to have "same as previous" headers and footers. Before you can go deleting and/or changing headers/footers, you need to change this property. I'm sure that is where you are having problems.

Let me be real clear. If your document begins with one section, and you want to insert a landscape page in the middle of it, you need to insert 2 sections breaks. You document will end up having section 1 (portrait), section 2(landscape), and section 3(portrait). Section 3 will have "same as previous" headers and footers!!!

I'll bet you that you did NOT remember (even after I stated it in my previous post so I'll say it again, lol), after you insert a section break, the section default is to have "same as previous". This INCLUDES the THIRD section. So when you delete the headers and footers in section 2, you are doing that for section THREE as well and any other subsequent sections that have "same as previous" cause that's what "same as previous" means. You need to change this property. I'm sure that is where you are having problems.

To make this extremely clear, attached is a sample that does EXACTLY what you asked for assuming you follow the directions I indicated previously by changing the "linktoprevious" property.

Good luck!

fumei
05-27-2008, 10:35 AM
lol

irisbu
05-27-2008, 01:47 PM
I understood your explenation completely and did exactly what you said... and still...

I have created the macro in a template. when I open a document from the template and apply the macro, the footer is not what I wanted it to be.

I will explain the steps I need again and post the macro code. hopefully you will be able to help me.

1. open a new section (section2)
2. open a new section (section3)
3. return to section 2
4. remove the "same as previos" from the footer and then delete the footer.
5. now the footer of section2 and section3 needs to be empty, and the footer of section 1 needs to have the text I need.


and here is the code:


Sub macro5()
Selection.InsertBreak Type:=wdSectionBreakNextPage
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.TypeParagraph
Selection.TypeParagraph
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.MoveUp Unit:=wdLine, Count:=1
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = CentimetersToPoints(3.17)
.BottomMargin = CentimetersToPoints(3.17)
.LeftMargin = CentimetersToPoints(2.54)
.RightMargin = CentimetersToPoints(2.54)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.25)
.FooterDistance = CentimetersToPoints(1.25)
.PageWidth = CentimetersToPoints(29.7)
.PageHeight = CentimetersToPoints(21)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosRight
.SectionDirection = wdSectionDirectionRtl
End With
End Sub

Thanl you for your reply, and I really hope you can help me!!

Iris

gwkenny
05-27-2008, 09:43 PM
Thanl you for your reply, and I really hope you can help me!!

Hi, we'd like to help, but we are not mind readers. At no point in either of your initial posts did you say, "section 1 needs to have the text I need."

My assumption was the problem with Section 3 headers/footers going away. I do not think that anyone can read that problem as being with Section 1 not having the desired text you need.

So, we can only help you if you adequately describe your need.

Problem is that you still need clarification.

What is this footer text in Section 1 that you "need"? I'm assuming it's not to retain the existing footer cause that's what should be in there now.

Alternatively, if you opened the sheet I attached to my previous email, it should be relatively easy to understand the code and may give you the tools you need to solve the problem.

But until you adequately describe what you need, we can't help you :(

irisbu
05-28-2008, 12:11 AM
I'm sorry if my post was vage... I am pretty new at this... + I could'nt open your attachment yesterday, and now suddenly it was available to me.

The code you have posted in your attachment is "write on the money!", but, and again, sorry for not being clear... :banghead: the templates setup (which I didn't mentioned before) is - first page header/footer is different from the rest of the document... I am attaching an example of my document with more explenations, hoping that this time I will be clearer...

Thank you again for your help!!
:friends:

Iris

gwkenny
05-28-2008, 03:31 AM
1) I believe the file you submitted is corrupt. The table in the header acts strange. Basically anything in the header acts strange. Go to the first paragraph mark in the header after the table. The paragraph is defined as left aligned, but yet it's on the right side of the page. If you change the alignment to right aligned, the cursor moves to the left side of the page.

If the cursor is on the right side of the screen and you type:
"Hello? What is this"

it comes out:
"What is this ?Hello

2) I recreated your file with a scratch document.

3) You still need to be clearer on what you need. You say the second landscape section needs to have the same header. The same header as in what? The same first page header of portrait section 1? Or the primary header (that starts on the second page of the portrait section?)? I assumed the primary header.

4) You also need to be clear what header is needed for section 3 (portrait). Again, I'm assuming you do not need a different first page header in this section.

In summary, I still made a lot of assumptions because I do not have all the necessary information, however given those assumptions you now have the attached file.

I did not write an "undo" macro because I do not even know if the macro I just attached is really what you need.

fumei
05-28-2008, 02:27 PM
I am tempted to get involved here...but I think you can handle it gw. <grin>

irisbu
05-29-2008, 01:34 PM
First I want to thank to all of you for your effort to help.

The last attachment was very close to what I need and Helped me a lot!

I am attaching a new template with your code plus some little changes, and that is exactly what I needed!

Now I need to create a macro that will undo this macro, even if someone typed more text in these sections... hope you can help me...

Thank you again for your help!

Iris

irisbu
05-29-2008, 01:40 PM
I hope now it will be OK

irisbu
05-29-2008, 01:44 PM
I tried to attach a template and failed, so I am attaching the file as *.doc

gwkenny
05-31-2008, 03:27 AM
Just a quick note. Investigating the file you put up, Word just doesn't act right.

Again, all the paragraph marks are left justified, but the paragraph marks are all on the right side of the page and are exhibiting the same problems as I stated previously.

I don't know if your Word is corrupted, maybe I'm missing a patch, or if this is how an israelie version of MS Word (I'm assuming that is what you are using) interacts with the standard US version.

Want to do a little more investigation, but having to recreate the documents you put up from scratch everytime does not induce me to help very much :(

irisbu
05-31-2008, 02:34 PM
I have sent you my template so you will see the header/footer. The paragraphd semm wrong to you because in hebrew we wright from right to left, exactly the opposit from English... so... please ignore the paragraphs and don't create a document from scratch everytime... I can handle all the rest, I need help with macro.
THANK YOU AGAIN, you helped me a lot and I hope you can help me reach the solution for my problem.

Iris

gwkenny
06-02-2008, 11:31 AM
Is there a reason why you always go to here:

Selection.GoTo What:=wdGoToHeading, Which:=wdGoToFirst, Count:=6, Name:=""

The code in your book errors on my machine at:

Selection.Find.Style = ActiveDocument.Styles("????? 1")

The style is not defined in the document (or the style name is not translating from your Hebrew version to my US English version :( ).

qazpl
06-02-2008, 01:47 PM
The document is probably not corrupt just from hebrew right to left computer. just select all and change rtlpara and rtlrun to ltrpara and ltrrun with macro and you should be OK.

irisbu
06-02-2008, 02:33 PM
I am going to this paragraph because, the section that turns into landscape always apply to heading 6 of the document, and yes, because its in hebrew it seems like the document is corrapted, but it's not... even the "gibrish" you see is hebrew letters that canot be translated to english, so just ignore that...

thanks, and sorry for the headake with the hebrew... :(

gwkenny
06-02-2008, 04:41 PM
qazpl:

Love learning new stuff! The only thing that needed to be set was .ReadingOrder to wdReadingOrderLtr

Iris:

Thanks, I got the "gibrish" not translating as hebrew characters :)

The thing I don't got is that style defined in my normal.dot and more importantly, my machine does not recognize it in my document so when the code executes Selection.Find.Style = ActiveDocument.Styles("????? 1"); the code is erroring out telling me the style is not a part of the collection (it's not defined). Macro aborts.

So my question is, where should this style be cause it determines where the section breaks are added?

"thanks, and sorry for the headake with the hebrew... "

Don't be sorry :) I learned something new and that's always nice!

qazpl
06-02-2008, 05:57 PM
Thanks. but I was really refering to Selection.Ltrrun method. anyways whatever works

gwkenny
06-02-2008, 08:22 PM
qazpl:

I was trying to be polite in pointing out that the methods you indicated did not solve the problem but it did point me in the right direction!

Which I definitely appreciated :)

irisbu
06-03-2008, 04:27 AM
I'm glad to be of service.... I didn't thought that I would try to learn something from you and end up teaching you something...:yes always a plesure!!

The ActiveDocument.Styles("????? 1") is actually Heading6... The template is built with Headings1 - Heading7 and is already in the document when the user opens a new document. The reason for that is that these headings are mandatory, they have to be in the document even if their is nothing written in them.

Heading7 always! begins in a new page, even if heading 6 has only 1 line... If we activate the sections macro, this page must be deleted, because it starts in a new section anyway....

to be more clear I am attaching 2 examples: example4.doc is how the document looks when the user starts a new document.

example5 is how the document should look after activating the section macro.

hope I covered all the things...:doh:

thanks

iris:hi:

irisbu
06-03-2008, 04:28 AM
example5

gwkenny
06-04-2008, 05:41 AM
I've attached a macro to revert back to portrait.

Note that the macro has to be started anywhere in the landscape section. If you don't want this to occur, but always to start with the section that contains Header 6, just add in your code from the first macro to find it in the beginning of the REVERT subroutine.

There's some error checking like determining the section is landscape orientation and that there are atleast 3 sections in the document and the macro is not started in section 1.

Cheers,

irisbu
06-05-2008, 04:20 AM
It Works GREAT!! :bow:

Thank you for your help!! Hope I can return a favour ....

And in another matter, have you ever worked with word documents that draws information from Access into the customdocument properties?

And if so, did you try to apply this to network users (like sharepoint)?

I have created a document that retrives information from Access db and inserts it to the document properties and everything works great... until I tried to put it on sharepoint... then I had the most bazzar problem... after working on the document for some time (everytime a different period of time), I get a message that it is not possible to save the document to a library in c drive (even though the document is on the F drive on the shrepoint network), and word reffers to a non existing temp file on c drive. From here on I cannot save the document anywhere else and I have to close word and reset the computer if I want to work with this file again....

sounds complicated... well... it is! It is driving me crasy!!

I am working with Office 2007.

If you know of such a problem or it's solution... I will be greatful for your help!

Thank you for everything!!

Iris

gwkenny
06-06-2008, 05:16 AM
Sorry, I don't know why that would be happening.

That would take some troubleshooting which would necessitate much more information than you have given and time I do not unfortunately have and there are people who have much more experience with sharepoint than I.

Good luck to you!