PDA

View Full Version : Help with ActiveDocument.Sections(X).Range.Delete



DavG63
12-15-2015, 01:30 PM
Hi

I have a Word document broken down into three sections, each section containing the same basic document but with different DocVariables so that I can create up to three copies at a time, using different base information, i.e. name and address details for three different people.

In my current code ComboBox1 relates to Section 1; ComboBox 2 to Section 2; and ComboBox3 to Section 3. There will always be at least one version of my document created when running the macro and so I've focused on variables involving ComboBoxes 2 and 3.




IF Me.ComboBox2.Value = "" and Me.ComboBox3.Value = "" Then
ActiveDocument.Sections(3).Range.Delete
ActiveDocument.Sections(2).Range.Delete
Selection.WholeStory
Selection.Fields.Unlink
Else
End If
If Me.ComboBox2.Value <> "" and Me.ComboBox3.Value = "" Then
ActiveDocument.Sections(3).Range.Delete
Selection.WholeStory
Selection.Fields.Unlink
Else
End If
If Me.ComboBox2.Value <> "" and Me.ComboBox3.Value <> "" Then
Selection.Wholestory
Selection.Fields.Unlink
End If



What I'm finding is that whilst the code runs fine and the unneeded documents are deleted, what I'm left with is a single blank page tacked onto the end of my document which reports as being the same section number as that just deleted. For instance, if I delete Section 3 automatically, I end up with blank page in place of Section 3, or if I delete Sections 2 and 3 I end up with a blank claiming to be Section 2.

Does anyone know why this blank page would exist? I've been through the entire document and confirmed that there are only section breaks where I want them. There are some content controls in the footer but I can't see how these would have any bearing.

Thanks in advance

Dav

gmaxey
12-15-2015, 04:16 PM
Deleting a section range does not delete the section break that created it:

Sub Test()
Dim oRng As Range
Set oRng = ActiveDocument.Sections(3).Range
oRng.MoveStart wdCharacter, -1
oRng.Delete
Set oRng = ActiveDocument.Sections(2).Range
oRng.MoveStart wdCharacter, -1
oRng.Delete
End Sub

DavG63
12-16-2015, 01:34 AM
Hi Greg

As I fully expected this worked a treat.

Thanks again

Dav

gmaxey
12-16-2015, 07:01 AM
You're welcome. Glad I could help.