Editing Word contentcontrol content with Excel VBA

dicktimmerman

New Member
Joined
Jan 11, 2018
Messages
12
Office Version
  1. 365
Platform
  1. Windows
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.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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.

Code:
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
 
Upvote 0
Solution
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.
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,223,779
Messages
6,174,492
Members
452,567
Latest member
ONEIL290

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top