Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 24

Thread: Two questions re Template Password

  1. #1
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location

    Two questions re Template Password

    Two questions re passwords:

    1) I have used the following macro in several Word templates to automatically lock a template when it is closed. I have been opening several templates at once, making updates, then "File, Save All" to close them all again.

    I am noticing that sometimes when I open the templates, they are not all locked. Does anyone have any idea why this is happening or how I would ensure that all templates are being locked? Is it something to do with "File, Save All"?

    [vba]Private Sub Document_Close()

    ' Apply password for closing a template file
    ElseIf ActiveDocument.Type = wdTypeTemplate Then
    ' Add specific password to prevent further edits without approval
    If ActiveDocument.ProtectionType = wdNoProtection Then
    ActiveDocument.Protect Password:="doccenter", _
    Type:=wdAllowOnlyFormFields, NoReset:=True
    End If
    End If

    End Sub
    [/vba]

    2) Other than disabling the above code, is it possible create a macro that will automatically apply the correct password (since I know what the password is) so I can make updates to some templates without having to enter the password each time for each one? I am updating 47 templates at a time!

  2. #2
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    What about something like this? This macro is not stored in the templates, but loops through all open documents applying the protection to templates then saving and closing them. With the test for protection, I've reversed this so it applies the protection even if some other protection type is currently active (rather than just testing for no protection at all).

    [vba]
    Sub CloseAllTemplates()
    Dim objDoc As Document


    For Each objDoc In Application.Documents
    If objDoc.Type = wdTypeTemplate Then

    ' Add specific password to prevent further edits without approval
    If objDoc.ProtectionType <> wdAllowOnlyFormFields Then
    objDoc.Protect Password:="doccenter", _
    Type:=wdAllowOnlyFormFields, NoReset:=True
    End If

    objDoc.Close True
    End If
    Next objDoc
    End Sub
    [/vba]

  3. #3
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    I have to lock the templates individually since the users are not allowed to unlock them to make edits without approval (using the password). But if they open several at a time, they might end up with an unlocked one, which would not be good.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Something not right here.

    1. Users should NEVER be opening templates. Period. This sort of breaks the point of having a template.

    2. "I have to lock the templates individually since the users are not allowed to unlock them" Not following this. Your question was how to lock all of them. So...why do you need to do it individually?

    Your code BTW uses Document_Close. This procedure only fires on the document that contains it. Geekgirlau's code does it on ALL open documents.

    47 templates???? This does not sound like real templates to me.

  5. #5
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    Okay, here's the scoop--

    1) I totally agree, but in some instances I have no control over where the templates are stored and what access users have to the files.

    2) I'm creating 47 templates (these are actually templates for letters--that's why there's 47 of them) that have a password to ensure that no one goes in and makes any changes (even if they somehow manage to finagle Full access to the folder the templates will be stored in). I've been making updates to all of them, so I've been opening about 20 at a time and noticed that one or two of them are not locked when I open them all together--even though my "Document_Close" macro is supposed to lock them. So, I'm wondering if when I close them as a group, some are not running through that macro correctly. If I make my updates and close them one at a time, they appear to all lock correctly. My concern is that if I do "Save All" instead of just "Save" one at a time, and some don't lock, then a user could potentially mess with that template. I'd like to be sure that they can't mess with any of them.

    3) I was also asking about applying the password by macro so I wouldn't have to enter it for each of the 47 templates when I want to make updates to all of them--just wondering if it's possible to shortcut through that somehow.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    3) I was also asking about applying the password by macro so I wouldn't have to enter it for each of the 47 templates when I want to make updates to all of them--just wondering if it's possible to shortcut through that somehow.
    geelgirlau's code does precisely this.

    I am still wondering about the 47 templates. Sorry, but Ihave worked in very complex business situations, and Ihave never found a need for that manny templates. But again, if a user is opening a template file...then you are not using templates properly.

    Templates create new documents. That is what they do. That is the ONLY thing they should be doing.

    IMO, the actual letter produced can be dynamic. One or two templates (REAL templates), that create the required document (letter).

    I have ONE template, ONE, that creates:

    a financial reporting document between managers
    a financial cost recovery letter between managers
    a training request costing letter from a manager to staff
    etc. etc.

    From ONE template file that the users NEVER, EVER, open.

    I am not being critical. However, opening 20 "templates" and making changes seems asking for trouble.

    The storage location of templates is irrelevant. They can be locked away in a folder completely unaccessible to the users. As long as the user has READ rights, they will work.

    The users should only "open" - ie. start a new document - a template using File > New. This can of course be set us as a menu item, or a icon, or whatever you choose.

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Gerry....I'm interested in how this works. could you explain please?
    IMO, the actual letter produced can be dynamic. One or two templates (REAL templates), that create the required document (letter).

    I have ONE template, ONE, that creates:

    a financial reporting document between managers
    a financial cost recovery letter between managers
    a training request costing letter from a manager to staff
    etc. etc.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    The template has each letter structure IN the document, with each one bookmarked. On Document_New (fired when the user invokes the template), a userform displays with the choices.

    The user selects "Cost Recovery Letter" and clicks OK.

    The userform closes and deletes the bookmarks (and therefore the content) of the other letter structures.

    What are they left with? A new Cost Recovery Letter document.

    If they chose Training Cost Request Letter, then the others are deleted, and what are they left with? A new Training Cost Request Letter.

    The power of bookmarks. The power of templates. It is all a matter of design.

    I have one template that handles nine different (but similar) letters. A very good reason for having them in one template. Each of the nine share about 60% of user input data.

    So no matter which of the nine choices they select, they get the same input userform to get that data. That way I only have to build ONE userform - rather than nine separate "template" files, and nine different userforms. Although yes, you can copy userforms between files...but why bother????

    Then, depending on what their choice was, a stream of logic specific to that choice runs.

    This may require another userform, or it may not. It depends on the requirements of their chosen letter.

    The point being is that all of this is in ONE .DOT file. Yes, I admit building them takes serious design considerations, and a bit of work. However, once this is done, changes only have to be done in ONE file.

    For example. We changed a graphic that was in the header. Badda-bing, badda boom. I open that ONE file, change the graphic. Done. All those nine different letters will be produced with the new graphic, as they all come from the same template file.

    PLUS, I keep the template files on a user restricted network folder, so they can not mess around with them. They can USE them, but that is it. PLUS, I can run very easy code to change ALL of my template files in one operation. I can change the graphic (or whatever) in ALL my templates in a twinkling.

    For "localized" templates used only in our national headquarters (not in the field), I actually do not put graphics in the template. The template New code uses WinHTTP to grab a graphic from a web server on our Intranet, and puts it into the header.

    When the template makes a new document it actually retrieves the graphic dynamically from a web server, using WinHTTP. That way, the graphic for ALL files comes from ONE location, and is always the latest one.

    NOTE: the same code is also in Document_Open, so each time the document is opened it always grabs the latest image. Change the graphic, no need to ever update any documents. They always use the latest image.

    This obviously is not the route for everyone. We have T3 connections within our headquarters. And I only use this for document I know are ONLY being used there. Field documents do not use this. We have T1 connections most places, but nevertheless with 30,000 users scattered over 5,000 points of services across 6 time zones.....our network guys would not approve.

  9. #9
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    Can geelgirlau's code be modified to unlock the templates using a specific password? His code is set up to close with the password. I've already got that covered.

  10. #10
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    What's your very easy method to change all the templates in one operation?

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    If all the template files are in one folder, then it is easy.

    Make a Sub of whatever it is you are doing for each file.

    Use Dir at that folder location, with *.dot (instead of the commonly used *.doc). Then call the sub for each file. Simple. Like this:[vba]Sub CheckMyDir()
    Dim path As String
    Dim strFile As String
    Dim getFile

    path = "c:\myfiles\"
    getFile = Dir(path & "*.dot")
    Do While getFile <> ""
    strFile = path & getFile
    Documents.Open Filename:=strFile
    Call DoSomething(strFile)
    With ActiveDocument
    .Save wdSaveChanges
    .Close
    End With
    getFile = Dir
    Loop
    End Sub

    Sub DoSomething(strIn As String)
    ' whatever it is you are doing
    ' With ActiveDocument.Sections(1) _
    ' .Headers(wdHeaderFooterPrimary)
    ' .Range.Delete
    ' .Range.Text = "Yahhooooooo!"
    ' End With

    MsgBox strIn
    End Sub[/vba]This looks for each file with .dot, and sends the full path and filename of the found file to the Sub DoSomething.

    Above, DoSomething will just display the full path and name for each .dot file in the folder myFiles. But I commented a possible change. It could delete the existing header for Section 1 (Primary) and replace it with "Yahoooo!" So every .dot file in myFiles would have this performed on it.

    It does not matter, it is just another procedure.

    Also attaching a VERY simple demo of a multiple content file that removes "chunks" depending on user selection. Note: this is NOT a template file, but a document file. I am doing it this way simply to make it easier to look at.

    All explanation on use are on the userform.

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Can geelgirlau's code be modified to unlock the templates using a specific password?
    If it is the same password...of course.

  13. #13
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    Quote Originally Posted by fumei
    If all the template files are in one folder, then it is easy.

    Make a Sub of whatever it is you are doing for each file.

    Use Dir at that folder location, with *.dot (instead of the commonly used *.doc). Then call the sub for each file. Simple. Like this:[vba]Sub CheckMyDir()
    Dim path As String
    Dim strFile As String
    Dim getFile

    path = "c:\myfiles\"
    getFile = Dir(path & "*.dot")
    Do While getFile <> ""
    strFile = path & getFile
    Documents.Open Filename:=strFile
    Call DoSomething(strFile)
    With ActiveDocument
    .Save wdSaveChanges
    .Close
    End With
    getFile = Dir
    Loop
    End Sub

    Sub DoSomething(strIn As String)
    ' whatever it is you are doing
    ' With ActiveDocument.Sections(1) _
    ' .Headers(wdHeaderFooterPrimary)
    ' .Range.Delete
    ' .Range.Text = "Yahhooooooo!"
    ' End With

    MsgBox strIn
    End Sub[/vba]
    I tried this macro, but it didn't open any files. The only thing I did was change the folder name to a folder I had template files in and get rid of the wdSaveChanges argument as that was causing an error.

  14. #14
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    If you used it just as you posted then it will do nothing...all of the code in the DoSomething subroutine is commented so there is no command to do anything. The first part of the code...CheckMyDir just finds files in that directory and runs DoSomething on it...then loops to the next file and runs DoSomething...etc.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  15. #15
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Also attaching a VERY simple demo of a multiple content file that removes "chunks" depending on user selection. Note: this is NOT a template file, but a document file. I am doing it this way simply to make it easier to look at.
    Gerry,
    Thanks for posting that.....it's very interesting. I had never considered using a template like that.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  16. #16
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    But if I used the code and commented out the Save and Close lines, shouldn't it at least open all the files in the folder? It isn't. What do I need to change to get it to do that?

  17. #17
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    If it is not working for you, then you changed something incorrectly.[vba]Sub CheckMyDir()
    Dim path As String
    Dim strFile As String
    Dim getFile

    path = "c:\myfiles\"
    getFile = Dir(path & "*.dot")
    Do While getFile <> ""
    MsgBox getFile
    getFile = Dir
    Loop
    End Sub[/vba]This simply displays a message box for each of the found files. It does not open them.

    So....change it to the folder you are trying to work with. Change it to the file extension you are working with - it is using .DOT. If that is what you want to work with, then fine.

    If you do not get anything...there ain't no .DOT files in the folder you specify as the variable path. That is, in fact, part of the code
    [vba] Do While getFile <> ""[/vba]If there is nothing that matches the Dir parameters, then it IS "".

    Are you sure there are files that match your Dir parameter?

  18. #18
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Steve, ah....thank you. You are welcome. Properly this should be a .DOT (template file). That way, the originator (the source file, the template .DOT) remains the same - with all the chunks. The documents produced are different according to user need.

    The best part is that those "different" documents - and they ARE different - will all have access to any VBA code in the template.

    NOTE: my demo used a keyboard shortcut to display the userform again. You would NOT do that in a production environment. Why?

    Because the userform has the chunk choices. Once a document is produced, those chunks are no longer there. So a userform that displays choices that would only produce an error if selected....ummm, is bad design right?

  19. #19
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Cheryl, post your code please. There has to be some syntax error, or something.

  20. #20
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Quote Originally Posted by fumei
    Steve, ah....thank you. You are welcome. Properly this should be a .DOT (template file). That way, the originator (the source file, the template .DOT) remains the same - with all the chunks. The documents produced are different according to user need.

    The best part is that those "different" documents - and they ARE different - will all have access to any VBA code in the template.

    NOTE: my demo used a keyboard shortcut to display the userform again. You would NOT do that in a production environment. Why?

    Because the userform has the chunk choices. Once a document is produced, those chunks are no longer there. So a userform that displays choices that would only produce an error if selected....ummm, is bad design right?
    Gerry,
    I understand that it should be a dot. I was able to open it this way easily and check it out and close without saving changes to see it work on the next attempt......it's very cool and I will be adapting some of my templates to this method...also understand why you would not want the userform back after the new document is produced....
    You give very clear explainations.....I appreciate it.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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