PDA

View Full Version : [SOLVED:] ContentControls Add to Word 2016 Documents through Excel 2016 VBA Code…



AreJay
08-20-2017, 08:21 PM
I can create various ContentControls for Word 2016 using VBA code from within Word.

However, I am creating various Word templates through Excel, using Excel VBA code, based upon data in an Excel file.

When I attempt to create ContentControls in Excel for a Word document (calling ActiveDocument), I get an error:

Compile error:
Method or data member not found

For example, when called from Excel:

Dim objCC As ContentControl

Set objCC = ActiveDocument.ContentControls.Add(wdContentControlRichText)

It errors on “.ContentControls”

If this is caused a missing reference in Excel, what is the appropriate reference to add in addition to the MS Word 16.0 Object Library?

Again, calling strictly from Word, it works fine.

Since this seems to be primarily an Excel issue, I have posted this within the Excel forum.

If there is another possible cause, any guidance would be appreciated.

Thank you.

macropod
08-20-2017, 10:06 PM
You need to spend some time learning how to automate one application from another, especially when it comes to declaring and using variables. For example, with early binding:

Sub Demo()
Dim wdApp As New Word.Application, wdDoc As Word.Document, wdCCtrl As Word.ContentControl
Set wdDoc = wdApp.Documents.Add
With wdDoc
Set CCtrl = .ContentControls.Add(Type:=wdContentControlText, Range:=.Characters.Last)
With CCtrl
.Range.Text = "My Text"
End With
End With
End Sub
As for:

what is the appropriate reference to add in addition to the MS Word 16.0 Object Library?
Did you look for the Word object entry under Tools|References?

PS: This is not an Excel issue; it's an automation issue. I've moved the thread accordingly.

macropod
08-20-2017, 11:10 PM
Cross-posted at: https://www.mrexcel.com/forum/excel-questions/1019729-contentcontrols-added-word-2016-documents-through-excel-2016-vba-code.html
Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3