Jacek Rzadca
Jacek Rzadca

Reputation: 79

Insert Table from Builiding Block in specified location in the word document.

I want to insert a table which is defined as building block. I placed a content control in specified location in the document and refer to it by "selectcontetcontrolsbytag". Unfortunetly when table is inserted to the conentcontrol, it is convertered to RichText. Here is my code:

ThisDocument.SelectContentControlsByTag("TermsConditions").Item(1).Range = _
ActiveDocument.AttachedTemplate.BuildingBlockTypes.Item(wdTypeTables).Categories.Item("Terms and Conditions Translation").BuildingBlocks.Item("Terms and Conditions Eng")

Could you help me with proper code to insert building block in specified location. Also I would like this building block to be replaced by another, when user will select other item from userform, combobox etc.

Upvotes: 0

Views: 444

Answers (2)

Jacek Rzadca
Jacek Rzadca

Reputation: 79

Complete solution for my problem is:

  1. Solution proposed by Cindy Meister Replacing content inside content control:
  2. To change content inside content control "TermsConditions" I added following code:

    If doc.SelectContentControlsByTag("TermsConditions").Item(1).Range.Text <> doc.SelectContentControlsByTag("TermsConditions").Item(1).PlaceholderText Then doc.SelectContentControlsByTag("TermsConditions").Item(1).Range.Cut Else End If

Upvotes: 1

Cindy Meister
Cindy Meister

Reputation: 25663

I'm not sure what you mean by "it is converted to Rich Text"...

The accepted way to insert a BuildingBlock is to use the BuildingBlock.Insert method to which you pass the target Range object. For example (based on your code sample):

Dim doc as Word.Document
Dim rngTarget as Word.Range
Set doc = ActiveDocument
Set rngTarget = doc.SelectContentControlsByTag("TermsConditions").Item(1).Range 
doc.AttachedTemplate.BuildingBlockTypes.Item(wdTypeTables).Categories.Item("Terms and and Conditions Translation").BuildingBlocks.Item("Terms and Conditions Eng").Insert rngTarget, true

Take a look at the example in the VBA Language Reference...

Also, you should not use ThisDocument in your code, use ActiveDocument. (Even better, declare a Document object, assign ActiveDocument to it, then use that.)

Upvotes: 0

Related Questions