PDA

View Full Version : Two questions re Template Password



clhare
06-26-2007, 09:59 AM
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"?

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


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!

geekgirlau
06-26-2007, 06:17 PM
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).


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

clhare
06-27-2007, 06:00 AM
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.

fumei
06-27-2007, 09:07 AM
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.

clhare
06-27-2007, 09:30 AM
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.

fumei
06-27-2007, 09:43 AM
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.

lucas
06-27-2007, 10:27 AM
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.

fumei
06-27-2007, 11:20 AM
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.

clhare
06-27-2007, 11:21 AM
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.

clhare
06-27-2007, 11:24 AM
What's your very easy method to change all the templates in one operation?

fumei
06-27-2007, 12:14 PM
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: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 SubThis 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.

fumei
06-27-2007, 12:20 PM
Can geelgirlau's code be modified to unlock the templates using a specific password?If it is the same password...of course.

clhare
06-29-2007, 06:13 AM
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: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

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.

lucas
06-29-2007, 07:01 AM
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.

lucas
06-29-2007, 07:06 AM
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.

clhare
06-29-2007, 08:31 AM
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?

fumei
06-29-2007, 09:37 AM
If it is not working for you, then you changed something incorrectly.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 SubThis 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
Do While getFile <> ""If there is nothing that matches the Dir parameters, then it IS "".

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

fumei
06-29-2007, 09:51 AM
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?

fumei
06-29-2007, 09:53 AM
Cheryl, post your code please. There has to be some syntax error, or something.

lucas
06-30-2007, 09:53 AM
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.:yes

clhare
07-06-2007, 05:49 AM
Sorry for the delay in posting my code?I had to work on other things for awhile. Anyway, here?s the code as I revised it. I need to open a group of password protected files, but I don?t want to have to manually type in the password and unlock each one.

I want to modify the code so that all the files in the folder will open, the password will be applied to unlock each one of them, and all files are on screen and ready for me to type in.

Sub CheckMyDir()

Dim path As String
Dim strFile As String
Dim getFile

path = "C:\work\My Folder"
getFile = Dir(path & "*.dot")

Do While getFile <> ""
strFile = path & getFile
Documents.Open FileName:=strFile, passwordtemplate:="doccenter"
getFile = Dir
Loop

End Sub

I'm totally sure it's me that's messed up the code!

fumei
07-06-2007, 09:00 AM
Like I said....you must have changed something incorrectly.

You need a "\" on your path!! Look at the code I posted.

path = "C:\work\My Folder\"

Say getFile is "thisTemplate.dot", here is what Documents.Open is seeing:
Documents.Open Filename:= "C:\work\My FolderthisTemplate.dot"In other words, a file named My FolderthisTemplate.dot in the c:\work folder.

BTW: I still think this is crazy crazy. WHY are you having all those files open??????? I am sorry, I just can not believe it is needed. I am not buying that anyone needs to have the number of files open you seem to be talking about.

Could you please tell me why you need to open all those files. Not that that you need to open them, and do stuff. That I can understand. But why you need to have them STAY open.

and all files are on screen and ready for me to type inWhat on earth could possible require this????? Especially if they really ARE template files.

Something not right here Cheryl.

clhare
07-09-2007, 04:34 AM
I would like to have these combined into fewer files, but the people requesting these templates do NOT want that. So, I need to work with what I have.

They also recently made changes that affect the text and the userforms in the letters (changes are not the same in all letters), so I want to make my life easier and open a group of them at one time and unlock them...all in one keystroke (the macro).

I put the "\" in and all the files in the selected folder will open now, however, they are still locked. What do I need to change to get the macro to unlock each of them as they are opened, or is that not possible? Here's the part of the code that I added the password into:

Do While getFile <> ""
strFile = path & getFile
Documents.Open FileName:=strFile, passwordtemplate:="mypassword"
getFile = Dir
Loop

fumei
07-09-2007, 09:12 AM
Sigh.

What kind of password? If it is a password for editing, that is, a protection password, then use Unprotect after the file is open. It is an opening password, then your code should work.

I would like to have these combined into fewer files, but the people requesting these templates do NOT want that. So, I need to work with what I have.Sorry, but I am STILL not buying this. So you have a bunch to work with. WHY, WHY, WHY do you feel you have to have them all open at the same time?????