jeudi 17 mars 2022

Show the next contents in a table in Word

Team,

Scenario I have a word document where I have a table as shown in Image 1. The checkboxes are used to show the next contents. For example, I have in first step yes and no, when yes is checked the next content is shown. And in next step, I have thre Checkboxes with case 1,2 and 3 respectively. enter image description here

When the case 1 is checked I have next a text that is filled via vba as F1Feld1...till F4Feld1.

Problem First problem is, I am unable to create a fucntion where only yes and no can be checked as well as either of the case can be checked. Second, problem is that the vba for case checkboxes run perfectly when I have them created seperate but when combined together only case 1 vba runs.

Following is my code

Option Explicit
Dim tabelle As Table, zelle As Cell
Private Sub Document_ContentControlOnEnter(ByVal CC As ContentControl)

    Dim r As Range
    Set tabelle = ActiveDocument.Bookmarks("local").Range.Tables(1)
    If ActiveDocument.SelectContentControlsByTag("Yes").Item(1).Checked = True Then

        ActiveDocument.SelectContentControlsByTag("No").Item(1).Checked = False
        Call local_blockiert

    Else: Call local_offen

    End If
    If ActiveDocument.SelectContentControlsByTag("Case1").Item(1).Checked = True Then
        On Error Resume Next
        With ActiveDocument
            .Bookmarks("TB1").Range.Text = "F1Feld1": .Bookmarks("TB1").Range.Font.ColorIndex = wdBlack
            .Bookmarks("TB2").Range.Text = "F1Fed2": .Bookmarks("TB2").Range.Font.ColorIndex = wdBlack
            .Bookmarks("TB3").Range.Text = "F1Feld3": .Bookmarks("TB3").Range.Font.ColorIndex = wdBlack
            .Bookmarks("TB4").Range.Text = "F1Feld4": .Bookmarks("TB4").Range.Font.ColorIndex = wdBlack
        End With
    ElseIf ActiveDocument.SelectContentControlsByTag("Case1").Item(1).Checked = False Then
        With ActiveDocument
            .Bookmarks("TB1").Range.Text = ""
            .Bookmarks("TB2").Range.Text = ""
            .Bookmarks("TB3").Range.Text = ""
            .Bookmarks("TB4").Range.Text = ""
        End With
    ElseIf ActiveDocument.SelectContentControlsByTag("Case2").Item(1).Checked = True Then
        With ActiveDocument
            .Bookmarks("TB1").Range.Text = "F2Feld1": .Bookmarks("TB1").Range.Font.ColorIndex = wdBlack
            .Bookmarks("TB2").Range.Text = "F2Fed2": .Bookmarks("TB2").Range.Font.ColorIndex = wdBlack
            .Bookmarks("TB3").Range.Text = "F2Feld3": .Bookmarks("TB3").Range.Font.ColorIndex = wdBlack
            .Bookmarks("TB4").Range.Text = "F2Feld4": .Bookmarks("TB4").Range.Font.ColorIndex = wdBlack
        End With
    ElseIf ActiveDocument.SelectContentControlsByTag("Case2").Item(1).Checked = False Then
        With ActiveDocument
            .Bookmarks("TB1").Range.Text = ""
            .Bookmarks("TB2").Range.Text = ""
            .Bookmarks("TB3").Range.Text = ""
            .Bookmarks("TB4").Range.Text = ""
        End With
    ElseIf ActiveDocument.SelectContentControlsByTag("Case3").Item(1).Checked = True Then
        With ActiveDocument
            .Bookmarks("TB1").Range.Text = "F3Feld1": .Bookmarks("TB1").Range.Font.ColorIndex = wdBlack
            .Bookmarks("TB2").Range.Text = "F3Fed2": .Bookmarks("TB2").Range.Font.ColorIndex = wdBlack
            .Bookmarks("TB3").Range.Text = "F3Feld3": .Bookmarks("TB3").Range.Font.ColorIndex = wdBlack
            .Bookmarks("TB4").Range.Text = "F3Feld4": .Bookmarks("TB4").Range.Font.ColorIndex = wdBlack
        End With
    ElseIf ActiveDocument.SelectContentControlsByTag("Case3").Item(1).Checked = False Then
        With ActiveDocument
            .Bookmarks("TB1").Range.Text = ""
            .Bookmarks("TB2").Range.Text = ""
            .Bookmarks("TB3").Range.Text = ""
            .Bookmarks("TB4").Range.Text = ""
        End With
    End If

End Sub
Private Sub local_blockiert()
    Dim i As Long, j As Long
    On Error GoTo fehler
    With ActiveDocument.Bookmarks("local").Range
        .Font.ColorIndex = wdWhite
    End With

fehler:
Call AllesAuf
End Sub
Private Sub local_offen()
    Dim i As Long, j As Long
    On Error GoTo fehler
    With ActiveDocument.Bookmarks("YesorNo").Range
        .Font.ColorIndex = wdBlack
    End With

fehler:
Call AllesAuf

End Sub


Private Sub yes_blockiert()
    Dim j As Long
    On Error GoTo fehler

    With tabelle.Cell(2, 2)
        .Shading.ForegroundPatternColorIndex = wdGray25
        .Range.Font.ColorIndex = wdGray25
        For j = 1 To .Range.ContentControls.Count
            .Range.ContentControls(j).LockContents = True
        Next j
    End With
    Exit Sub
fehler:
Call AllesAuf
End Sub

Private Sub yes_offen()
    Dim j As Long
    On Error GoTo fehler

    With tabelle.Cell(2, 2)
        For j = 1 To .Range.ContentControls.Count
            .Range.ContentControls(j).LockContents = False
        Next j

        .Shading.ForegroundPatternColor = RGB(255, 242, 204)
        .Range.Font.ColorIndex = wdAuto

    End With
    Exit Sub
fehler:
Call AllesAuf
End Sub

Private Sub AllesAuf()
    Dim i As Long

    With ActiveDocument
        For i = 1 To .ContentControls.Count
            .ContentControls(i).LockContents = False
        Next i
    End With
End Sub

Hope to find a solution for this. Thank you.




Aucun commentaire:

Enregistrer un commentaire