Solved

How to show a building block gallery or custom gallary using VBA in Word 2007

Posted on 2011-09-19
10
754 Views
Last Modified: 2012-05-12
I am reposting this question because I did not get an answer the first time, I'm hoping it was because it was in the wrong zone and not because its unanswerable.  I apologize if someone sees this twice (I'm having trouble picking zones in my browser - customer support is working on that.)

anyway..

I have a Word template which I designed in '03 and have now converted to '07.  One of the features I used heavily was to copy a statement into autotext and then assign it to a button on a custom toolbar.     I'm aware you can't do that anymore, at least not without creating code for it.

I've figured out how to put the statements into a custom gallery using the building blocks feature and how to put a custom gallery button on the quick toolbar.  

My problem is that I also need to have the document protected in order to continue to use legacy fields which have calcuations and references to other fields throughout the document.  Apparently '07 will not allow you to use the gallery buttons, even if the section in which you are attempting to insert text is unprotected.
So I"m looking for code that will:
1 - turn the protection off
2 - open the custom gallery so the user can insert the text he/she wants.
3 - turn the protection back on.

Any help with this.  I'd consider myself of intermediate ability with Access VBA, but I'm still a beginner with Word VBA.

If this can't work an alternative strategy would be greatly appreciated.  Ultimately I want the user to be able to view a list of "autotext" entries grouped by a category and choose the one they want.  I have discovered that you can use the buildingblocks organizer to view and insert in an unprotected section,  but there's no way to filter or group the entries easily and this makes it way too complicated for most of my users who have rudmentary understanding of Words features at best.

any help would be greatly appreciated.

Thanks experts!
0
Comment
Question by:BLCSwimmer
  • 6
  • 4
10 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36566153
I'm wondering whether this can be done at all in VBA. Some of the ribbon functions use 'classic' Word dialogues and commands which can still be used via VBA, but a lot of the new stuff needs to use XML

Removing and re-enabling protection is easy enough, it's the bit that you already do that might be difficult. Can you describe what you did to create the QAT that opens your Custom gallery?
0
 

Author Comment

by:BLCSwimmer
ID: 36566350
Custom gallery was listed as one of the choises to drag onto the quick toolbar.  

I went to Options --> customize --> "commands not in the ribbon" and chose "custom Gallery 1."  Then I assigned the text entries to the custom gallery through properties in building blocks.

I've played around a little with XML and was able to create a custom ribbon tab using the custom UI editor but that's the extent of my knoweldge of it.  



0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36567097
We could consider creating a VBA Userform to list the entries by name and select from the list. It wouldn't be possible to show all the formatting in a preview pane, though some text might be viewable.

Would anything like that be acceptable?
0
 

Author Comment

by:BLCSwimmer
ID: 36567332
Yes - that would be actually be closer to my original toolbar from '03.  The user doesn't really need to see the formating or text, I used the custom gallery feature because I couldn't figure out another way to display the choices.

To give you an example.  The document is an educational template use for special education services.

So the toolbar looked something like this.

Accomodation (main menu)
               Testing (Sub menu)
                    Read Orally
                   Dictate Responses
                   Modify Length of test

When use user chose "read Orally" it would insert a paragraph of boilerplate text.




0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 36570863
Hmm, a bit more complicated that I expected, but here is the code for the Userform

Option Explicit

Private Sub BBEntryCombo_Change()
    cmdOK.Enabled = Len(BBEntryCombo.Text) > 0
End Sub

Private Sub cmdCancel_Click()
    Finish
End Sub

Private Sub cmdOK_Click()
    Dim oBB As BuildingBlock
    Dim oTmp As Template
    Dim t As Integer
    
    For t = 1 To Templates.Count
        Set oTmp = Templates(t)
        If oTmp.Name = "Building Blocks.dotx" Then
            Exit For
        End If
    Next t

    Set oBB = oTmp.BuildingBlockEntries(BBEntryCombo.Text)
    oBB.Insert Selection.Range, True
    Finish
