Access VBA Code to insert Word Building Block in Word Document (1 Viewer)

AngC

New member
Local time
Today, 11:26
Joined
Oct 31, 2017
Messages
2
Hi Everyone,

I have a table in Access containing custom MS Word Building Block categories and names which are stored in Word Templates. I am trying to build an Access form where the user can select a building block name and programatically paste that building block into the active word document in the selected area.

The macro I am using works within Word, but not from Access (I have added the MS Word 16.0 Object Library in the Access VBA Editor). I also want to change this code to dynamically fill the names for the Categories and the Building Blocks from the selection in the Access Form.

Can anyone help?

Private Sub PasteBBlockInWordDoc ()

Dim objTemplate As Template
Dim objBB As BuildingBlock

Set objTemplate = ActiveDocument.AttachedTemplate

Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustomAutoText) _
.Categories("SubOutcome").BuildingBlocks("*A1C1OptionA")


Selection.MoveUp Unit:=wdLine, Count:=1
objBB.Insert Selection.Range

End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:26
Joined
May 7, 2009
Messages
19,229
Maybe something similar like this
 

Attachments

  • aaWordContentControl.zip
    32.5 KB · Views: 154

AngC

New member
Local time
Today, 11:26
Joined
Oct 31, 2017
Messages
2
Thanks so much Arnelgp, I'm not using any content controls though, as the end user needs to have a fair amount of editing input for the Word doc - I really only want to be able to insert the word building block that is selected on the Access form to be inserted where the user has their cursor in the word document (to get around the unwieldy Building Blocks Organiser in Word). Is there a way of modifying your Access code to include the code below (which works well when contained just in the Word VBA environment)?

Dim objTemplate As Template
Dim objBB As BuildingBlock

Set objTemplate = ActiveDocument.AttachedTemplate

Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustomAutoText) _
.Categories("SubOutcome").BuildingBlocks("*A1C1OptionA")


Selection.MoveUp Unit:=wdLine, Count:=1
objBB.Insert Selection.Range
 
Last edited:

Users who are viewing this thread

Top Bottom