# Editing Word contentcontrol content with Excel VBA



## dicktimmerman (Jan 19, 2018)

Hi all,

I have a sort of Database built in Excel and a Word document (Certificate of Conformity)  which has to be filled out with Data from the Excel Database.
I already sorted out how I can fill the several "ContentControls" with VBA, now i'm stuck with the following problem.

There are some fields in the word document of which certain parts have to be striked out.
For instance: Steering category: <strike>manual/</strike>power-assisted/<strike>servo steering/differential</strike> (1).

I have named my contentcontrol "Steering_Cat", know how to add text to that, now is the challenge to select wording in this contentcontrol and add the "strikethroug" property to that part of the contentcontrol.

Who has the solution? I don't.


----------



## John_w (Jan 19, 2018)

Welcome to MrExcel forums.

This Word VBA macro works if "Steering_Cat" is a rich text content control, *not *a plain text content control.  With a plain text CC, setting the strikethrough of a single character affects the whole text value.


```
Public Sub Content_Control_Strikethrough_Text()

    Dim CC As ContentControl
    Dim CCrange As Range
    Dim i As Long, p As Long
    Dim plainPart As String
    
    'Get the Steering_Cat content control
    
    Set CC = ActiveDocument.SelectContentControlsByTitle("Steering_Cat").Item(1)
    Set CCrange = CC.Range
    
    'Set all its text to non-strikethrough
    
    CCrange.Text = "manual/power-assisted/servo steering/differential"
    For i = 1 To Len(CCrange.Text)
        CCrange.Characters(i).Font.StrikeThrough = False
    Next
        
    'Set all its text except "power-assisted" to strikethrough
    
    plainPart = "power-assisted"
    
    p = InStr(1, CCrange.Text, plainPart, vbTextCompare)
    If p > 0 Then
        'Strikethrough characters before plain part, if any
        For i = 1 To p - 1
            CCrange.Characters(i).Font.StrikeThrough = True
        Next
        'Strikethrough characters after plain part, if any
        p = p + Len(plainPart)
        For i = p To Len(CCrange.Text)
            CCrange.Characters(i).Font.StrikeThrough = True
        Next
    End If
    
End Sub
```


----------



## dicktimmerman (Jan 30, 2018)

Thnx John,

Been a week skiing so replying a little late, your solution worked almost immediately, only had to change "Dim CCrange As Range" into "Dim CCrange As Variant" because code came back with "Type Mismatch" error.

Now have to figure out how to implement in my existing code.


----------



## Macropod (Jan 30, 2018)

Running from Excel with early binding, you'd do better to use:
Dim CCrange As Word.Range
Running from Excel with late binding, you'd do better to use:
Dim CCrange As Object


----------



## dicktimmerman (Jan 30, 2018)

Ahhh, Macropod, I'm not a "programmer" in I have learned the programming in school, I've learned it beeing a little "self learning".

What do you mean by early and late binding?


----------



## Macropod (Jan 30, 2018)

See:
https://msdn.microsoft.com/en-us/vb...ifiers-late-binding-and-early-binding-project
https://support.microsoft.com/en-us/help/245115/using-early-binding-and-late-binding-in-automation


----------

