O
Ogier
My problem is to insert a source file (with neither section breaks nor
headers nor footers) at the end of a target file, which contain many
sections, each with their own header and footer. As I understand it, the
section information stored at the end of the source file must be avoided in
the process: If I manually copy *all* of the source file (Ctrl-A) the
header/footer of my last target file section disappears, but if I very
carefully mark the source file *without* the final section information
(hardly visible as an extra blank space in the marked text after the last
character) and copy this, everything works fine.
After many failed attempts using InsertFile, in desperation I have used this
approach:
Sub InsertFile(ByRef rng As Word.Range, DiskFileName As String)
Dim R As Word.Range
Dim doc As Word.Document
Documents.Open FileName:=DiskFileName, _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
Set doc = ActiveDocument
Set R = doc.Content
R.End = R.End - 3 'Avoid Section information
R.Select
R.Copy
ActiveDocument.Close
rng.Paste
rng.Collapse Direction:=wdCollapseEnd
End Sub
This works, but is ugly because of the overhead involved (opening a file and
using the clipboard) and the shortening of the range R.
I would much prefer to use InsertFile, but cannot figure out how to avoid
the section informationat the end. I have read articles on the subject by
Dave Rado (no code given) and tried my luck with a sub by Jean-Guy Marci in
this discussion group (RE: inserting section break without header/footer),
condensed into
Sub InsertFile(ByRef rng As Word.Range, DiskFileName As String)
With rng
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
.InsertFile FileName:=DiskFileName
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
End Sub
But I must be missing something. Debugging shows, that the If-statements are
never entered. And how do I get rid of the inserted break? If I delete it
manually, I am back to the lost Header/footer situation.
I would much appreciate help here!
Best wishes
Holger Nielsen
headers nor footers) at the end of a target file, which contain many
sections, each with their own header and footer. As I understand it, the
section information stored at the end of the source file must be avoided in
the process: If I manually copy *all* of the source file (Ctrl-A) the
header/footer of my last target file section disappears, but if I very
carefully mark the source file *without* the final section information
(hardly visible as an extra blank space in the marked text after the last
character) and copy this, everything works fine.
After many failed attempts using InsertFile, in desperation I have used this
approach:
Sub InsertFile(ByRef rng As Word.Range, DiskFileName As String)
Dim R As Word.Range
Dim doc As Word.Document
Documents.Open FileName:=DiskFileName, _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
Set doc = ActiveDocument
Set R = doc.Content
R.End = R.End - 3 'Avoid Section information
R.Select
R.Copy
ActiveDocument.Close
rng.Paste
rng.Collapse Direction:=wdCollapseEnd
End Sub
This works, but is ugly because of the overhead involved (opening a file and
using the clipboard) and the shortening of the range R.
I would much prefer to use InsertFile, but cannot figure out how to avoid
the section informationat the end. I have read articles on the subject by
Dave Rado (no code given) and tried my luck with a sub by Jean-Guy Marci in
this discussion group (RE: inserting section break without header/footer),
condensed into
Sub InsertFile(ByRef rng As Word.Range, DiskFileName As String)
With rng
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
.InsertFile FileName:=DiskFileName
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
End Sub
But I must be missing something. Debugging shows, that the If-statements are
never entered. And how do I get rid of the inserted break? If I delete it
manually, I am back to the lost Header/footer situation.
I would much appreciate help here!
Best wishes
Holger Nielsen