+ Reply to Thread
Results 1 to 23 of 23

Automatically enable macros and display a userform when a new document is created in Word

  1. #1
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Automatically enable macros and display a userform when a new document is created in Word

    Hi all
    Following on from my last post, I have another issue relating to the same project I'm working on.
    I want to automatically [run a macro to] display a userform ("CallUF_TemplateOptions") whenever a new document is created from my normal.dotm template.

    To this end, following instructions I saw online I put the following code into the ThisDocument section of my Normal.dotm but when I create a new document nothing happens.

    Please Login or Register  to view this content.

    I'm wondering whether perhaps macros are not enabled when a new document is created from my Normal.dotm as when I go to look at the available macros in the developer tab, none are listed. I thought macros were automatically enabled in any new document created from Normal.dtom. Is that not the case? Or is it simply that my approach / code is wrong?

    For reasons I won't bore you with (unless it becomes relevant), the userform is located in a custom macro-enabled Building Blocks template.

    As ever, any pointers would be much appreciated.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Automatically enable macros and display a userform when a new document is created in W

    Use:
    Please Login or Register  to view this content.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    Thanks Paul. Sorry for not acknowledging your suggestion sooner; for some reason I didn't get an email telling me you'd responded.
    I've tried your suggesting but it is not working properly. I've just spent half an hour drafting quite a long post in response, explaining what I've done so far, etc, but I can't seem to post it as I'm getting an 'access denied - Sucuri Firewall' error for some reason... No idea why, what it means or how to get around it.
    Thanks anyway.

    Patch

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Automatically enable macros and display a userform when a new document is created in W

    Provided your userform is named 'CallUF_TemplateOptions' and you've added the macro and userform to the relevant template, I can see no reason it wouldn't work. For my part, I wouldn't put either in the Normal template, however; I'd use a different template.

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Automatically enable macros and display a userform when a new document is created in W

    Now cross-posted at: http://www.msofficeforums.com/word-v...t-created.html
    Please read Excel Forum's Cross-Posting policy in rule 8: http://www.excelforum.com/forum-rule...rum-rules.html

  6. #6
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    Quote Originally Posted by macropod View Post
    Now cross-posted at: http://www.msofficeforums.com/word-v...t-created.html
    Please read Excel Forum's Cross-Posting policy in rule 8: http://www.excelforum.com/forum-rule...rum-rules.html
    Oops... I hadn't read or understood the rules properly and assumed that I wasn't supposed to multi-post in other parts of this forum; I hadn't realised I wasn't allowed to try asking for help in other, completely separate forums. Clearly my bad, so apologies for that. However, the reason I tried posting elsewhere is that whilst I have had a number of responses, I wouldn't say my query has been answered as I am still having problems and for some reason, as mentioned in my post #3, I'm struggling to post comprehensively on this forum, i.e. my comprehensive reply to your post #2 (which tried to include some of my code) keeps being blocked by some firewall of some sort. Nevertheless, as my other thread as been closed down and referred back here, I'll try and persist:

    Just to verify I'm not being stupid, my userforms and the 'CallUF_TemplateOptions' macro are not located in my Normal.dotm but in my custom building block template ("MyBuildingBlocks"), but the Private Sub Document_New() code you've suggested should go into the ThisDocument object of the Microsoft Word Objects of Normal, right? If so, when I try that I get a "Run-time error: 424 Object required error".

    Also, in answer to Slaycock's response on my other (now closed) thread:
    • When I first launch Word, if I cancel the userform which comes up the blank document I am left with is based on normal.dotm
    • If I open word and run through the userforms, the document that results is also based on normal.dotm
    • Finally, if I then create a new, second, document that document is also based on normal.dotm

    Thanks for your continuing interest.

  7. #7
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    I hope I'm not breaking any more rules, but since my cross-post at http://www.msofficeforums.com/word-v...t-created.html has been closed, I thought I would re-post my original query as worded there, here, since it explains things a bit more clearly than my original post above.

    I have developed a custom macro-enabled building blocks template ("MyBuildingBlocks") which, in addition to various standard document components, includes a number of userforms which are designed to guide the user to insert the correct building block to start his document with;
    • The primary form ("frmTemplateOptions") asks users to select a category of document using radio buttons. Then depending on the option chosen;
    • The correct one of a choice of secondary userforms ("frmOptionA", "frmOptionB", "frmOptionC", etc) is called up asking users to select a specific document from within the selected type, again using buttons;
    • Finally, the relevant building block is inserted according to the selection made.

    Referring to various articles online, I have created a macro ("CallUF_TemplateOptions") within a module ("modTemplateOptions") in MyBuildingBlocks with which I can successfully call up the primary userform. This then correctly takes me through my selection options and inserts the required building block.

    If I cstart Word and manually call up the primary userform using this macro, everything seems to work fine. My problem is that I want the primary userform to flash up automatically every time I create a new document (i.e. open word for the first time as it is set to open a new doc automatically, or press Ctrl +N, click on the New Document icon on the QAT, etc). I do not want it to come up if I open up an existing document (either when I chose file/open from within word, or if I right-click on the Word icon in the start bar and then choose an existing document).

    I've managed to get halfway there by adding some code to a public AutoExec sub in a second module ("modLaunchPrimaryForm"). This successfully calls up the form when I first open word, but:
    • If I create another new document from within Word it does not launch
    • If I open an existing document it launches and I have to cancel before the exisitng document opens up

    I suspect this is really simple if you know what you're doing - which I clearly don't - so any pointers to help me get on the right track would be much appreciated.

  8. #8
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Automatically enable macros and display a userform when a new document is created in W

    Earlier in this thread you said
    Quote Originally Posted by ShogunPatch View Post
    I want to automatically [run a macro to] display a userform ("CallUF_TemplateOptions") whenever a new document is created from my normal.dotm template.
    That clearly implied your userform's name is 'CallUF_TemplateOptions'. Accordingly, I provided code for that. From your post at http://www.msofficeforums.com/word-v...tml#post119932, however, it seems that isn't the userform's name at all; it's apparently 'frmTemplateOptions'. Quite Obviously, you can't expect the right results when you provide the wrong information; it should also be perfectly obvious what you need to change to get the code I posted working - provided the userform is in the same template the macro is in; otherwise you'll need to use the Application.Run Method to access that template's code (see the Application.Run Method in the VBA help file).

  9. #9
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    You're right Paul, it seems I have confused things somewhat, but only because I am at the very edge of my own understanding. Apologies (again) if you feel you've wasted your time as a result.

    My initial form is, I'm pretty sure, called 'frmTemplateOptions'. It is located in my custom building blocks template under Forms. The routine 'CallUF_TemplateOptions' is a piece of code I pulled together from various forum posts and is what I think physically displays the form. It is in a module, also in my custom building blocks template, called 'modTemplateOptions'. I would happily share it with you, but when I try to my post is being blocked by some firewall.

    'CallUF_TemplateOptions' works, albeit on it's own I have to manually run the routine once Word is open in order to get the form to display.

    In an attempt to get the form to display automatically when Word opens I added some additional code, also cobbled together from various replies to other people's problems, in a second module called 'modCallPrimaryForm' (also located in my custom building blocks template). Again, happy to share it with you in only I could.

    As I've said previously, this sort of does the trick in that it calls the primary form up when Word first opens into a new document and works fine. However, it also calls it up if I open word directly into an existing document, which I don't want it to, and it does not call it up if I create a new document from within an open instance of Word.
    Attached Images Attached Images

  10. #10
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    I'll try posting the code for 'CallUF_TemplateOptions' on its own:

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    OK that worked... Having just tried again it seems it is the code in the module 'modCallPrimaryForm' which is causing the firewall error. I'll try posting a screen capture of it instead:

    Code2.PNG

    On a separate note, can someone please tell me how I set this thing to email me if I get any replies? Other forums I'm on seem to do this automatically but I'm not getting any notifications about this and keep having to check periodically if anyone has added anything.

    Many thanks.

  12. #12
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Automatically enable macros and display a userform when a new document is created in W

    Instead of storing your userform in a custom building block, you should have created a new template and stored both the userform and any boilerplate content in that. Your approach can only create problems, especially if you try to distribute the document/template for anyone else to use. As it is, your Document_New macro would have to open your custom building blocks template then use the Application.Run Method to access that template's CallUF_TemplateOptions code.

    It's also beyond my why you're creating class events, etc. when all you apparently need to do is load a userform.

    As for the forum notifications, open your User CP (https://www.excelforum.com/profile.php?do=editoptions) and check the settings. You should at least have the option for 'Receive Email from Administrators' checked and the 'Default Thread Subscription Mode:' option set to 'Daily, using email'; preferably 'Instantly, using email'.
    Last edited by macropod; 09-28-2017 at 06:30 PM.

  13. #13
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    Quote Originally Posted by macropod View Post
    Instead of storing your userform in a custom building block, you should have created a new template and stored both the userform and any boilerplate content in that. Your approach can only create problems, especially if you try to distribute the document/template for anyone else to use. As it is, your Document_New macro would have to open your custom building blocks template then use the Application.Run Method to access that template's CallUF_TemplateOptions code.

    It's also beyond my why you're creating class events, etc. when all you apparently need to do is load a userform.

    As for the forum notifications, open your User CP (https://www.excelforum.com/profile.php?do=editoptions) and check the settings. You should at least have the option for 'Receive Email from Administrators' checked and the 'Default Thread Subscription Mode:' option set to 'Daily, using email'; preferably 'Instantly, using email'.
    Dear oh dear, this conversation doesn't seem to be going very well, is it?...

    You obviously think I am being completely thick. Perhaps I am, but as far as I am concerned, I have created a new template and both my userforms and boiler plate text, etc, are stored in it. It just so happens that I've set the template up as a building block template, precisely so I can distribute it to the other people in my company, on whose computers it is intended to be stored in Microsoft's purpose designed Building Blocks folder. This may be misguided, even stupid, but I opted for this route following the advice given in this thread on the Microsoft site: https://answers.microsoft.com/en-us/...4-6d38ce6bc279
    Since I have read quite a lot of posts by Charles Kenyon, Greg Maxey and Suzanne Barnhill and they all clearly know a lot more about Word and VBA than I do (admittedly, that's not saying much), it seemed reasonable to try and follow some of their advice.

    As for creating unnecessary class events, I did clearly state in Post #9 that I am at the very limit of my own understanding and that my code is often largely cobbled together (perhaps I should have said lifted) from other people. That particular code was borrowed from another forum post about using userforms, though I couldn't tell you which one. When I copy and insert code I often don't fully understand what it all does, though I try to. I generally delete any bits in it that are clearly redundant to my needs, but otherwise I leave things well alone if they appear to be working. Hence, I did clear some lines out, but not knowing what it all did I also left much of it in.

    Now, I appreciate any help you or anyone else is generous enough to be willing to give me. I really do. But I have got the message already that you think I'm very, very stupid and I'm sure anyone else who may have bothered to read this thread will have got it too, so perhaps instead of berating me at every possible opportunity you could actually try and contribute something useful, like the suggestion in your very first reply (only with a bit more guidance on how to actually implement it: where to put it, etc), or indeed your clear instructions on how to get emails notifications of any reply (thanks for that by the way). That way, I might actually be able to work my way through to a solution to my problem, which I'm assuming is what all these help forums I'm not allowed to cross-post on are about, no?

  14. #14
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Automatically enable macros and display a userform when a new document is created in W

    1. Nowhere is it stated that you should add a new template to Word's Building Blocks folder; all the advice will be that you should add it to either the user's templates folder or the workgroup templates folder, depending on whether it's for personal use or workgroup use. The link you gave on this has nothing to do with creating templates; it merely concerns the creation of a building block - which nothing in this thread indicates you're either doing or need to do!

    2. Instead of blundering along adding templates to Word's Building Blocks folder and creating unnecessary class events (userforms have no need of them), you should spend some time learning how Word works. Posting in forums such as this with a clear statement of what you're trying to achieve (i.e. not saying how you want to do something) is a good way of avoiding such blunders. Have a look at my attachments in http://www.msofficeforums.com/word-v...tml#post120013. Both have userforms that open when the document is opened and neither has any need for Word's Building Blocks folder or a class event. Simply changing the names of the Document_Open macro in either of them to Document_New and saving the document as a template (even if it's saved to your 'Documents' folder) is all that would be required to have the userforms run automatically anytime a new document is created from said template.

    3. If your documents need to use building blocks for adding content, the correct procedure is to: first create a template; then add the buildings blocks, userform, & other macro code to that template. You should not be adding anything to Word's Normal template or built-in building blocks template for any of this.
    Last edited by macropod; 09-29-2017 at 07:50 PM.

  15. #15
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    1. Perhaps you're right, the link I gave didn't specifically tell me to add my custom Buidling Block template to Word's Building Block folder - I got that steer from here: http://www.techrepublic.com/blog/win...s-with-anyone/. As for nothing in my thread indicating that I am trying to create (and work with) building blocks, I should have thought that was made fairly clear in Post #7 (though I acknowledge that perhaps it would have been helpful if everything post #7 had been included right up front in my original query).

    2. I agree that taking the time to sit down and learn Word properly would be an excellent first step. When I'm retired and have more time on my hands than I know what to do with I will no doubt do just that but in the meantime, like most people I suspect, I have to focus on trying to learn the specific bits I need to address my given problems and that sadly leads to a certain amount of blundering (I mean really, was that really necessary?) and trial and error (AKA putting in bits of code I might not need). Your attachment is very simple and elegant. However, I think I am right that it does not quite do what I want because to work it relies on the user creating a new document from that template. What I need is for this to happen by default every time a new document is created, without my users having to manually select a specific template.

    3. Yes my document needs to use building blocks. Hence I have created a new custom building block template and I have added my required building blocks and macros to that custom template. As per the techrepublic article I, had saved that to Word's building block folder (though as explained below I am currently trying a new approach which is to save it to my Word Startup folder instead).

    Working my way through your seemingly endless belittling and trying to make use of the odd occasional useful pointer, I have now made some significant changes to my setup which is currently as follows:
    • I have customised my Normal.dotm to include the various fonts and styles that I want. You may recoil in horror at this, but it seems to me the easiest way to make sure all new documents by default include our house styles. It also seems to be recommended here https://support.office.com/en-gb/art...7-ccb5166f98ea.
    • As stated above I have now tried moving my custom building block template to the Word Startup folder. This is in an effort to ensure it loads up automatically when Word opens without me having to put in a a specific command to pre-load building blocks (as I had in one of my earlier attempts)
    • My custom building block template includes not only my custom building blocks, but the macros that automatically paste them into a document and the userforms designed to allow the user to select the block that they want. Just to be clear, my custom building block template includes both blocks which might be inserted as individual components of a document and blocks which are entire document structures. What I am trying to do with my userforms is insert the latter, i.e. if someone needs to create a proposal then the form inserts a bare-bones proposal structure which can then be tailored to the specific job in hand.

    The ThisDocument Microsoft Word Object of my Normal.dotm now includes the following code (apologies for including it as an image, but if I try to include it as code I run into the Sucuri firewall):
    Code3.PNG

    I don't really understand what the AutoExec sub is doing (specifically why I seem to manually need to add a new document) but without it my Document_New sub doesn't seem to work; with it, it does and my primary form flashes up and everything works as intended when Word first starts up.
    My (I think/hope) last remaining issue is that if, having successfully selected and inserted a building block to (say) add a proposal structure, I then need to create a second document to create a fax cover, when I press the new document icon I momentarilly see a new document flash up but then Word skips back to the existing document (in this example the proposal structure) before again flashing up the primary userform and when I then select the fax cover building block that I want, it just adds it to the end of that document instead of adding it to Document2 (which is there, but empty). I've tried adding SendKeys "%{TAB}" to my Document_New sub to force it to flip back to the new document, but it seems to just double flip back to the original document.

    Any thoughts on how to get around this so that word stays on the new doc (Document2, Document3 or whatever it may be) before flashing up the primary user form and then pastes the new selection into the new document would be much appreciated.
    Then, with a bit of luck, I'll be done.

  16. #16
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Automatically enable macros and display a userform when a new document is created in W

    Quote Originally Posted by ShogunPatch View Post
    I agree that taking the time to sit down and learn Word properly would be an excellent first step.
    Indeed it would be - and don't wait till you retire! Your productivity will increase by far more than the time it takes to learn the basics.
    Quote Originally Posted by ShogunPatch View Post
    Your attachment is very simple and elegant. However, I think I am right that it does not quite do what I want because to work it relies on the user creating a new document from that template. What I need is for this to happen by default every time a new document is created, without my users having to manually select a specific template.
    I'd hate to see that happening for every document I create but, if that's what you want, simply make your template a global template. See: http://www.addbalance.com/usersguide...tes.htm#Global

    Quote Originally Posted by ShogunPatch View Post
    Yes my document needs to use building blocks. Hence I have created a new custom building block template and I have added my required building blocks and macros to that custom template.
    Saving that template to the Word Startup folder, which you now seem to have done, makes it global. Your useform should be in that same template.
    Quote Originally Posted by ShogunPatch View Post
    The ThisDocument Microsoft Word Object of my Normal.dotm now includes the following code (apologies for including it as an image, but if I try to include it as code I run into the Sucuri firewall):
    Attachment 540794

    I don't really understand what the AutoExec sub is doing (specifically why I seem to manually need to add a new document) but without it my Document_New sub doesn't seem to work; with it, it does and my primary form flashes up and everything works as intended when Word first starts up.
    If your macros, etc. are now all in the global template, you should be able to delete the AutoExec macro and simply use the Document_New macro:
    Please Login or Register  to view this content.
    Either way, you shouldn't need the 'Set App = Word.Application' line.
    Quote Originally Posted by ShogunPatch View Post
    My (I think/hope) last remaining issue is that if, having successfully selected and inserted a building block to (say) add a proposal structure, I then need to create a second document to create a fax cover, when I press the new document icon I momentarilly see a new document flash up but then Word skips back to the existing document (in this example the proposal structure) before again flashing up the primary userform and when I then select the fax cover building block that I want, it just adds it to the end of that document instead of adding it to Document2 (which is there, but empty).
    You might get better results by making your userform modeless (i.e. set its ShowModal property to False). Also, when you create the Fax document, you should set a reference to it. For example:
    Please Login or Register  to view this content.
    Then, when you want to work with the Fax, address it via the DocFax reference.
    Last edited by macropod; 10-03-2017 at 05:39 PM. Reason: correct QUOTE

  17. #17
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    I'd hate to see that happening for every document I create
    Usually, I'd agree but the fact here is that for the people who are going to get this template, 95% of the time they go into Word it will be to either (a) work on an existing document (hence I don't want the primary form coming up when Word starts up into an existing document or they manually open up an existing document) or (b) to create one of the document types accessible through the userforms. For the rare occasions when that is not the case, I have both a 'Cancel' button and a 'Blank Document' option on the form which will simply close it down leaving a blank document with our house styles.

    If your macros, etc. are now all in the global template, you should be able to delete the AutoExec macro and simply use the Document_New macro:
    Yes, pretty much everything else I've read on the topic suggests this also, but if I do delete it, my form doesn't then come up automatically when Word starts up into a new, blank document. On further playing around I've realised that with just the Document_New sub, when Word first starts up the primary form does not fire up and I end up with just a blank Document1. If I then click on New Document, a blank Document2 opens up and then my primary form shows up and I can select the document structure I want, which then pastes correctly into Document2. So far, so good. However, if I then click on New Document again, another blank document is created (Document3) which flashes up momentarilly but then Word immediately jumps back up to Document2 (which by now has whatever document structure I'd previously selected in it) before finally flashing up my primary form. If I then select another document structure it pastes it into the end of Document2, leaving Document3 there but in the background.

    So far, the only way I've found to get the primary form to show up when word first starts up is to include the AutoExec sub, though you're right it works without the 'Set App = Word.Application' line. The problem with that is that, if I try to start Word up into an existing document rather than a new blank document, it first goes into a new blank document and fires up the form and only after I've either selected a document structure or cancelled will it then open up and focus on the existing document I was trying to open up in the first place (leaving the newly created Document1 in the background).

    You might get better results by making your userform modeless (i.e. set its ShowModal property to False)
    I tried this and everything seems to happen as above with one critical difference which is that when I click New Document from within an open instance of Word, the primary form flashes up first/immediately and only then does a new document appear.

    So just to recap:
    With both the AutoExec and the Document_New subs in place, when Word first starts up into a blank document (Document1), the primary form displays and works correctly, but if I then click on New Document to create a second (temporarily blank) document, Document2 then:

    With ShowModal property as True the sequence of events is as follows:
    • Document2 flashes up momentarily
    • Word Jumps back to Document1
    • Word displays my primary form with Document1 in the background
    • Any document structure I select gets pasted into Document1
    • Document2 remains blank but I have to manually switch to it to see it

    With ShowModal property as False, the sequence of events is as follows:
    • Word displays my primary form with Document1 in the background
    • Word then immediately displays (creates?) Document2, hiding the primary form and stays on that.
    • If I manually switch back over to Document1, which has the Primary form over it and select any document structure it again gets pasted into Document1, leaving Document2 Blank.

    This is all hugely frustrating. I feel as though I am close, and yet so far...

  18. #18
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Automatically enable macros and display a userform when a new document is created in W

    As I said in my previous post, instantiating your Fax document should be done via code like:
    Please Login or Register  to view this content.
    and you should then address it via the DocFax reference; I suspect all you're ever addressing documents by is ActiveDocument or Selection, neither of which provides the necessary discrimination. If the fax document disappears, you might try making the next line:
    DocFax.Activate

  19. #19
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    Quote Originally Posted by macropod View Post
    As I said in my previous post, instantiating your Fax document should be done via code like:
    Please Login or Register  to view this content.
    and you should then address it via the DocFax reference; I suspect all you're ever addressing documents by is ActiveDocument or Selection, neither of which provides the necessary discrimination. If the fax document disappears, you might try making the next line:
    DocFax.Activate
    Thanks for persisting with me on this. I've attempted to implement your suggestion but it does not work; that may be because I have not implemented it correctly - probably because I don't understand the logic: the idea of identifying a document as whatever type of document it is makes sense to me and I can see how that could be useful in all sorts of ways; but when Word first starts up, or indeed a new document is created, the macro cannot know what kind of document the user intends to create. This implies to me that your code would need to be added to the individual macros that insert each specific type of document structure, no? But I don't then see how that would solve my problem, which is that:
    • If ShowModal is True then Word switches momentarily to (creates?) the new document but then switches back to the previous document before displaying the Primary Form
    • If ShowModal is False then word shows the primary form (over the existing document), then switches to a new blank document (in so doing hiding the form)
    Either way, I end up with the primary form over, and therefore set to insert the next building block into the initial document, not the new one. My problem therefore seems to be two-fold:
    • how to identify each newly created document (which may be Document2, Document3, Document4, etc); and
    • how then to make sure the focus is on the newly created document when the form actually displays.

    I came across this post https://wordmvp.com/FAQs/MacrosVBA/PseudoAutoMacros.htm which looked to me like it might offer a solution, but when I tried the code I got a 'Compile error in hidden code', I think because I'm using Word 2016 whereas that solution is coded for Word 2007.

  20. #20
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Automatically enable macros and display a userform when a new document is created in W

    Without seeing your template & code, it's impossible to be more specific about what you should be doing. If you still can't attach the template with the updated code, perhaps you could add it to a zip archive and upload that.

  21. #21
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    I think I've pretty much shared all of my code already (there really isn't that much of it!) - if not as code then as a an image because of the firewall issue.
    Unfortunately I don't think I can upload the whole template itself because that'll breach our internal rules (the BB template includes all sort of proprietary info). I think what'll have to try and do is create a copy of my BB template, then delete/replace all the blocks themselves with bits of sample text, and then I can try and upload that as an attachment; would that work?
    I'll need a little bit of time to sort it all out but will post it ASAP.
    Thanks.

  22. #22
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Automatically enable macros and display a userform when a new document is created in W

    In this thread, at least, you've only ever provided a few lines of code; nothing that includes a userform or code for your fax, for example. A stripped-down copy of your template that contains enough to maintain its functionality would be fine.

  23. #23
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Automatically enable macros and display a userform when a new document is created in W

    I've finally been able to prepare a zip file with my normal.dotm and a sanitised MyBuildingBlocks.dotm templates. Apologies for the delay in uploading this but some way through the process of sanitising my building blocks something got corrupted and Word kept crashing. It took a bit of time to sort, which I've now managed to do so here it is.
    As you'll see it is all pretty simple and lightweight; hopefully you will spot something very obvious and easy to fix.
    Many thanks.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. In a userform I would like to display a word document.
    By joebbb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-26-2017, 05:34 PM
  2. Ask enable or disable macros when opening a document.
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-22-2016, 02:52 AM
  3. Keyboard Shortcut To Trust Document/Enable Macros
    By liquidmettle in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-24-2015, 05:41 PM
  4. Making a workbook a trusted document- Always. Always enable macros
    By emilyloz in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 07-03-2013, 01:52 PM
  5. [SOLVED] Enable Macros when workbook is created
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-17-2011, 10:26 AM
  6. Automatically enable self-created macros
    By jonlemur in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-16-2009, 02:19 PM
  7. Word document created is not showing up in history.
    By blaze-one in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 11-02-2005, 11:40 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1