section break/insert file

B

bryan

I have a 1 page template that has a header and footer.
I have a check box to add file, another to delete the file.
Adding works fine but the delete does not.
It deletes the added file but, is also removing the header and footer from
1st page.

I have done this with a 2 page template and it works fine.

Her is my remove code:
Sub Chk1()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
Dim rnge As Range
Set rnge = ActiveDocument.Sections.Last.Range
rnge.Start = rnge.Start - 1
rnge.Delete
End If
End Sub
 
J

Jean-Guy Marcil

bryan said:
I have a 1 page template that has a header and footer.
I have a check box to add file, another to delete the file.
Adding works fine but the delete does not.
It deletes the added file but, is also removing the header and footer from
1st page.

I have done this with a 2 page template and it works fine.

Her is my remove code:
Sub Chk1()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
Dim rnge As Range
Set rnge = ActiveDocument.Sections.Last.Range
rnge.Start = rnge.Start - 1
rnge.Delete
End If
End Sub

What is the difference between your 1-page and 2-page templates? (Obviously,
besides the number of pages!)

Meanwhile, you may want to investigate the possibility of making sure that
the header/footer in the section to delete have been unlinked from the
previous section prior to deleting it... Depending on how your templates are
designed, it may have an impact.
 
B

bryan

I found the problem. It was in the document itself. I turned on all
formatting marks and seen that it had grayed out footer tabs on the bottom. I
deleted those and all is well. Not sure why it would work fine on the 2 pager
and not the one pager but, that took care of it.

What do you mean by ' making sure that
the header/footer in the section to delete have been unlinked from the
previous section prior to deleting it' ?

Thanks,
Bryan
 
J

Jean-Guy Marcil

bryan said:
I found the problem. It was in the document itself. I turned on all
formatting marks and seen that it had grayed out footer tabs on the bottom. I
deleted those and all is well. Not sure why it would work fine on the 2 pager
and not the one pager but, that took care of it.

Don't know what you mean by "grayed out footer tabs" but I am glad you
found a solution.
What do you mean by ' making sure that
the header/footer in the section to delete have been unlinked from the
previous section prior to deleting it' ?

Try this:
A)
Create a one page document.
Add a header.
Go to the end of the doucment and insert a "Next Page" section break.
Add some text in that second section.
Select the second section, including the section break itself at the bottom
of the first page (Use the ¶ button to see it).
Hit "Delete".
The header is gone for the whole document.

B)
Now repeat, but before deleting the second section, double-click on the
header at the top of the second page to access the second section header. On
the Header/Footer toolbar, unlink this header from the one on the previous
page.
Now delete the second section with its section break. See the difference?

Here is what I bvelieve happens:
In A), you delete both the section break and the last ¶. Both of these are
the actual container for the section header information (and footer...). But
the last ¶ does not have actual information, it just knows that it should get
its header from the previous section. When the previous section is gone the
new ¶ cannot get information from nothing. So, if you delete both, Word
cannot invent a header for you.. the header is gone. If you preserve the last
¶, Word seems to be able to transfer the info from the first section to this
¶ before deleting the section break.
In B), the section break and the last ¶ both have actual information
regarding the header, even if it is the same infromation. So, when you delete
both the section break and the last ¶, Word can transfer the information from
the last ¶ to the new last ¶ before deleting the old last ¶...

I see that you achieved the same by not selecting the last ¶ of the document
in your code:
Set rnge = ActiveDocument.Sections.Last.Range
rnge.Start = rnge.Start - 1
rnge.Delete

But sometimes, this can have undesirable effects...

Let's say you have a different header in the last section. If you delete
that last section, the header for that section will become the header for the
first section, whether you delete the last ¶ or not. Also, if you do not
delete the last ¶, the first section will inherit the page set up format of
what used to be the last section because the last ¶ was preserved.

If, in such a case, if you want to preserve the the first section header and
page set up, you have to link the second section header to the first section.
This will effectively copy the first sectin header into the second section
header. Then, unlink the second section, otherwise when you delete the second
section, the first section header will be gone (And you cannot use the "do
not include the las ¶" trick or you might change the margins in the first
section). Now you can delete the last section, its section break and the last
¶.
 
B

bryan

I replied too soon.
In the form it looks good, however when doing a print preview, there is no
section break. I inserted code when adding a file like this which takes care
of the print preview so there is a section break, but then when deleting I
loose the header and footer:
'With ActiveDocument
'ActiveDocument.Unprotect
'Set meNotice = ActiveDocument.Sections(ActiveDocument.Sections.Count).Range

