Consulting

Results 1 to 3 of 3

Thread: ContentControls Add to Word 2016 Documents through Excel 2016 VBA Code…

  1. #1
    VBAX Regular
    Joined
    Feb 2017
    Posts
    10
    Location

    ContentControls Add to Word 2016 Documents through Excel 2016 VBA Code…

    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.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: https://www.mrexcel.com/forum/excel-...-vba-code.html
    Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •