Consulting

Results 1 to 12 of 12

Thread: How to use corporate template by default and have it launch a form on new document

  1. #1
    VBAX Regular ShogunPatch's Avatar
    Joined
    Jan 2018
    Location
    London
    Posts
    14
    Location

    How to use corporate template by default and have it launch a form on new document

    Hi all
    I'm new to the forum; self taught, reasonably proficient with Word basics but pretty poor at VBA.

    I have created a corporate template which includes not only styles, etc, but also lots of custom building blocks and want to roll it out to all users in the company (all five of them!). But I'm having a few problems:

    1. I want to set things up so that it is the template which Word uses by default to create new documents; i.e. if I just click on Word in the Start Bar I want it to launch into a blank document based on the corporate template, not normal.dot; similarly if I click on New Document on the QAT or go to File/New. Specifically, I want to bypass the page where I have to choose the template and have it create using my custom template by default. I would have thought this was a simple and fairly common requirement but I can't for the life of me get it to work that way.

    2. When a new document is created, I want a userform to pop up automatically offering options to insert various building blocks (or not, as the case may be). I want that to happen, again, regardless of whether it is as Word launches into a new document based on the template (as per 1. above), or whether a new document is created from within an open instance of Word (by whatever mechanism). The only time I don't want the form to pop up is if an existing document is opened (whether word launches into it or it is opened from within an open instance). My userforms and associated commands are all setup and working, including the pasting of the Buildings Blocks, but I can't get the form to come up correctly and automatically on initial launch and on New.

    Any pointers would be much appreciated.

  2. #2
    While you could do this it is not a good idea to do anything that messes with the intended use of the normal template. The normal template is a required system file and substituting your template will have unwanted knock-on effects. It will also irritate your users when it messes with their preferences that aid their productivity.

    The simplest solution is to create an add-in which adds a button to the Home tab (or adds a company tab to the ribbon) which when clicked creates a document from your template. (see template in attached zip). The attachment also adds a button to the backstage > File Menu. To change the text of these buttons you will need the CustomUI editor - see also http://gregmaxey.mvps.org/word_tip_p...bbon_main.html

    Yoiu could put a link to your template on the desktop which would create a new document using that template when clicked.

    Or you could download the Select Template add-in from my web site - http://www.gmayor.com/select_document_template.htm which expands on the attachment and will allow you to create a folder for your company templates and easily create new documents. The last used document is always stored, so creating a new document using your template takes two clicks rather than the one provided by a dedicated add-in. The add-in replaces the built-in file creation functions.
    Attached Files Attached Files
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular ShogunPatch's Avatar
    Joined
    Jan 2018
    Location
    London
    Posts
    14
    Location
    Thank you for you prompt reply Graham.

    I was not proposing to replace the Normal template if I could avoid it, as I recognise that is not ideal. I have found an article which allows me to launch into my template when I start Word using a switch, but any additional new documents created thereafter revert to the normal template.
    As my understanding was that global templates load-up automatically alongside the normal template when Word starts, I guess what I was hoping was that I could set my template as a global template and then somehow have all new documents be based on the global template by default, rather than on the Normal template; however, perhaps what I was looking to do cannot be done, in which case your suggestion to add a button to the Home tab (or add a company tab to the ribbon) is probably the best alternative.
    I shall investigate your attachment and the Select Template add-in on your site and revert if I have any further questions.

    Many thanks.

  4. #4
    VBAX Regular ShogunPatch's Avatar
    Joined
    Jan 2018
    Location
    London
    Posts
    14
    Location
    Graham
    Your attachment worked a treat and I think that is definitely the way forward so thanks for that (I'm now scratching my head trying to digest Greg Maxey's instructions on how to change the text of the button and add a custom icon, but that's a different story...).

    In the meantime I have a further question relating to the second part of my original post, re userforms. I'm not sure if I should start a separate thread for this or not, but as it was part of my original question I'll continue here and I'm sure someone will tell me if I need to move it somewhere else:

    I have setup a userform in my custom template from which the user can select one of a number of options, the options being to insert various building blocks. The form itself seems to be working correctly: it looks the way I want, offers the options I want, has a working cancel button (which just closes the form), and pastes the correct building block when I press OK. I have set (or tried to set) the template up so that when a new document is created from it, it automatically displays my userform. I have done this very simply by putting the following code into the ThisDocument object of my custom template (I hope I am using the right terminology to explain this):

    Private Sub Document_New()
    
    
      frmMyForm.Show
    
    
    End Sub
    The code to actually insert the chosen building block, which I have entered by going to the "View Code" option on my OK button is as follows:


    
    Private Sub cmdOK_Click()
    
    
    Application.ScreenUpdating = False
    If optLetter.Value = True Then
        Me.Hide
        Application.Templates(ThisDocument.FullName).BuildingBlockEntries("1 - Letter").Insert Where:=Selection.Range, _
            RichText:=True
        Else
    If optFax.Value = True Then
        Me.Hide
        Application.Templates(ThisDocument.FullName).BuildingBlockEntries("2 - Fax Cover").Insert Where:=Selection.Range, _
            RichText:=True
    Else
    MsgBox "Option Button has Not been Selected"
    End If
    End If
    Application.ScreenUpdating = True
    End Sub
    It was adapted from code proposed by Charles Kenyon in another post (unfortunately I cannot give you a link to it as I haven't posted enough messages to this forum yet).

    However, I have come up against one infuriating glitch, the solution to which I've no doubt is probably elementary to anyone who actually knows what they're doing. The problem I have is this:
    1. When I first open Word, I get a new blank document (Document1) created using the Normal template, as expected. Thanks to your add-in, the Ribbon shows the custom "Create Company Form" button
    2. When I click on "Create Company Form" button, it successfully creates a new document (Document2) using my custom template.
    3. As soon as Document2 shows up on screen, my userform is displayed.
    4. Whatever building block I select from the form is then successfully inserted into Document 2. So Far, so great.
    5. However, and this is where things start to go wrong, if I then click on the "Create Company Form" button again, whilst it does open another document (Document3), it then switches back to Document2 before displaying the userform, meaning whatever building block I select would be added to Document2 instead Document3. Document3 remains open in the background and I can switch back to it, but the form only shows over Document2.


    Search as I might, I cannot work out for the life of me why this is happening. After much trial and error I have noticed that the problem occurs regardless of whether I click "OK" or "Cancel" when my first userform appears on Document2, but if I close the form using the "X" at the top right corner of the form, it does not happen, i.e. if I click on "Create Company Form" again after closing the userform that shows up over Document2 using the "X" on the form, it creates Document3 and then stays on it before opening the userform again, allowing me to correctly insert my desired building block into Document3. I surmise from this that there is probably a key piece of code missing from the code associated with my "cancel" and "OK" buttons that somehow makes sure that the newly created document into which the building block is pasted becomes the "active" document (again, not sure if I am using the right nomenclature, but hopefully I'm making myself clear), rather than the document which was "active" before the "Create Company Form" button was pressed.

    I'm sorry if the above explanation seems very complicated but hopefully you've been able to follow me. FYI, my custom template, which contains both the form and the building blocks, is stored in the Microsoft/Templates folder.

    Any help or advice on this would be hugely appreciated.
    Many thanks.

  5. #5
    Without the template it is difficult to imagine what is going on, but it seems probable that it relates to the fact that you haven't unloaded the form after use. I prefer to call a new form from the macro that calls the form, so in your template, you need an ordinary module with the code below (which assumes your userform is called frmMyForm and it has two option buttons optLetter and optFax and that it has two command buttons btnCancel and btnOK.

    Option Explicit
    
    Sub RunProcess()
    Dim oFrm As New frmMyForm
        With oFrm
            .OptLetter.Value = True    'set the initial value
            .Show
            If .Tag = 0 Then GoTo lbl_Exit    'Cancel clicked
            Application.ScreenUpdating = False
            If .OptLetter.Value = True Then
                Application.Templates(ThisDocument.FullName).BuildingBlockEntries("1 - Letter").Insert Where:=Selection.Range, _
                 RichText:=True
            ElseIf .OptFax.Value = True Then
                Application.Templates(ThisDocument.FullName).BuildingBlockEntries("2 - Fax Cover").Insert Where:=Selection.Range, _
                 RichText:=True
            End If
            Application.ScreenUpdating = True
        End With
        Unload oFrm
    lbl_Exit:
        Set oFrm = Nothing
        Exit Sub
    End Sub
    Your Private Sub Document_New() macro should call the above Macro e.g.

    Option Explicit
    
    Private Sub Document_New()
        ModMain.RunProcess
    End Sub
    and the userform code should be
    Option Explicit
    
    Private Sub btnCancel_Click()
        Hide
        Tag = 0
    End Sub
    
    Private Sub btnOK_Click()
        Hide
        Tag = 1
    End Sub
    which leaves the ribbon captions, for which you will need the CustomUI editor. Close Word and open the add-in template with the ribbon in the editor. You will see two sets of code
    The second is a sub set of the first and both will need to be changed. The labels are shown below

    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon>
    <tabs>
    <tab idMso="TabHome">
    <group id="CompanyGroup001" insertBeforeMso="GroupClipboard" label="Document">
    <button id="CompanyTemplateBtn001" imageMso="FileNew" size="large" label="Create Company Form" onAction="CompanyTemplateBtn"/>
    </group>
    </tab>
    </tabs>
    </ribbon>


    <backstage>
    <tab idMso="TabOfficeCompany" visible="false" />
    <tab id="tabCompanyTemplateNew" insertAfterMso="TabInfo" label="Company Form" columnWidthPercent="25">
    <firstColumn>
    <group id="CompanyGroup000" helperText="

    Create a new blank document based on the Company Form" >
    <primaryItem>
    <button id="CompanyTemplateBtn000" label="Company Form" imageMso="FileNewBlankDocument" isDefinitive="true" onAction="CompanyTemplateBtn" />
    </primaryItem>
    </group>
    </firstColumn>
    </tab>
    </backstage>
    </customUI>


    and

    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon>
    <tabs>
    <tab idMso="TabHome">
    <group id="CompanyGroup001" insertBeforeMso="GroupClipboard" label="Document">
    <button id="CompanyTemplateBtn001" imageMso="FileNew" size="large" label="Create Company Form" onAction="CompanyTemplateBtn"/>
    </group>
    </tab>
    </tabs>
    </ribbon>
    </customUI>
    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
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    @Graham --

    No biggie, but I get an Add in error using Win10, Office 360 32-bit

    Capture.JPG

    The closest I could find in

    https://www.microsoft.com/en-us/down....aspx?id=50745


    was TabOfficeStart
    Last edited by Paul_Hossler; 02-01-2018 at 08:47 AM. Reason: Add link
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Regular ShogunPatch's Avatar
    Joined
    Jan 2018
    Location
    London
    Posts
    14
    Location
    Graham
    You, sir, are a gentleman and a scholar.
    A gentleman for taking the time and the trouble to read and consider my plea for help. A scholar for producing a solution which works faultlessly and explaining it so clearly and simply that even a newbie like me could apply it.
    I followed your solution to the letter and it worked, brilliantly and exactly as requested.
    Thank you.
    I've even managed to change the custom ribbon, with the exception that I haven't managed to get it to use my custom icon. Do you know whether that needs to be stored somewhere special?

  8. #8
    Using the Custom UI editor insert the icon in both modules, then in the code the icon is placed with

    imageMso="FileNewBlankDocument"
    replace this with

    image="NewIconName"
    where NewIconName is the name in the list on the left of the XML code. (imageMSO is used for built-in images - see http://soltechs.net/CustomUI/imageMso01.asp)
    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
    Quote Originally Posted by Paul_Hossler View Post
    @Graham --

    No biggie, but I get an Add in error using Win10, Office 360 32-bit
    Paul - that line shouldn't be there and can be deleted from the ribbon XML. Oddly enough it is ignored here
    <tab idMso="TabOfficeCompany" visible="false" />
    Incidentally the forum crashed when I tried to add code tags to the XML sequences, so I left them out when I retyped my reply. Leaving them out enables formatting to be used for emphasis. If you put the formatting inside the code tags, it adds formatting tags to the code, which is not what is required. This is an unfortunate limitation of the forum format, but as I posted the add-in c/w code, I thought the lack of code tags of little consequence.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  10. #10
    VBAX Regular ShogunPatch's Avatar
    Joined
    Jan 2018
    Location
    London
    Posts
    14
    Location
    Brilliant. I had got myself 99% of the way there, but didn't realise that "imageMSO" is just used for built-in images. I've changed each of those references to "image" and it works perfectly so that now my ribbon is custom branded with a NewDoc icon that clearly shows our company logo and so is obviously different from the standard one.
    Once again, thank you so much.

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Paul - that line shouldn't be there and can be deleted from the ribbon XML. Oddly enough it is ignored here

    Graham --

    Do you have 'Show Errors' checked?

    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  12. #12
    Quote Originally Posted by Paul_Hossler View Post
    Graham --
    Do you have 'Show Errors' checked?
    Paul
    Ah! That would do it - thanks for the heads up.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

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