Consulting

Results 1 to 16 of 16

Thread: Problems with my Building Block Template

  1. #1

    Problems with my Building Block Template

    I'm back again with some more questions on my custom letter templates project.

    Background:
    I have created some letter templates which use a Userform to populate a letter with the relevant paragraphs. The paragraphs are building building blocks inserted into content controls. The building blocks were originally in my Normal.dotm file. It all worked beautifully!

    However, the point was to allow for all of my colleagues to use these templates (via their instance of Word rather than opening up the templates). In our office the majority of people work on a remote desktop (using thin clients); I am one of a few who use an actual laptop and work on my desktop rather than on the remote desktop application. We have a 3rd party IT support service who have helped to place the templates and building blocks template onto each persons User profile so that they can be accessed by their instance of Word. This has been done by holding the files in a central location on the remote server and then running script to deploy a these files into each users profile (in the right places for Word to access them I think).

    The problem that I am having is this:
    If I leave the building blocks in the Normal.dotm file, then when this is deployed to each user then it seems to replace their Normal.dotm file and then when they try to create a completely blank new word document it actually contains all of the building blocks. We obviously don't want this to happen, so we renamed the building blocks file to BuildingBlocks.dotm.

    I have obviously updated my VBA code in the letter templates for the new file name & path, but it still seems to be having problems accessing the file. My IT support tell me that this is because the BuildingBlocks.dotm file is not launching in the same way that a Normal.dotm file would therefore the VBA can't get access to it. I know that the Normal.dotm file is a special kind of file but I don't know how to make my BuildingBlocks.dotm file have the same characteristics. I have seen that people do use files other than their Normal.dotm to house their building blocks so can you tell me what I am doing wrong that is causing the file not to launch?

    Thanks
    SJ

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Normal loads when Word is started. You have to load the building block template. Your IT could do that by putting it in each user's startup folder or you could load it with code e.g.,:

    AddIns.Add filename:=p_strDatapath & "\Firm Building Block Bank.dotm", Install:=True

    ..or you might try:

    Templates.LoadBuidlingBlocks
    Last edited by gmaxey; 07-26-2019 at 06:35 AM.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Hi Greg,

    Thank you for replying so quickly!

    I have tried putting the file in my Start up folder on my laptop and this method worked straight away.

    However, I also have a remote desktop where I can trial the functionality for those users who work on remote desktops and it didn't seem to work. I'm wondering if it is something in the filepath I am using.

    The BuildingBlocks.dotm template is held in a central location so that edits to it can be made in one place and then they are reflected in the version deployed to each users profile. Therefore the filepath in the central template has to be generic (they call it a 'wild card') so that it selects each users specific file path to their Start up folder. My IT people gave me this filepath to use:

    c:\users\%username%\%AppData%\Roaming\Microsoft\Word\STARTUP\BuildingBlocks .dotm

    I'm wondering if the filepath is the problem?

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Sarah,

    When you start Word on one of these remote laptops, is the BuildingBlocks.dotm loaded? If isn't then you IT people aren't placing it in the user's startup folder. If it is, then you need to set a template object variable to it. E.g.,
    Dim m_oTmp as Template
    If m_oTmp Is Nothing Then
    For Each m_oTmp In Templates
    If UCase(m_oTmp.Name) = "BUILDINGBLOCKS.DOTM" Then Exit For
    Next m_oTmp
    End If

    Now you should be able to use m_oTmp in your code to insert various BBs.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    when they try to create a completely blank new word document it actually contains all of the building blocks
    No it doesn't. Building blocks are stored only in templates. If you use a global template like the normal template or a template in the startup folder they are available to all documents on that PC. If you want building blocks to apply only to particular documents then they should be stored in that document's template.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    Quote Originally Posted by gmaxey View Post
    Sarah,

    When you start Word on one of these remote laptops, is the BuildingBlocks.dotm loaded? If isn't then you IT people aren't placing it in the user's startup folder. If it is, then you need to set a template object variable to it. E.g.,
    Dim m_oTmp as Template
    If m_oTmp Is Nothing Then
    For Each m_oTmp In Templates
    If UCase(m_oTmp.Name) = "BUILDINGBLOCKS.DOTM" Then Exit For
    Next m_oTmp
    End If

    Now you should be able to use m_oTmp in your code to insert various BBs.
    Hi Greg

    No my IT people had put the file in the Templates folder, the same as where the Normal.dotm file is because originally that is where building blocks were pulled from. However, following your original reply I put a copy of the building blocks template in my start up folder on my remote desktop to test your solution and it still didn't work.

    I have now tried the above code but it just throws an exception (my userform doesn't even load). However I may be putting it in the wrong place (I put it at the top of my userform code as it is my userform code which is trying to pull the building blocks). I don't really understand what that piece of code does. My original code for the user form sets the template as the template path I've been trying to use:

    Set oTmp = Templates("C:\Users\xx\AppData\Roaming\Microsoft\Word\STARTUP\Building Blocks.dotm")

    Does this not already set the object variable for the template?

    Sorry, I've literally had to learn all of this on the fly for this project, so I'm probably not understanding the nuances of the various ways of coding.

    SJ

  7. #7
    Quote Originally Posted by gmayor View Post
    No it doesn't. Building blocks are stored only in templates. If you use a global template like the normal template or a template in the startup folder they are available to all documents on that PC. If you want building blocks to apply only to particular documents then they should be stored in that document's template.
    Hi Graham,

    I think I may have misunderstood how the building blocks are saved and not explained myself properly.

    Essentially I had all the content of the building blocks written out in my Normal.dotm, so every time I opened a blank document I had all of the content there on the page. I think I understand now that I don't need to leave all of that content written on the page in order for the building blocks to actually be saved into the template. However, the building blocks organiser is quite frankly s**t! I can't expand the window which makes the details of the controls difficult to read easily (have to constantly scroll left to right and expand columns) and the preview of the building block in the preview pane is so tiny I can't actually see what the content of it is! In addition, I need to be able to edit some of the building blocks annually (because I want the content to change not the functioning of my userform letter templates).

    My solution was to leave all of the content of my building blocks in the document (all labelled up so I know which building blocks they are) - this way I can easily edit the content and resave it to the same building block label, therefore overwriting the original.

    So I guess my question is then, presumably I can have all this content saved in a regular file, but each time I create a building block from it I save the building block to the Normal.dotm to make it available to my various letter templates? (I don't need the building blocks to be saved to specific templates as some of them are used in multiple ones).

    Thanks,
    SJ

  8. #8
    Building blocks are stored in templates, not documents. By default they are stored in the normal template, but you can choose instead to save the building block in any open template or installed add-in template. There is also a BuildingBlocks template which stores the default building blocks provided with Word.

    Building blocks can be called from any template that contains them that is loaded into Word, either as an add-in or because you have created a document from that template.

    The building blocks organizer will allow you to move a building block from its current location to any other open or loaded template by selecting the block and using the edit function to save it in a different location. Similarly when you save a building block by selecting the content and pressing ALT+F3 you can choose where to save it without having to first save it in the normal template.

    Where you store the building blocks depends on where they are to be used and by whom. For specific documents, save them in the document templates. For use just by you on all documents, save them in the normal template or the buildingblocks template. For use by others, save them in an add-in template and make that template available to the others.

    Greg has a couple of Building Block utilities on his web site which may prove helpful - such as https://gregmaxey.com/word_tip_pages...galleries.html
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    Ok so Here is an example of my UserForm code as was originally...

    'Apply introduction as to what is included
    Dim oCCA As ContentControl
    Dim oCCB As ContentControl
        Set oCCA = ActiveDocument.SelectContentControlsByTitle("ccINTROACTION1").Item(1)
        Set oCCB = ActiveDocument.SelectContentControlsByTitle("ccINTROACTION2").Item(1)
        Select Case True
            'Paper Version
            Case chbTR And chbR185 And Not chbAccounts
                Set oTmp = Templates("C:\users\%username%\%appdata%\Roaming\Microsoft\Templates\Building Blocks.dotm")
                oTmp.BuildingBlockEntries("bbTRONLY1").Insert Where:=oCCA.Range, RichText:=True
                oTmp.BuildingBlockEntries("bbPPRTRR185").Insert Where:=oCCB.Range, RichText:=True
            Case chbTR And chbAccounts And Not chbR185
                Set oTmp = Templates("C:\users\%username%\%appdata%\Roaming\Microsoft\Templates\Building Blocks.dotm")
                oTmp.BuildingBlockEntries("bbTRACCOUNTS1").Insert Where:=oCCA.Range, RichText:=True
                oTmp.BuildingBlockEntries("bbPPRTRACCOUNTS").Insert Where:=oCCB.Range, RichText:=True
            Case chbTR And chbR185 And chbAccounts
                Set oTmp = Templates("C:\users\%username%\%appdata%\Roaming\Microsoft\Templates\Building Blocks.dotm")
                oTmp.BuildingBlockEntries("bbTRACCOUNTS1").Insert Where:=oCCA.Range, RichText:=True
                oTmp.BuildingBlockEntries("bbPPRTRACCOUNTSR185").Insert Where:=oCCB.Range, RichText:=True
            Case chbTR And Not chbR185 And Not chbAccounts
                Set oTmp = Templates("C:\users\%username%\%appdata%\Roaming\Microsoft\Templates\Building Blocks.dotm")
                oTmp.BuildingBlockEntries("bbTRONLY1").Insert Where:=oCCA.Range, RichText:=True
                oTmp.BuildingBlockEntries("bbPPRTRONLY").Insert Where:=oCCB.Range, RichText:=True
            'Electronic Version
            Case Not chbTR And chbR185 And Not chbAccounts
                Set oTmp = Templates("C:\users\%username%\%appdata%\Roaming\Microsoft\Templates\Building Blocks.dotm")
                oTmp.BuildingBlockEntries("bbTRONLY1").Insert Where:=oCCA.Range, RichText:=True
                oTmp.BuildingBlockEntries("bbTRR185").Insert Where:=oCCB.Range, RichText:=True
            Case Not chbTR And chbAccounts And Not chbR185
                Set oTmp = Templates("C:\users\%username%\%appdata%\Roaming\Microsoft\Templates\Building Blocks.dotm")
                oTmp.BuildingBlockEntries("bbTRACCOUNTS1").Insert Where:=oCCA.Range, RichText:=True
                oTmp.BuildingBlockEntries("bbTRACCOUNTS2").Insert Where:=oCCB.Range, RichText:=True
            Case Not chbTR And chbR185 And chbAccounts
                Set oTmp = Templates("C:\users\%username%\%appdata%\Roaming\Microsoft\Templates\Building Blocks.dotm")
                oTmp.BuildingBlockEntries("bbTRACCOUNTS1").Insert Where:=oCCA.Range, RichText:=True
                oTmp.BuildingBlockEntries("bbTRACCOUNTSR185").Insert Where:=oCCB.Range, RichText:=True
            Case Not chbTR And Not chbR185 And Not chbAccounts
                Application.Templates.LoadBuildingBlocks
                Set oTmp = Templates("C:\users\%username%\%appdata%\Roaming\Microsoft\Templates\Building Blocks.dotm")
                oTmp.BuildingBlockEntries("bbTRONLY1").Insert Where:=oCCA.Range, RichText:=True
                oTmp.BuildingBlockEntries("bbTRONLY2").Insert Where:=oCCB.Range, RichText:=True
            
        End Select
    I have tried all of the following, none of which have worked:

    1. I have tried having the Building Blocks file in the StartUp folder (with the file enabled as an Add-In in the Options) obviously I changed the file path to pull from the StartUp folder instead of Templates.

    2. I have tried Application.Templates.LoadBuildingBlocks.

    3. I have tried

    Dim m_oTmp as Template
    If m_oTmp Is Nothing Then
    For Each m_oTmp In Templates
    If UCase(m_oTmp.Name) = "BUILDINGBLOCKS.DOTM" Then Exit For
    Next m_oTmp
    End If
    With this coded to happen on the opening of a new document
    And changed my code to
    Case chbTR And chbR185 And Not chbAccounts
                m_oTmp.BuildingBlockEntries("bbTRONLY1").Insert Where:=oCCA.Range, RichText:=True
                m_oTmp.BuildingBlockEntries("bbPPRTRR185").Insert Where:=oCCB.Range, RichText:=True
    4. I realised that my UserForm initialises on Document_New and figured that it was maybe opening before the Building Blocks had loaded so I changed it so that the UserForm was loaded manually (I have a UI Ribbon button) after the file opened.

    5. I know that the Building Blocks are never loaded until you perform an action which loads them (such as using the insert building blocks function) or you tell them to load; I tried opening my file, inserting a building block (from the organizer) and then running the UserForm.

    I'm assuming that, if my building blocks file is visible in the project pane then the file is loaded and should be able to be used (even though, when the actual file is not open, it will say unable to view) - is that correct? Even when the file is there in the project pane it still won't call my building blocks; I get runtime error 5941.

    Please can someone tell me what I am missing?

  10. #10
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Sarah,

    So after the End If in your 3. above, add Msgbox m_oTmp.FullName. Does it return the path of your building block template? If yes, then the template is definitely loaded . If not, well then it isn't.

    Yes, if a template is just loaded, but not open then you will not be able to view its code.

    Put a Stop in your code above the select case. Run, it. When it hits the stop, step through with F8 and find out which line throws the error.
    Greg

    Visit my website: http://gregmaxey.com

  11. #11
    Quote Originally Posted by gmaxey View Post
    Sarah,

    So after the End If in your 3. above, add Msgbox m_oTmp.FullName. Does it return the path of your building block template? If yes, then the template is definitely loaded . If not, well then it isn't.

    Yes, if a template is just loaded, but not open then you will not be able to view its code.

    Put a Stop in your code above the select case. Run, it. When it hits the stop, step through with F8 and find out which line throws the error.
    With the file in the StartUp folder then the msg box returns my filepath - so must be loaded.

    The lines of code that are causing the problem are literally every line that is calling on the building blocks. My large chunk of code above specifies the building blocks to use to fill two content controls depending on the varying combinations of about 4 different checkboxes. Each time I run the Userform, whichever combination of those checkboxes I tick, it will find the correct case but then gets stuck on the line that tells it which building block to use:

    Case chbTR And chbR185 And Not chbAccounts
    
    
                m_oTmp.BuildingBlockEntries("bbTRONLY1").Insert Where:=oCCA.Range, RichText:=True
                m_oTmp.BuildingBlockEntries("bbPPRTRR185").Insert Where:=oCCB.Range, RichText:=True

  12. #12
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Sarah,

    I just recorded a macro to insert a Normal.dotm building block in a plain text CC. Then wrote a custom macro to do the same thing:

    Sub Macro1()
      Application.Templates("D:\My Documents\Word\Templates\Normal.dotm"). _
      BuildingBlockEntries("Sincerely yours,").Insert Where:=Selection.Range, _
      RichText:=True
    End Sub
    
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oCC As ContentControl
    Dim oTmp As Template
      For Each oTmp In Templates
        If UCase(oTmp.Name) = "NORMAL.DOTM" Then
          Exit For
        End If
      Next oTmp
      Set oCC = ActiveDocument.SelectContentControlsByTitle("Tests").Item(1)
      oTmp.BuildingBlockEntries("Sincerely yours,").Insert Where:=oCC.Range, RichText:=True
    lbl_Exit:
      Exit Sub
    End Sub
    which I think is similar to what you are doing. The only way to get that error on that line that I see is a misnamed building block. If you want to wrap your template and file into a zip file and send it to me, you can and I will take a look. Just use the Feedback link on my website.

    Best Regards,
    Greg Maxey

    Saru mo ki kara ochiru (literally: Monkey even tree from fall). ~ Japanese Proverb
    Greg

    Visit my website: http://gregmaxey.com

  13. #13
    Hi Greg,

    I have just sent you an email with the zipped file.

    One thought though - I don't think it can be misnamed building blocks as the error occurs on every single line that references the building blocks file.

    Thanks,
    Sarahjane

  14. #14
    I am sure Greg can get to the bottom of the problem when he gets up (he's in the US), but if you are sure that the named building block is present you can access it from wherever it is stored using the macro at the end of https://www.gmayor.com/word_vba_examples_3.htm
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  15. #15
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Sarahjane,

    I replied via e-mail. As you are now aware, it was a scope issue. Happens to all of us form time to time ;-)

    For others reading this the OP was getting a runtime time error on:

    m_oTmp.BuildingBlockEntries("bbTRONLY1").Insert Where:=oCCA.Range, RichText:=True

    She had declared m_oTmp and set it to a template in the templates collection. Unfortunately she did it at a procedure level in her document_open event.

    She had then declared the variable m_oTmp again in her form click event so she got no compile errors. Naturally at runtime m_oTmp was nothing and the error occurred.
    Greg

    Visit my website: http://gregmaxey.com

  16. #16
    VBAX Newbie
    Joined
    Aug 2019
    Location
    United States Texas
    Posts
    1
    Location
    I think I may have misunderstood how the building blocks are saved and not explained myself properly.
    Link of casino spiele is here

Posting Permissions

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