VBA Using Excel to Populate Word Content Controls

richh

Board Regular
Joined
Jun 24, 2007
Messages
245
Office Version
  1. 365
  2. 2016
I'm attempting to populate a Word.dotm document that has content control text boxes using Excel.

In Excel, I create the word objects and open the file, but I can't figure out how to actually access the controls.

In the Word doc's VBA, I can access them via

.SelectContentControlsByTitle("control").Item(1).Range.Text = "..."

but that doesn't work using an Object in Excel..

Here's the code I've tried in Excel:

VBA Code:
Dim objWord         As Object
Dim objDoc          As Object
Dim objSelection    As Object
Dim ctrl            As Word.ContentControl 'Not an Excel type

Set objWord = CreateObject("Word.Application")

Set objDoc = objWord.Documents.Open(ThisWorkbook.Path & "\MyDoc.dotm")

objWord.Visible = True

Set objSelection = objWord.Selection

'Runtime Error 438: Object doesn't support this property or method.
'(This is running in Excel. It works in Word though)
objDoc.SelectContentControlsByTitle("control").Item(1).Range.Text = "..."       

'Can't enter the loop as the early binding doesn't work
For Each ctrl In objDoc.contentcontrols
    If ctrl.Title = "control" Then
        ctrl.Range.Text = "!"
    End If
Next ctrl
 
Last edited:

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.

richh

Board Regular
Joined
Jun 24, 2007
Messages
245
Office Version
  1. 365
  2. 2016
I didn't have the Word 16.0 library referenced. Nevermind!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,194
Messages
5,768,776
Members
425,492
Latest member
blueexcel123

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
Top