vbams-wordword-2013

copy contents of a section to another section, without disturbing section breaks?


I have a source and a destination Word 2013 doc. Each document has multiple section breaks with very particular footers in each of those sections which I cannot disturb. I need to copy just the contents (without the section break) of a certain section from the source doc, and paste those contents to a certain section of the destination doc - e.g. copy the text of source section 3 to dest section 5.

The problem is that when I copy the source section, that copy command also includes the section break character from the source doc. So when I paste it into the destination doc, it either blows away that dest section's break character (or adds a new section if that destination section was the last one in the document, and therefore did not have a section break character following it).

Is there a way in Word, with a VBA macro, to copy just the raw contents of a given section from the source document without copying that section's section break and paste them into a different doc without blowing away that destination section's section break?

I have tried all sort of variations like this:

source.Sections(3).Range.Select
source.Sections(3).Range.Copy
dest.Sections(5).Range.Select
dest.Sections(5).Range.Paste

But the paste line disturbs the section breaks of the destination document. I have also tried reducing the selection length from the source doc (before I copy it) by one character, hoping to exclude the section break:

source.Sections(3).Range.Select
source.ActiveWindow.Selection.MoveEnd Unit:=wdCharacter, Count:= -1  ' (I also tried -2, -3, etc)
source.Sections(3).Range.Copy
dest.Sections(5).Range.Select
dest.ActiveWindow.Selection.MoveEnd Unit:=wdCharacter, Count:= -1  ' (I also tried -2, -3, etc)
dest.Sections(5).Range.Paste

These reductions in the selection reduce the actual text of the section, but dont seem to exclude the section break, which I assume is in the selection range?


Solution

  • Thanks Cindy! Your suggestion got me to where I needed to be. Your code needed a little tweaking. You dim'd rngSec as a Word.Section but it complains; I think you meant Word.Range, no? And without doing a rng.select, the copy line complained that no text was selected.

    Here is code to take the contents of the sections from one doc, and put them into reverse order in a different doc - without affecting any section breaks:

    Option Explicit
    
    Sub switch_sections()
    
    Dim SourceDoc As Document, DestDoc As Document
    Dim i As Integer
    Dim has_section_break As Boolean
    
    Set SourceDoc = Application.Documents("source.docx")
    Set DestDoc = Application.Documents("destination.docx")
    
    Dim SrcRng As Range    ' Word.Section
    Dim DestRng As Range    ' Word.Section
    
    For i = 1 To SourceDoc.Sections.Count
        With SourceDoc.Sections(i).Range.Find
            ' Check for a section break.  Put this find first, else it
            ' screws up the selection we will do below.
            .Text = "^b"
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .Execute
            If .Found Then
                has_section_break = True
            End If
        End With
    
        Set SrcRng = SourceDoc.Sections(i).Range
        SrcRng.Select
        If has_section_break Then SrcRng.MoveEnd wdCharacter, -1
        SrcRng.Copy     ' Copy all but section break
    
        With DestDoc.Sections(DestDoc.Sections.Count - (i - 1)).Range.Find
            ' Check for a section break.  Put this find first, else it
            ' screws up the selection we will do below.
            .Text = "^b"
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .Execute
            If .Found Then
               has_section_break = True
            End If
        End With
        Set DestRng = DestDoc.Sections(DestDoc.Sections.Count - (i - 1)).Range
        DestRng.Select
        If has_section_break Then DestRng.MoveEnd wdCharacter, -1
    
        DestRng.Paste   ' Replace all but the section break
       Next
     End Sub