' meNotice.Select
' Selection.Find.Text = "Remove Form"
' If Selection.Find.Execute Then
' Selection.MoveRight Unit:=wdCharacter, Count:=2
' Selection.InsertAfter Chr$(13)
' Selection.MoveRight Unit:=wdCharacter, Count:=2
' Selection.InsertAfter Chr$(13)
' End If
'ActiveDocument.Protect wdAllowOnlyFormFields, NoReset
'End With

Is there code I need when deleting to remove the chr$(13)?
And do this before or after this code to delete:
Dim rnge As Range
Set rnge = ActiveDocument.Sections.Last.Range
rnge.Start = rnge.Start - 1
rnge.Delete

If I leave out the code to insert the chr$(13) the form looks fine but, the
print preview does not have the section break

Would appreciate any help?

Thank,
Bryan
 
J

Jean-Guy Marcil

bryan said:
I replied too soon.
In the form it looks good, however when doing a print preview, there is no
section break. I inserted code when adding a file like this which takes care
of the print preview so there is a section break, but then when deleting I

I do not understand. You state "there is no section break", followewd by "so
there is a section break"...
Which is it? Either there is a break or there isn't...
loose the header and footer:

Read my previous post again. I think I exhausted all possible situations on
this topic. If you are loosing your header/footer, you are not doing it
right. I gave you you many ways to handle that depending on the actual
content of the document you are working with.
'With ActiveDocument
'ActiveDocument.Unprotect
'Set meNotice = ActiveDocument.Sections(ActiveDocument.Sections.Count).Range

' meNotice.Select
' Selection.Find.Text = "Remove Form"
' If Selection.Find.Execute Then
' Selection.MoveRight Unit:=wdCharacter, Count:=2
' Selection.InsertAfter Chr$(13)
' Selection.MoveRight Unit:=wdCharacter, Count:=2
' Selection.InsertAfter Chr$(13)
' End If
'ActiveDocument.Protect wdAllowOnlyFormFields, NoReset
'End With

Is there code I need when deleting to remove the chr$(13)?
And do this before or after this code to delete:

If I leave out the code to insert the chr$(13) the form looks fine but, the
print preview does not have the section break

Hard to help you as I do not know how/when you insert the said setion break...
Would appreciate any help?

Working wih section breaks is tricky because of the many different
combination you can face (Different Odd/Even, Different First Page, whethere
they are linked or not, etc.)
First, you seem confused by the presence/absence of a section break. You
have to first ascertain if there is a section break or not. Go into
Normal/Draft view. A section break will be reprsented by a double dotted
line. Can you see such a line?
Also, you have to identify why the print preview gives you a different page
set up... Are there any hidden characters?

If you need to post back, explain in details how the document you are
testing the code with was created. Also describe its content, inlcuding the
type of headers/footers, number of sections, etc.
 
B

bryan

If only I were a guru!
I am using the templates provided with the header/footer. I do not have the
options to work with the footer/header.
I've tried umpteen different ways with my form.
I think I've narrowed it down a bit to where the problem is that I am hiding
the check box and another form field towards the bottom of the main form. If
I leave those unhidden it works fine. An undesireable solution. I do not have
any paragraph marks after "Remove Form". If I add any paragraph marks, then
when I delete the 2nd page, I loose the header/footer even if I have the last
2 form fieldss not hidden.

I would be open to doing a webex session to have you look at what I am doing
and what I am missing.
I am fairly new to working this indepth with Word and unfortunately have no
giudance other than what I am gathering from the DG, which is great!

Would appreciate any help.

Thanks,
Bryan
 
J

Jean-Guy Marcil

bryan said:
If only I were a guru!
I am using the templates provided with the header/footer. I do not have the
options to work with the footer/header.
I've tried umpteen different ways with my form.
I think I've narrowed it down a bit to where the problem is that I am hiding
the check box and another form field towards the bottom of the main form. If
I leave those unhidden it works fine. An undesireable solution. I do not have
any paragraph marks after "Remove Form". If I add any paragraph marks, then
when I delete the 2nd page, I loose the header/footer even if I have the last
2 form fieldss not hidden.

If you are hiding stuff (Using the "Hidden" attribute on the Font dialog),
make sure you are working in "Show All" mode (Click on the ¶ button to
display all non printing characters). Then, when you apply the hidden
attribute, make sure you do not select the Section break itself. This might
be a bit tricky becasue Word does not highlight in black a selected Section
Break like it does with Page Breaks and other characters... Yet another
bizarre design flaw...
For instance, let's say you have:

....some text.¶
=======(Section Next Page)======
New page paragraph...¶

