Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code Opening template with PW (2000 (97, XP))

    Hi All,

    I am looking for some code that does the following:

    - Open a document based on a password protected template
    - Remove all passwords from the document

    As it is now, I use :
    Set oNewDocument = Documents.Add
    (Template:=sTemplateLocation & sTemplateName)

    But when the template is write protected with a password,
    I get the readonly question *and* the password remains in
    the new document, making it write proteced after its first
    save as well.

    Any ideas?

    Regards,

    Jan Karel Pieterse
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  2. #2
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    Are you sure you're working with a template? Only the readonly question shouldn't apply unless you're *opening* a document, not *creating* a new document based on a template (ie using File New or double-clicking in Windows Explorer).

    As far as the password goes, if you password the template to open you will need the password to use it, too; if you only password it to modify it will work fine.

    Since by definition a template's contents (ordinary text, that is) cannot be confidential from any person expected to use copies of it, any text in the template doesn't need to be passworded; all you should need to password would be the code in your VBE, and this can be passworded separately in the VBE without affecting the main document.

    To do this, in the VBE Project Explorer, right click on the name of your template and choose Normal properties, choose the Protection tab, check 'lock project for viewing' and enter the password you want. The template should now be usable (remember to remove the passwords from the front end!) while the code is safely locked away.

    HTH
    Beryl M


  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    Try this:
    <pre>Sub NoPass()
    Dim aDoc As Document
    Set aDoc = Documents.Add("template.dot", False)
    With aDoc
    .AttachedTemplate = vbNullString 'See Note 1
    .Password = vbNullString
    .WritePassword = vbNullString
    .SaveAs "testdoc.doc"
    .Close
    End With
    Set aDoc = Nothing
    Documents.Open "testdoc.doc"
    End Sub</pre>

    Note 1: You might not want to detach your document from the template, but if I don't, I get prompted for passwords for the template after the document opens. Which is annoying.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    Hi Beryl,

    <<Are you sure you're working with a template? Only the readonly question shouldn't apply unless you're *opening* a document, not *creating* a new document based on a template (ie using File New or double-clicking in Windows Explorer).

    As far as the password goes, if you password the template to open you will need the password to use it, too; if you only password it to modify it will work fine. >>

    Maybe I did not explain clearly enough what I'm doing.

    - I have code in an add-in that controls the opening of some templates (lets call them part of a company identity program), which users will use for creating memo's, letters and faxes. I want the users to be able to use the documents without any restriction, but I *don't* want them to be able to make changes to the templates themselves, hence the password protection.

    The templates contain no code, all code is in a central add-in.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    Thanks for the code.

    <<Note 1: You might not want to detach your document from the template, but if I don't, I get prompted for passwords for the template after the document opens. Which is annoying. >>

    Why wouldn't I want to detach? What's the technical reason for that, what are consequences?
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  6. #6
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    In that case all you need is the 'modify' password, which won't affect the template's use by the users.

    Open the template, hit File, Save As, choose the Options button and clear the password from the 'Password to Open' field, leaving only the one in the 'Password to Modify' field. The template should now run fine, but no-one will be able to modify the template, only the copies created by running it.

    Is that what you wanted?
    Beryl M


  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    Jan,

    The setup you describe (custom templates for creating new documents, processes controlled via code in a global add-in) is pretty typical.

    But you shouldn't have to password-protect the templates to keep the users from changing them - put the templates on a network share that is set to Read-Only (and do not password them).

    Gary

  8. #8
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    Hi Beryl,

    I think I will explore this, thanks.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    Hi Gary,

    <<The setup you describe (custom templates for creating new documents, processes controlled via code in a global add-in) is pretty typical.>>

    It is part of a small document system that:

    - takes your personal data (and keeps it)
    - when started presents the user a choice of document type (memo, letter, fax)
    - shows a userfrom that takes general information about the doc, like addressee(s), keywords, title, etcetera
    - opens the appropriate template and fills in this information (and applicable personal data) at the bookmarks in the template
    - adds and/or updates a record in Access with the general information.
    Hence the construction I mentioned.

    <<But you shouldn't have to password-protect the templates to keep the users from changing them - put the templates on a network share that is set to Read-Only (and do not password them).>>

    I thought of that, but I want to have the templates available for notebook users as well, when e.g. they are abroad and haven't got access to out network.

    Contemplating the modify pw as Bery suggested.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    Hi,

    Tried your code. I still get a password dialog, which I need avoided (want to open as readonly). I adapted your code slightly:

    <pre>Sub test123()
    Dim aDoc As Document
    SendKeys "%r"
    Set aDoc = Documents.Add("c:correspondencetemplatesodme memo english.dot", False)
    With aDoc
    .AttachedTemplate = vbNullString 'See Note 1
    .Password = vbNullString
    .WritePassword = vbNullString
    End With
    End Sub
    </pre>


    As you can see, I added sendkeys (I know, I know, terrible way of doing this, highly unreliable, etc, etc) to press the read-only key I subsequently get.

    Any better way to do this?

    NB: If I already have a document open based on that template, the Password dialog does not show! what on earth is going on here?

    Oh I love consistancy!
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    I might have had the template open, and that would explain why I didn't get the prompt. The other workaround then would be to open the template in the background first:

    <pre>Sub NoPass()
    Dim aDoc As Document, aDoc2 As Document
    Set aDoc2 = Documents.Open("template.dot", , True, , , "openpass", , , _
    "readpass", , , False)

    Set aDoc = Documents.Add("template.dot", False)
    With aDoc
    .AttachedTemplate = vbNullString 'See Note 1
    .Password = vbNullString
    .WritePassword = vbNullString
    .SaveAs "c:testdoc.doc"
    .Close
    End With
    aDoc2.Close
    Set aDoc2 = Nothing

    Set aDoc = Nothing
    Documents.Open "c:testdoc.doc"
    End Sub</pre>

    Of course, if your code is editable by the end-users (by directly opening the global template from the Startup folder), your passwords could become known. You could password protect the VBA project to resolve that issue.

  12. #12
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    I would solve this increasingly complex problem by a modification of the simpler method that Gary suggested. I would have just applied a Read Only file property onto the template. Of course, any user with enough knowledge could turn that off again.

    But then again, why would they want to? If they did want to modify the template and they really know what to do then I don't have a problem with that. I figure that employees are trusted with millions of dollars worth of customer relationships and decisions but rarely trusted with $2,000 worth of hardware and software. Just seems odd to me so I rarely lock features and templates down EXCEPT to stop accidental actions by people who just see a dialog and click OK without reading or understanding it.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  13. #13
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    -------------------------
    NB: If I already have a document open based on that template, the Password dialog does not show! what on earth is going on here?
    -------------------------

    In that instance, as far as it's concerned, you've already given the password in an active situation, so it doesn't need to ask you do it again as long as at least one document based on that template is open. Close 'em all and you'll be back to square one!
    Beryl M


  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    Hi Andrew,

    I agree on complixity. If it would be my call, I'd omit any protection. But it is my colleague from the Company identity dept that insists on some sort of protection. I'll try and convinde her <g>.

    Thanks for your comments.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  15. #15
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code Opening template with PW (2000 (97, XP))

    Hi,

    Thanks for that code. If my colleague keeps insisting on this password protection, I'll use it.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

Page 1 of 2 12 LastLast

Posting Permissions

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