I have a problem.
So, when I am in a cell then I gonna make a function that highlights it(cellColor()) and when I am in a ContenControl I also have a function which highlights(toggleYellow) it. So there is a simple If-function.(checkTableCC), which looks if you are in a ContentControl or in a Cell. So there is no problem.
But when there is a table and in that table there is ContentControl and in the ContentControl there is again a new table the If-Function does not work when I want to
highlight the cell of the table which is in the ContentControl and which is in another table and I get an error
4641 The HighlightColor statement is currently disabled. I tried some things out but I cannot get the double check of the function. Maybe someone can help.
The error appears here: Selection.Cells(1).Range.HighlightColorIndex = wdBrightGreen
So I want to highlight the "Hey" in that Cell in the image.
Here is my code so far which I did with the help of you guys. Thank you again:
Code:
'CC function
Function IsSelectionInCC(sel As Word.Selection) As Word.ContentControl
Dim rng As Word.Range
Dim doc As Word.Document
Dim nrCC As Long
Dim cc As Word.ContentControl
Dim InCC As Boolean
InCC = False
Set rng = sel.Range
Set doc = rng.Parent
rng.Start = rng.Document.Content.Start
nrCC = rng.ContentControls.Count
If nrCC > 0 Then
If sel.InRange(doc.ContentControls(nrCC).Range) Then
InCC = True
Set cc = doc.ContentControls(nrCC)
Else
sel.MoveEnd wdCharacter, 1
If Len(sel) = 0 Then
InCC = True
Set cc = doc.ContentControls(nrCC)
End If
End If
End If
Set IsSelectionInCC = cc
End Function
'Highlight ContentControl[![enter image description here][1]][1]
Sub toggleYellow()
Dim rng As Word.Range
Dim cc As Word.ContentControl
Set cc = IsSelectionInCC(Selection)
If Not cc Is Nothing Then
Set rng = cc.Range
If rng.HighlightColorIndex = wdNoHighlight Then
rng.HighlightColorIndex = wdYellow
Else
rng.HighlightColorIndex = wdNoHighlight
End If
End If
End Sub
'Highlight cell
Sub cellColor()
Selection.Collapse Direction:=wdCollapseStart
If Not Selection.Information(wdWithInTable) Then
MsgBox "Nur in einer Tabelle funktioniert dieses Makro."
Exit Sub
End If
If Selection.Cells(1).Range.HighlightColorIndex = wdNoHighlight Then
Selection.Cells(1).Range.HighlightColorIndex = wdYellow
Else
Selection.Cells(1).Range.HighlightColorIndex = wdNoHighlight
End If
Exit Sub
End Sub
'Check If you are in a CC or in a Cell
Sub checkTableCC()
Dim rng As Word.Range
Dim cc As Word.ContentControl
Set cc = IsSelectionInCC(Selection)
If Not cc Is Nothing Then
toggleYellow
Else
cellColor
End If
End Sub