Position your cursor just before the "t" in "text.¶" above and do the
following:
Hold down the SHIFT key, then hit the Right arrow on the keyboard. The "t"
gets selected. Hit the Right arrow 4 more time to select "text."
Now hit the Right arrow one more time. The ¶ gets selected and you can see
that.
Hit the right arrow yet one more time. Nothing seems to happen on the
screen. In fact, the section break is now selected. To confirm that, hit the
Right arrow again (Still while holding the SHIFT key down) and you will see
that the "N" at the top of the next page gets selected.
If you triple-click a paragraph immediately preceding a section break, the
whole paragraph gets selected, but the section break does not. However, let's
you have a one-line paragraph immediatley before a section break, and instead
of triple-clicking to select the paragrpah, you single-click in the left
margin to select the one-line paragraph, now the section break is selected...

See...? You have to be careful when assigning attributes to text around
section breaks because although they do not print, section breaks are like
characters and will take on any font/paragraph attributes you care to assign
to them.

This migth have to do with the fact that your print preview is different
from your display in regular view.
I would be open to doing a webex session to have you look at what I am doing
and what I am missing.

Sorry, no can do... At work they will certainly not allow a Webex session...
And I do not have time to do NG stuff at home... I have two teen age sons who
are into sports and music... so I do not have lots of free time!

I am fairly new to working this indepth with Word and unfortunately have no
giudance other than what I am gathering from the DG, which is great!

When writing code that handle section breaks, work with ranges as you seem
to be doing.
Then, debug the code step by step (Use F8 in the VBA editor window). But
before you debug, split your screen in two so you have the Word document
window above the VBA editor window. Each time you hit F8 in the VBA editor
window, see what happens to your document (If anything, depending on the
actual line that gets executed... A "Set.." line will not have any impact on
the document, obviously...). Also, for debugging purposes, every time you
change your Range object, insert a "Range.Select" line after modifying the
Range object to see if the new range is what it should be. Once you have the
code do what you want it do, you can remove those "Range.Select" lines.

This way, if you notice that your range is not what you expect it to be, you
know you have to modify the code accordinbgly. Finally, carefully read the
post where I explain what happens to headers/footers in the penultimate
section of a document when you delete the last section of that document.

I know it is frustrating, but I believe we have all gone through this phase
where we learn to control a Range object in our code while learning to
understand the impact of removing section breaks and/or the last "¶" in the
document. It is well worth it to invest some time now, but once you
understand what is going on, you will be able to write very "surgically"
precise code that will handle adding, deleting, modifying sections...
 
B

bryan

Thanks for the advice. I will go through this step by step over the long
weekend.
I offered the webex as a possibility.
I have a 13 yr son and 14 yr old daughter, both into sports. Every night is
baseball and/or softball. My daghter is also in track through school which is
just finishing. So I know what you mean, not a lot of spare time.

Thanks for the help and hopefully I will figure it out!

Thanks,
Bryan
 
J

Jean-Guy Marcil

bryan said:
Thanks for the advice. I will go through this step by step over the long
weekend.
I offered the webex as a possibility.
I have a 13 yr son and 14 yr old daughter, both into sports. Every night is
baseball and/or softball. My daghter is also in track through school which is
just finishing. So I know what you mean, not a lot of spare time.

Thanks for the help and hopefully I will figure it out!

Good luck, and also, I forgot about a useful article... I should have
brought it up from the get go...:
http://word.mvps.org/FAQs/Formatting/WorkWithSections.htm
 
B

bryan

thanks for heads up on the article. I've read it and will need to work abit
more on this. As I mentioned before:
On my template I have the check box that gets formatted as hidden so it does
not print. If I format the check box as hidden (last field), when I insert a
file it acts as a continuous, first 2 lines of added file are on main form.
If I leave the check box as normal, insertion works fine, a section break.

Any idea,
Bryan
 
J

Jean-Guy Marcil

bryan said:
thanks for heads up on the article. I've read it and will need to work abit
more on this. As I mentioned before:
On my template I have the check box that gets formatted as hidden so it does
not print. If I format the check box as hidden (last field), when I insert a
file it acts as a continuous, first 2 lines of added file are on main form.
If I leave the check box as normal, insertion works fine, a section break.

Any idea,

If you use the Range Object in your code, the fact that it is hidden or not
should not matter, as long as your code is written correctly. I mean, if the
inserted range is inserted immediately next to a hidden checkbox (or
character...), it will pick up the hidden attribute as well.

Since you are having problem with this, it may be easier to unhide that
checkbox, insert whatever you need to insert, then re-hide the checkbox...

But, with well written code, you would not need to do this.
 

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