PDA

View Full Version : Programatically delete building blocks by category



bstephens
09-02-2010, 08:13 PM
Hi, I am using Greg Maxey's code as seen below to delete Building Blocks.

The following code will delete the building block "Test Block" just fine.

But is there a way to modify the below code so that it will delete all the building blocks that are in the category "Test Category" instead of just a single building block called "Test Block"?

Sub DeleteBBProgramatically()
Dim oTmp As Template, sPath As String
sPath = Options.DefaultFilePath(wdUserTemplatesPath) & "\Normal.dotm"
Set oTmp = Templates(sPath)
oTmp.BuildingBlockTypes(wdTypeCustom1).Categories("Test Category").BuildingBlocks("Test Block").Delete
End Sub

Thanks for your input.

Best,
Brian

gmaxey
09-03-2010, 03:23 AM
Sub DeleteBBsProgramatically()
Dim oTmp As Template, sPath As String, i As Long, oBBs As BuildingBlocks
sPath = Options.DefaultFilePath(wdUserTemplatesPath) & "\Normal.dotm"
Set oTmp = Templates(sPath)
Set oBBs = oTmp.BuildingBlockTypes(wdTypeCustom1).Categories("Test Category").BuildingBlocks
For i = oBBs.Count To 1 Step -1
oBBs(i).Delete
Next i
Set oTmp = Nothing
Set oBBs = Nothing
End Sub

bstephens
09-03-2010, 03:13 PM
Thanks Greg, works well.

gmaxey
09-03-2010, 05:13 PM
bstephens,

I don't know why, but while the code I sent you works Microsoft decided to not let the following work:

Sub DeleteBBsProgramaticallyII()
Dim oTmp As Template, sPath As String, i As Long
Dim oBB As BuildingBlock, oBBs As BuildingBlocks
sPath = Options.DefaultFilePath(wdUserTemplatesPath) & "\Normal.dotm"
Set oTmp = Templates(sPath)
Set oBBs = oTmp.BuildingBlockTypes(wdTypeCustom1).Categories("Test Category").BuildingBlocks
For Each oBB In oBBs
oBB.Delete
Next oBB
Set oTmp = Nothing
Set oBBs = Nothing
Set oBB = Nothing
End Sub