End Sub

Private Sub Finish()
    ActiveDocument.Protect wdAllowOnlyFormFields, True ',password
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    Dim oBBT As BuildingBlockType
    Dim oCat As Category
    Dim oTmp As Template
    Dim t As Long
    Dim c As Long
    Dim b As Long
    Dim bbty As WdBuildingBlockTypes
    Dim strBuildingBlock As String
    Dim strTemplateName As String
    Dim strTemplatePath As String
    Dim strUser As String
    
    ActiveDocument.Unprotect 'password
    strUser = Environ("UserName")
    strTemplatePath = "C:\Users\" & strUser & "\AppData\Roaming\Microsoft\Document Building Blocks\1033\"
    strTemplateName = "Building Blocks.dotx"
    Application.AddIns.Add strTemplatePath & strTemplateName, True
    
    'Building Blocks.dotx
    For t = 1 To Templates.Count
        Set oTmp = Templates(t)
        If oTmp.Name = "Building Blocks.dotx" Then
            Exit For
        End If
    Next t
    
    Set oBBT = oTmp.BuildingBlockTypes(wdTypeCustom1)
    bbty = oBBT.Index
    If oBBT.Categories.Count > 0 Then
        'Categories can exist that don't have any BBEs
       If HasBlocks(oBBT) = True Then
            For c = 1 To oBBT.Categories.Count
                Set oCat = oBBT.Categories(c)
                If oCat.BuildingBlocks.Count > 0 Then
                    For b = 1 To oCat.BuildingBlocks.Count
                        strBuildingBlock = oBBT.Categories(c).BuildingBlocks(b).Name
                        BBEntryCombo.AddItem strBuildingBlock
                    Next b
                End If
            Next c
        End If
    End If

End Sub

Open in new window


I have also attached a template. The extension has been changed to .dot, but it is a 2007 template, and the extension must be changed to .dotm which isn't accepted for attachment. It has a QAT button which calls a macro to open the form.  
 
BBlocks.dot
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36570876
Note that it looks for Custom1 entries (wdTypeCustom1 in line 60)
0
 

Author Comment

by:BLCSwimmer
ID: 36573116
When I downloaded the template it converted to a .doc file - I changed the extention to .dotm but when  I tried to open it wouldn't open.  I changed the extention to .dot and it did open.

The macro stops at line 64 HasBlocks giving me a compile error "sub or function not defined"
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 500 total points
ID: 36573345
Sorry, that function is one that I wrote a while ago. Here it is:
Function HasBlocks(ByRef BBT As BuildingBlockType) As Boolean
    Dim i As Long
    For i = 1 To BBT.Categories.Count
        If BBT.Categories(i).BuildingBlocks.Count > 0 Then
            HasBlocks = True
            Exit Function
        End If
    Next
End Function

Open in new window

0
 

Author Comment

by:BLCSwimmer
ID: 36584031
I tried this out and it worked great.  Thanks so much.

Maybe one further refinement and I'm good to go.

I have the boilerplates grouped by category - Is it possible to have multiple comboboxes on the form and have each combobox display one category?  I could then put the category header on the userform.   If not I can always change the title of the autotext to group them together, but this might be more visually appealing.

For example
combobox displays Custom 1, Category 1

and combobox 2 displays custom 1, category 2

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36593257
Sorry. I missed your last comment. If you haven't worked it out for yourself, the code would need to be modified like this:
If oCat.BuildingBlocks.Count > 0 Then
                    For b = 1 To oCat.BuildingBlocks.Count
                        strBuildingBlock = oBBT.Categories(c).BuildingBlocks(b).Name
                        Select Case oCat.Name
                             Case "MyCat1"
                                  BBEntryCombo1.AddItem strBuildingBlock
                             Case "MyCat2"
                                  BBEntryCombo2.AddItem strBuildingBlock
                             Case ...
                         End Select
                    Next b
                End If

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now