inserting section break without header/footer

B

bryan

I am posting this new looking for some help. I have posted this before but my
last post went unanswered.

I have a template with header and footer.
In the template is a checkbox to insert a file, which I want to insert
without a header and footer.
I could insert just pagebreak but, that will not allow me to update fields
in the added file only.
How can I not have the header/footer carry forward?

Here is the code I am using:
Sub chk1()
'
' chk1 Macro
' Macro created 12/2/2008 by bjsorens
'
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then

stringfile = "W:\Claims\ImageRight\document attachments\"
docoa = "Driver Statement.doc"

Dim myDoc As Document
Dim i As Integer

Set myDoc = ActiveDocument
With myDoc
.Unprotect
With .Range
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
With .Sections.Last
For i = 1 To 3
With .Headers.Item(i)
.LinkToPrevious = False
.Range.Delete
End With
With .Footers.Item(i)
.LinkToPrevious = False
.Range.Delete
End With
Next i

.Range.InsertFile stringfile & docoa
End With
End With
.Protect wdAllowOnlyFormFields, NoReset
End With

Else

End If
End Sub


Thanks,
Bryan
 
J

Jean-Guy Marcil

bryan said:
I am posting this new looking for some help. I have posted this before but my
last post went unanswered.

I have a template with header and footer.
In the template is a checkbox to insert a file, which I want to insert
without a header and footer.
I could insert just pagebreak but, that will not allow me to update fields
in the added file only.
How can I not have the header/footer carry forward?

Here is the code I am using:
Sub chk1()
'
' chk1 Macro
' Macro created 12/2/2008 by bjsorens
'
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then

stringfile = "W:\Claims\ImageRight\document attachments\"
docoa = "Driver Statement.doc"

Dim myDoc As Document
Dim i As Integer

Set myDoc = ActiveDocument
With myDoc
.Unprotect
With .Range
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
With .Sections.Last
For i = 1 To 3
With .Headers.Item(i)
.LinkToPrevious = False
.Range.Delete
End With
With .Footers.Item(i)
.LinkToPrevious = False
.Range.Delete
End With
Next i

.Range.InsertFile stringfile & docoa
End With
End With
.Protect wdAllowOnlyFormFields, NoReset
End With

Else

End If
End Sub


Thanks,
Bryan

Working with sections is never an easy task, especially with VBA.

When you insert a file like you are doing with your code, if the inserted
file already has a defined header/footer, it is automatically retained (and
unlinked from the previous section).
However, if a header/footer is undefined in the inserted file, Word
automatically applies the "Link to Previous = True" default.

So, unlinking BEFORE inserting the file has no effect.
You have to unlink AFTER.

Here is a variation on your code.
The code checks if the last section header/footer is "Link to Previous,"
which means that the inserted file had no header/footer of its own, so in
those cases the code deletes them. If you want to systematically delete all
headers/footers from the inserted file, remove the "IF" blocks.

Finllay, keep in mind that if the inserted file has more than one section,
things might get a little more complicated!

Oh, and don't forget to Dim all variables...

Sub chk1()

Dim myDoc As Document
Dim i As Integer
Dim stringfile As String
Dim docoa As String

If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
stringfile = "W:\Claims\ImageRight\document attachments\"
docoa = "Driver Statement.doc"

Set myDoc = ActiveDocument
With myDoc
.Unprotect
With .Range
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
.InsertFile stringfile & docoa
With .Sections.Last
For i = 1 To 3
With .Headers.Item(i)
If .LinkToPrevious Then
.LinkToPrevious = False
.Range.Delete
End If
End With
With .Footers.Item(i)
If .LinkToPrevious Then
.LinkToPrevious = False
.Range.Delete
End If
End With
Next i
End With
End With
'You had a mistake here with "NoReset"
.Protect wdAllowOnlyFormFields, True
End With

Else

End If

End Sub
 
B

bryan

Hey Jean-Guy,
That did the trick!
You would not believe how much this has been bugging me.

Two questions:
1) What do you mean by
" If you want to systematically delete all
headers/footers from the inserted file, remove the "IF" blocks"?
Do you mean the whole blocks or just the
if and the end statement, meaning do regardless

2) What is meant by
"Finllay, keep in mind that if the inserted file has more than one section,
things might get a little more complicated!"
Do you mean if the inserted file has a header and footer ?

Thanks again,
Bryan
 
J

Jean-Guy Marcil

Hey Jean-Guy,
That did the trick!
You would not believe how much this has been bugging me.

Two questions:
1) What do you mean by
" If you want to systematically delete all
headers/footers from the inserted file, remove the "IF" blocks"?
Do you mean the whole blocks or just the
if and the end statement, meaning do regardless

Yes, keep the code between the If and End If
2) What is meant by
"Finllay, keep in mind that if the inserted file has more than one section,
things might get a little more complicated!"
Do you mean if the inserted file has a header and footer ?

Yes, and/or also, if it has more than one section.
As is, the code works on the last section of the inserted document. It the
inserted document has one section, it will be the last of the document as
well. If the inserted document has more than one section, the code does not
address the section(s) that are before the last one in the inserted document.
 
B

bryan

Thanks again for all the help.
As I mentioned I had tried this from prior post but, I was doing this stuff
after inserting the break not after inserting the file.

Again kudos to you and to all who make this discussion group so great!

Bryan
 

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