Consulting

Results 1 to 10 of 10

Thread: Solved: Saving Word Doc from Template

  1. #1
    VBAX Regular
    Joined
    Nov 2005
    Posts
    39
    Location

    Solved: Saving Word Doc from Template

    I have been playing with VBA in Excel for about a year now and finally have found a reason to venture into Word with VBA. But I am quickly hitting a wall.

    I have a word template (.dot) that is a basic document that will be updated. There will be some VBA code with the template that will limit the actions that the user can do. However, when the file is saved under a new name, I only want to have the completed template and not the VBA code that is saved with the template.

    Can anyone help me with this?

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    You have two choices.

    1. Have the code that you need in another template - a global template perhaps. That way the template that is cloned into a new document, and that document saved, will not have the code.

    2. Write a VBA procedure - in a global template - that will take the active document (the one cloned from the template) and strip out the code before saving the file.

    #2 is possible, but would take more work/development.
    There will be some VBA code with the template that will limit the actions that the user can do.
    Care to elaborate on that? What is being limited, and why?

  3. #3
    VBAX Regular
    Joined
    Nov 2005
    Posts
    39
    Location
    The template will be read only and protected so that the user will only be able to update the fields on the document. I also want to limiting the toolbars and buttons that are shown. I will probably be creating a new tool bar that has a New, Save, Close, Spell Check, Print Preview, and Print buttons.

    New = Creates a blank tempate

    Save = Save the template as Word Doc. (I am still working on how much freedom I am going to give on the location and name of the saved document)

    Close = Close the template (prompt to save if needed).

    Spell Check = Obviously spell check the document (I realize that I will have to temporarly remove the protection from the file.)

    Print Preview = Obviouse enough (I am questioning the need for this though)

    Print = Obviouse enough

    As for Why, I am creating this for a single user who I am confident the easier I make it the better. The project is as much a toy for me to play with and learn something new as it is for the user to use the process that I am creating. She is not very computer savvy and will appreciate a simple, clean look that only have the options of what she will want to do.

  4. #4
    VBAX Regular
    Joined
    Nov 2005
    Posts
    39
    Location
    I think option 1 will be best.

    Throwing caution to the wind about sounding a little on the dumb side..... How do I create a global template?

    I understand that I need to go into Tools/Templates and Addins... to add as a global template. But, am I to write the VBA with a blank document as the Global template and have the Global Template open the actual template that will be updated and saved?

    This seems more confusing than it should be, which tells me that I am not getting something yet.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    First off, I think you may want to really, really, think of about the restrictions you are contemplating. In principle, I am all that much for locking down things. It is difficult to do for most users, and impossible to do for those users who want to mess it up. There is just no way to really lock down a document. You can get close - with a lot of coding, and rewriting of Word commands - but a really good hacker can still get at it.

    With all due respect, if you are asking about how to create a global template then trying to lock down your documents may be a stretch. Not that having to stretch is bad thing.

    A global template is simply a template (.DOT) file that is loaded as global. This can be automatically done - in which case, save the .DOT in a Word Startup folder. This can be either the Program File\...\OFFICE startup folder, which will load it automatically for all users on that machine, or it can be in the Document and Settings\username\Application Data\..yadda yadda...\Word\Startup, which will load it automatically for THAT user.

    OR, a global template can be loaded "manually" via Tools > Templates and Addins. By manually, I also mean it CAN be loaded by code - as opposed to being loaded automatically on startup.

    Certainly, you can have a procedure in a global template that will use another (non-global) template to create a document.

    The major purpose of global templates, IMO, is to simply hold code. It makes code maintainence easier.

    Please clarify:
    open the actual template that will be updated and saved?
    Users should never be opening and updating templates. Templates are used to make documents that users work with. Which makes me wonder about:
    New = Creates a blank tempate

    Save = Save the template as Word Doc.
    properly used, a template makes a document. The users should NEVER be given the template itself to save as a document. The template makes a document that is a clone of itself.

    What you seem to be saying is that you are giving the users a original document, that may be saved differently. This is, generally, not a good idea.

  6. #6
    VBAX Regular
    Joined
    Nov 2005
    Posts
    39
    Location
    Thanks for the response. I certainly understand that locking down a document that someone wants to "hack" or dig in to make changes is difficult if not impossible. I am simply trying to limit what is on the screen and the options easily seen. Especially being that when a document has protection turned on, many options become unavailable and grayed out. I am simply looking to create a tool bar that only has the options as I listed.

    With all due respect, if you are asking about how to create a global template then trying to lock down your documents may be a stretch. Not that having to stretch is bad thing.
    It was not necessarily the global template I was having the issue with. It was the idea that the global template had the code and when I would open the new document (based on the global template) I was under the impression the code came with it. After playing around a little bit I realized this thought was incorrect. (This is why I marked the string as solved, I realize I should have explained with a post).

    As for being a stretch... Yes, the entire project is a stretch. I learned a valuable lesson early in life "Everything is difficult until you know how to do it." And trust me, I don't know enough about VBA. But the more I read and play the better I get. I am positive that if I ever posted complete code of what I have done on this site it would be torn to pieces. Not as being bad, but certainly many other more precise, clean, and efficient ways to complete the task. But I am learning and honestly try to limit the post to only the issues where I am stumped.


    Users should never be opening and updating templates. Templates are used to make documents that users work with.
    I agree, I did not explain that very well. The NEW button would create a new document (cloned from the template). The SAVE button would save the document.

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am positive that if I ever posted complete code of what I have done on this site it would be torn to pieces.
    Sorry you have that impression. As someone stated about me...I can be gruff, but really, I DO try to encourage people to push themselves and write good code. And I, for one, never intend to tear anyone's code to pieces.

    So....I am still trying to understand.

    You have a global template. Fine. It has code that uses a different template to clone a new document.

    WHY would that new document clone another document cloned from the same template?
    when I would open the new document (based on the global template) I was under the impression the code came with it.
    Any code in the new document would come from the template it was cloned from. The global code (if it IS global) would still be available, but not IN the new document.

    A cloned document contains the code from the template it is cloned from. Why do you need the global code?

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    And again, I am not trying to tear anything apart. I sincerely wish to help.

  9. #9
    VBAX Regular
    Joined
    Nov 2005
    Posts
    39
    Location
    Sorry you have that impression. As someone stated about me...I can be gruff, but really, I DO try to encourage people to push themselves and write good code. And I, for one, never intend to tear anyone's code to pieces.
    My code hasn't been torn apart. I was just stating that I sometime go about things the long way. Lets try this from the beginning...

    I have created a Global Template that has VBA Code in it. A document is cloned from this template and is opened in Word. Work is completed and a little VBA Code is ran and the user saves the Cloned Document as Test.doc.

    QUESTION: Will the Code from the Template be in Test.doc?
    ANSWER: Yes (Correct me if I am wrong)

    What I would like to accomplish is to have the code remain in the Template but when the document is saved (Test.doc) there is no VBA Code with it.

    In writing this out, I think I know how to accomplish this. I was reading through the KB's for Word and found one to "Remove All VBA From Document". Shouldn't that be accomplished if I run that just before the document is saved?

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    OK, to repeat - any document cloned from a template will have the code that is in that template. It IS possible to write a procedure from a different template (acting as a code holder) that will delete VBA code. This is not a particularly elegant solution.

    You actually had it correct a few posts back.

    1. Do NOT use the global template to make the new document. In other words, do NOT clone the global template.

    2. Make another template - with no code in it. Use code in the global template to clone that template to make the new document.

    3. Use the global template to run your other code. As the code will be run from there - but the document cloned from a different template that has no code - then the saved document will....have no code. Or it won't if the the template that made it has no code. There may be as much, or as little, as you want.

Posting Permissions

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