0

在此处输入图像描述

我有一个问题。(现在再次使用代码)所以,当我在一个单元格中时,我将创建一个突出显示它的函数(cellColor()),当我在 ContenControl 中时,我还有一个突出显示的函数(toggleYellow)它。所以有一个简单的 If-function.(checkTableCC),它会查看您是在 ContentControl 中还是在 Cell 中。所以没有问题。

但是当有一个表并且在该表中有ContentControl并且在 ContentControl 中又有一个新表时,当我想突出显示ContentControl中的表的单元格并且在另一个表中时,If-Function 不起作用,我收到错误 4641 HighlightColor 语句当前已禁用。我尝试了一些东西,但我无法仔细检查该功能。也许有人可以帮忙。

到目前为止,这是我在你们的帮助下完成的代码。再次感谢你:

'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



4

0 回答 0