Consulting

Results 1 to 6 of 6

Thread: Deleting ActiveDocument.Sections when True/False values apply to each section

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    54
    Location

    Deleting ActiveDocument.Sections when True/False values apply to each section

    Hi

    I'm hoping someone will have an easy answer to this problem but I realise it's a bit odd.

    I am building a template which could potentially generate up to 15 different letters to various individuals all on the one topic. Each of the 15 letters is contained within the one document, separated by section breaks.

    A1 B1 C1 D1 E1 F1 G1
    A2 D2 E2 F2 G2
    A3 D3
    D4

    In the past I have been able to code define a range where X is the page number I want to delete:-

    Set oRng = ActiveDocument.Sections(X).Range
    oRng.MoveStart wdCharacter -1
    oRng.Delete
    I am now faced however with a dilemma when trying to work out the various combinations of letters depending on true and false values selected by the user.

    For example

    A1-A3 always appear and so I've put these at the start of the document so that their range values will not change.
    B1 is optional
    C1 is optional
    D1 - D4 are optional. However, if D1 is not selected then D2 - D4 are not available, if D2 is not selected, D3-D4 are not available etc.
    The same applies to E, F & G - if option one is not selected then option two cannot be selected independently.

    Is there a way to code to cover off every potential combination on the basis of the above rules and to delete the ranges of the pages which are giving false values? I'm afraid I'm just not good enough with VBA to work this out on my own (and I've been trying for days).

    Any assistance anyone is willing to offer would be of great help to me.

    Thanks

    Dav

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    This can be done quite easily using bookmarks. With D1-D4, for example, simply bookmark that entire range with a single bookmark (e.g. D1); likewise for D2-D4, and so on. Your macro code to delete the unwanted ranges might then be coded along the lines of:
    Sub Demo()
     Dim DelItems As String
     DelItems = InputBox("Please input Groups/Items to delete, separated by spaces" & vbCr & _
     "(e.g. D2 E2 to delete group D2-D4 and item E2)")
     If Trim(DelItems) = "" Then Exit Sub
     Do While InStr(DelItems, "  ") > 0
       DelItems = Replace(DelItems, "  ", " ")
     Loop
     With ActiveDocument
       For i = 0 To UBound(Split(DelItems, " "))
       Select Case Left(Split(DelItems, " ")(i))
         Case "D"
           Select Case Right(Split(DelItems, " ")(i))
             Case "1": .Bookmarks("D1").Range.Delete
             Case "2": .Bookmarks("D2").Range.Delete
             Case "3": .Bookmarks("D3").Range.Delete
             Case "4": .Bookmarks("D4").Range.Delete
           End Select
         Case "E"
           Select Case Right(Split(DelItems, " ")(i))
             Case "1": .Bookmarks("E1").Range.Delete
             Case "2": .Bookmarks("E2").Range.Delete
           End Select
         Case "F"
           Select Case Right(Split(DelItems, " ")(i))
             Case "1": .Bookmarks("F1").Range.Delete
             Case "2": .Bookmarks("F2").Range.Delete
           End Select
         Case "G"
           Select Case Right(Split(DelItems, " ")(i))
             Case "1": .Bookmarks("G1").Range.Delete
             Case "2": .Bookmarks("G2").Range.Delete
           End Select
       End Select
     End With
     End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Jul 2014
    Posts
    54
    Location
    Thanks Paul

    I've always tried to avoid using bookmarks in the past as they've always confused me somewhat. I'll have a play around with this and see how I get on with however. I appreciate the assistance.

    Thanks

    Dav

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    There are other ways, but using bookmarks is perhaps the simplest. Bookmarks are a bit fragile, though (i.e. easily deleted and prone to users expanding their scope), but shouldn't be a problem here. If that doesn't suit, it then becomes a matter of coding so that the designations can be translated into specific Sections, for example.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Jul 2014
    Posts
    54
    Location
    Hi Paul

    I'm afraid I'm losing my mind on this one. I tried the bookmark suggestion and your code but sadly it didn't get me to where I wanted to be. Whether that's because I'm just rubbish with bookmarks or in general I'm not sure.

    I went back to the idea of coding in the delete for each page just using very basic IF statements on the basis of whether the ComboBox in the Userform is vbNullString or not:-

    Dim oRng As Range
    
    If Me.ComboBox36.Value = vbNullString ThenSet oRng = ActiveDocument.Sections(15).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    If Me.ComboBox34.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(14).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    If Me.ComboBox32.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(13).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    If Me.ComboBox30.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(12).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    If Me.ComboBox28.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(11).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    If Me.ComboBox26.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(10).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    
    
    If Me.ComboBox20.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(9).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    If Me.ComboBox19.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(8).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    If Me.ComboBox18.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(7).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    If Me.ComboBox17.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(6).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    If Me.ComboBox16.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(5).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    
    
    If Me.ComboBox14.Value = vbNullString Then
    Set oRng = ActiveDocument.Sections(4).Range
    oRng.MoveStart wdCharacter, -1
    oRng.Delete
    End If
    Unfortunately all this seems to be doing is deleting out Sections 4 to 15 no matter what values are in the ComboBoxes. I'm stumped as I can't think of any logical reason why that would be.

    Any ideas?

    Thanks

    Dav

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by DavG63 View Post
    I tried the bookmark suggestion and your code but sadly it didn't get me to where I wanted to be. Whether that's because I'm just rubbish with bookmarks or in general I'm not sure.

    I went back to the idea of coding in the delete for each page just using very basic IF statements on the basis of whether the ComboBox in the Userform is vbNullString or not:-
    Are you actually using Section breaks to define the Sections? If you're expecting the code to work on something you simply call a Section, but isn't defined via Section breaks, you'll get unexpected results.

    Furthermore, deleting by Section is problematic since, if you delete a Section, all subsequent Sections have their index # decreased by one. At least your code is doing the right thing by deleting them in reverse order. The following works for me:
    Private Sub CommandButton1_Click()
    With ActiveDocument
      If .Sections.Count < 15 Then
        MsgBox "Invalid Action. At least one Section has already been deleted", vbExclamation
        Exit Sub
      End If
      If Me.ComboBox36.Value = vbNullString Then .Sections(15).Range.Delete
      If Me.ComboBox34.Value = vbNullString Then .Sections(14).Range.Delete
      If Me.ComboBox32.Value = vbNullString Then .Sections(13).Range.Delete
      If Me.ComboBox30.Value = vbNullString Then .Sections(12).Range.Delete
      If Me.ComboBox28.Value = vbNullString Then .Sections(11).Range.Delete
      If Me.ComboBox26.Value = vbNullString Then .Sections(10).Range.Delete
      If Me.ComboBox20.Value = vbNullString Then .Sections(9).Range.Delete
      If Me.ComboBox19.Value = vbNullString Then .Sections(8).Range.Delete
      If Me.ComboBox18.Value = vbNullString Then .Sections(7).Range.Delete
      If Me.ComboBox17.Value = vbNullString Then .Sections(6).Range.Delete
      If Me.ComboBox16.Value = vbNullString Then .Sections(5).Range.Delete
      If Me.ComboBox14.Value = vbNullString Then .Sections(4).Range.Delete
    End With
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •