Inserting a disk file without its section information

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
 
P

Pesach Shelnitz

Hi Holger,

My solution is also a bit ugly, but it copies tables and footnotes from the
source file and does not create any new sections in the target file.

Sub CopySectionsWithoutBreaks()
Dim srcFile As String
Dim trgFile As String
Dim srcDoc As Document
Dim sect As Section
Dim pos As Long

trgFile = ActiveDocument.name
With Application.FileDialog(msoFileDialogFilePicker)
If .Show Then
srcFile = .SelectedItems(1)
Else
MsgBox "You didn't select a source file to insert."
Exit Sub
End If
End With
Set srcDoc = Documents.Open(srcFile)
Documents(trgFile).Activate
With Selection
For Each sect In srcDoc.Sections
.EndKey Unit:=wdStory
.InsertParagraphAfter
pos = .Range.End
sect.Range.Copy
.Paste
.start = pos
.End = pos
With .Find
.Text = "^b"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
Next
End With
srcDoc.Close
Set srcDoc = Nothing
End Sub
 
P

Pesach Shelnitz

Hi Holger,

After I posted my ugly macro, I realized that it actually contained the
solution for avoiding the copy/paste and for using InsertFile. Here is my
revised macro.

Sub CopySectionsWithoutBreaks()
Dim srcFile As String
Dim pos As Long

With Application.FileDialog(msoFileDialogFilePicker)
If .Show Then
srcFile = .SelectedItems(1)
Else
MsgBox "You didn't select a source file to insert."
Exit Sub
End If
End With
With Selection
.EndKey Unit:=wdStory
.InsertParagraphBefore
pos = .Range.End
.InsertFile fileName:=srcFile
.start = pos
.End = pos
With .Find
.Text = "^b"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End With
End Sub
 
O

Ogier

Pesach Shelnitz said:
Hi Holger,

After I posted my ugly macro, I realized that it actually contained the
solution for avoiding the copy/paste and for using InsertFile. Here is my
revised macro.

Sub CopySectionsWithoutBreaks()
Dim srcFile As String
Dim pos As Long

With Application.FileDialog(msoFileDialogFilePicker)
If .Show Then
srcFile = .SelectedItems(1)
Else
MsgBox "You didn't select a source file to insert."
Exit Sub
End If
End With
With Selection
.EndKey Unit:=wdStory
.InsertParagraphBefore
pos = .Range.End
.InsertFile fileName:=srcFile
.start = pos
.End = pos
With .Find
.Text = "^b"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End With
End Sub

It really does, thank you very much! I spent much time trying to solve this,
so your input will really make things a lot easier for me.

As you can see, I work with Range instead of Selection, so I changed your
code slightly into

Sub InsertDiskFile(ByRef rng As Word.Range, DiskFileName As String)
Dim Pos As Long
With rng
.Collapse Direction:=wdCollapseEnd
.InsertParagraphBefore
Pos = .End
.InsertFile FileName:=DiskFileName
.Start = Pos
.End = Pos
With .Find
.Text = "^b"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End With
Set rng = ActiveDocument.Content
rng.Collapse Direction:=wdCollapseEnd
End Sub

The two final commands ensure that the end of the document is reached; from
here I can make further insertions.

Is "^b" (Control-B ?) a marker for a section break? Is this described
somewhere? I have previously encountered Chr(11), a soft line break, and
Chr(160), a non-breaking space.

Thank you for your time, best wishes
Holger
 
P

Pesach Shelnitz

Hi Holger,

Using Range objects instead of the Selection object is definitely a better
coding practice. In this case, however, there is very little cursor movement,
so the benefit of using a Range object is minimal.

The ^b code (Shift-6 followed by b) is for a section break. The line break
character Chr(11) corresponds to the code ^l, and nonbreaking space
corresponds to ^s. You can find these and other codes in the expanded Search
and Replace dialog box. When you insert a section break from the drop-down
list into your search string, ^b will appear in the search string.
 

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