Replace Content Controls using VBA (Word to Excel)

nmc

New Member
Joined
Aug 25, 2022
Messages
38
Office Version
  1. 2021
Platform
  1. Windows
Hello,
I'M trying to replace the contents conntrol of a word document but my code isn't working.
Situation: I need to replace content controls of word document with the contents of column B considering Navette sheet but this should only happen if the column A has the same name as the titles of content controls.
Example:
Content control of document title = Name
Column A cell A1 name = Name
Replacement of content control should be content of cell B1
It should occur in loop
I made a code but isn't working:

VBA Code:
[
Dim row As Long
For row = 1 To Navette.UsedRange.Rows.Count
    If Navette.Range("A" & row).Value = wordDoc.ContentControls(Navette.Range("A" & row).Value).Range.Text Then
        wordDoc.ContentControls(Navette.Range("A" & row).Value).Range.Text = Navette.Range("B" & row).Value
    End If
    Next row
    
    
]
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Situation: I need to replace content controls of word document with the contents of column B considering Navette sheet but this should only happen if the column A has the same name as the titles of content controls.
Do you mean you want to replace the text contents of the content controls, or the content controls themselves? If the former, try this:

VBA Code:
    Dim CC As Object 'Word.ContentControl
    For row = 2 To Navette.UsedRange.Rows.Count
        Set CC = Nothing
        On Error Resume Next
        Set CC = wordDoc.SelectContentControlsByTitle(Navette.Range("A" & row).Value).Item(1)
        On Error GoTo 0
        If Not CC Is Nothing Then CC.Range.Text = Navette.Range("B" & row).Value
    Next row
 
Upvote 0
Wow! It worked!
Maybe no I need to inser a Loop?
For example I have more than one word that as Content Control Title "Name" but everytime that I run the code only replace one of them

I must insert loop in the end?
 
Upvote 0
I have more than one word that as Content Control Title "Name" but everytime that I run the code only replace one of them
Do this to replace the text contents of every Content Control which have the specified title:

VBA Code:
    Dim CC As Object 'Word.ContentControl
    For row = 2 To Navette.UsedRange.Rows.Count
        For Each CC In wordDoc.SelectContentControlsByTitle(Navette.Range("A" & row).Value)
            CC.Range.Text = Navette.Range("B" & row).Value
        Next
    Next row
 
Upvote 1
Solution
Wow!!!!! It worked so well!

Now If I want to do the same but for bookmarks it's like this?

Set CexcelApp = New Excel.Application
Set CExcelWb = CexcelApp.Workbooks.Open("C:\Add-in\Copy of Mapping.xlsx")
Set CexcelWsh = CExcelWb.Sheets("Clauses")

With wordDoc
Dim bm As Bookmark
For Each bm In ActiveDocument.Bookmarks
Set bm = ActiveDocument.Bookmarks(CexcelWsh.Range("D" & row).Value)
If Not bm.Range.Text = "" Then
bm.Range.Text = CexcelWsh.Range("F" & row).Value
End If
Next bm
End With
 
Upvote 0
Sorry I inserted wrong
It like this if I want to appear only if it's yes?

Set CexcelApp = New Excel.Application
Set CExcelWb = CexcelApp.Workbooks.Open("C:\Add-in\Copy of Mapping.xlsx")
Set CexcelWsh = CExcelWb.Sheets("Clauses")

Set ExcelNavette = Workbooks("Siemens SLS - Navette.xlsx")
ExcelNavette.Activate
ExcelNavette.Sheets("Navette").Activate
Set Navette = ExcelNavette.Sheets("Navette")


With wordDoc
Dim bm As Bookmark
If Navette.Range("A" & row).Value) = Yes Then
For Each bm In ActiveDocument.Bookmarks
Set bm = ActiveDocument.Bookmarks(CexcelWsh.Range("D" & row).Value)
Else
Nothing
End If
Next bm
End With
 
Upvote 0
Wow! It worked!
Maybe no I need to inser a Loop?
For example I have more than one word that as Content Control Title "Name" but everytime that I run the code only replace one of them

I must insert loop in the end?
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,693
Members
449,048
Latest member
81jamesacct

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