Word VBA Issue copying content from other documents with mulitplesections

M

mablake

I am trying to code around a copy and paste issue with Word. In that
if you
copy and paste the contents of a document that has multiple sections
into an
existing document, it brings with it header and footer information. I
know
if I manually select the content of each section in turn and past
that,
headers and footers remain unaffected. I need to recreate this in
code.
Currently I use:

Sub InsertTextFromBoilerPlate()
Dim dlgOpen As FileDialog
Dim strFileLoc As String
Dim vrtSelectedItemFields As Variant

On Error Resume Next
Dim ChkResults As Variant

If
IsMissing(ActiveDocument.CustomDocumentProperties("ValidTemplate"))
Then
MsgBox "This toolbar is restricted to Valid Corporate
templates
only..."
Exit Sub
End If

Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)

With dlgOpen
.Filters.Add "Word Document Format", "*.doc", 1
If .Show = -1 Then
.AllowMultiSelect = False

For Each vrtSelectedItemFields In .SelectedItems
strFileLoc = vrtSelectedItemFields
Next vrtSelectedItemFields
Else
End If
End With
Set dlgOpen = Nothing
If strFileLoc = "" Then

Else
Documents.Open FileName:=strFileLoc,
ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False,
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False,
WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto,
XMLTransform:=""

Selection.WholeStory
Selection.Expand (wdMainTextStory)
Selection.Copy
ActiveDocument.Close
Selection.PasteAndFormat (wdPasteDefault)
'Selection.InsertFile FileName:=strFileLoc, Range:="", _
'ConfirmConversions:=False, Link:=False, Attachment:=False
End If

End Sub

I have experimented with selecting sections, but it always brings the
Header
and Footer. Does anyone know how to select the text within a
section,
meaning section(1) line 1 to last line of section? Or another way of
solving
this problem?

Your assistance is appreciated, Mark
 
K

Klaus Linke

I have experimented with selecting sections, but it always brings
the Header and Footer.

How about deselecting the end-of-section-marker at the end of the selected
section, before you copy?
Say with
Selection.MoveEndWhile CSet:=Chr(12), Count:=wdBackward

Regards,
Klaus
 

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