Consulting

Results 1 to 12 of 12

Thread: Solved: Protecting a word document

  1. #1

    Solved: Protecting a word document

    I've created a word document which has been linked to its own independent template and all connections to normal.dot have been severed.

    What I want to do is this. If a user opens this document and chooses not to include the macros, then the user shouldn't be able to see the contents of the document or (even better) open it at all.

    I tried doing some tricks with the Document_Open() procedure but to no avail. It seems that this procedure is triggered after the user has chosen to include the macro.

    I tried password protecting the document and then wondered whether there was a way to pass that password within Document_Open() but it seems the password request happens before that.

    So is there any way to prevent a user from viewing the contents of a document at all if he chooses not to include the macros?

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    If a user opens this document and chooses not to include the macros, then the user shouldn't be able to see the contents of the document or (even better) open it at all.
    Seems a bit of a logic problem. If teh user OPENS the document, then they can hardly have a choice to NOT open it. It IS opened.

    Now, you can have it that it closes as a result of their choice, but your "better yet" is not possible. The document IS open.

    Could you elaborate on the design purposes of this?

    Code wise, on open you want a user choice - Accept macros, or Not Accept macros. Correct? This is easy from a tecnical point of view. They choose Not Accept...close the document. You state you do not want them to see the contents, well gosh, if the document is closed they are not going to see ity, are they?

    They accept macros, then do nothing, as the document IS opened.

    However, to reiterate, there are design questions here. What are you trying to hide? Why do you need to ask if they wish to accept macros? Why even give them the choice? What is important about giving them the choice? There is omething here that is unstated.

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Oh, and your statement "all connections to normal.dot are severed". Cool. Neat trick. But impossible. You can not sever connections to normal.dot. If you know of a way, please do share. The fact is, if Word is open, then normal.dot is loaded. Word can not run without it.

  4. #4
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Maybe he's trying to force the users to enable macros but has no way (or does not want) to change the macro security to low.

    If that's the reason, maybe harassing the user by hiding the text and / or locking the document and reversing these using a macro on open can already be enough.

    If it should be something to increase security and you need to force the user, you might need a bigger caliber, but maybe that's not necessary.

    Daniel

  5. #5
    What I want to do is really simple.

    At the correct security level, when you double click on a document (which contains a macro) or open it through whatever method, Word will give you a messagebox allowing a choice of (1) excluding the macro (2) including the macro (3) canceling.

    If a user chooses (1) excluding it, I don't want him to see the contents of the word document.

    I'm just curious if it can be done within VBA without accessing any win32 API.

    Oh, and if my initial inquiry was just foolish enough (perhaps I should have prostated myself and admitted I'm a newbie?) to send a few people on a long awaited ego-trip, my congrats to the cunni who gave birth to them.

  6. #6
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Seems a bit of a logic problem.
    Gerry's first line just about sums it up.
    The document is already open when the Enable Macros dialog is shown - if the user chooses no, that's it.
    The only way to use code to control this would be to have the document content generated by the code from document open.
    This is not likely to be practical but as Daniel suggested, you could have the document saved with all the content hidden (or locked in some way) and the Document_Open code exposes it to the user. That said, I can't really think of a way that isn't easily by-passed.
    If this document/template is only going to know users (where you work, for example) you might want to consider digitally signing your code. That way, once the signature has been trusted once by the user, the code will always run without the dialog. It depends whether your looking for a secure solution or whether you just need to "remind" people to use the macros written for the template.
    K :-)

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi and Welcome to VBAX,

    The hidden text option is not gonna work if the user has Show hidden text on by default.

    I can only think of 5 workable options:
    1. Put the template/document in to one of Word's trusted locations. (Templates folder to name one)
    2. Set security to Low (Not recommended)
    3. Sign the code (Buy certificate or use SelfCert.exe)
    4. Start the document with VBA automation
    5. Start the document with WSH Script like:
    [VBA]
    Option Explicit
    Dim WordApp

    set WordApp = CreateObject("Word.Application")
    WordApp.Documents.Open("C:\test.doc")
    WordApp.Visible = True[/VBA]
    Put the code in a textfile and give it extension ".vbs"
    If automated all code will execute bypassing the security settings.

    There's no way within VBA to force the macro setting dialog to acknowledge because that would defeat the purpose of the dialog.

    Please don't let your self get temped to sentences like:
    Oh, and if my initial inquiry was just foolish enough (perhaps I should have prostated myself and admitted I'm a newbie?) to send a few people on a long awaited ego-trip, my congrats to the cunni who gave birth to them.
    Everybody is here to help each other on a free bases.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    At the correct security level, when you double click on a document (which contains a macro) or open it through whatever method, Word will give you a messagebox allowing a choice of (1) excluding the macro (2) including the macro (3) canceling.

    If a user chooses (1) excluding it, I don't want him to see the contents of the word document.

    I'm just curious if it can be done within VBA without accessing any win32 API.

    Oh, and if my initial inquiry was just foolish enough (perhaps I should have prostated myself and admitted I'm a newbie?) to send a few people on a long awaited ego-trip, my congrats to the cunni who gave birth to them.
    1. The last sentence is out of order, but perhaps understandable. Rude, but perhaps understandable.

    2. Let's see. You are right. It is simple. The answer was posted. The user OPENS the document (double click or whatever). The opening macro runs, asking if you want to continue with the OTHER macros available. The openingmacro has to run. It has to run with the Document _Open event. And as stated, but perhaps not clear enough, not being able to see content MEANS....well, not being able to see the contents...which means....the document is not open and visible. Or are you asking about selectively making parts of the document not viewable. if so....say so.

    So here you go from the spawn of the cunni.
    [vba]Sub Document_Open()
    Dim response
    response = Inputbox("This document contains macros. " & _
    "If you wish to continue, press Yes. If you do not " & _
    "want macros active, press No. " & _
    Pressing No will close the document."), vbYesNo
    If response = vbNo Then
    ActiveDocument.Close wdDoNotSaveChanges
    End If
    End Sub[/vba]

    So if they answer no (don't want macros), then the document closes - end of story. Yes...keeps document open. No, closes it.

    If you are asking for something other than this - say a method of selectively choosing procedures (macros), state so clearly.

    I sincerely apologize if clarifying or correcting statements such "severing" connections to normal.dot appears to be an ego trip. I will abase myself for your benefit. Better yet, I will eagerly await learning more, as I realize that gosh, there are always new things to learn.

    As I previously asked, WHAT is "the macro"? It is, I humbly submit, a rather important question.

    You ask for a choice.

    1. exclude "THE MACRO"
    2. include "THE MACRO"
    3. cancel

    Let's make something clear...if, well i MEAN, heck, if that is OK with you. You need to define terms. What does "cancel" mean? Unless you want to actually rewrite the VBA code itself, then the document, if opened, HAS that code. There is no (easy) way to exclude a specific procedure. They are there. What you can do, is have logic that only runs THE MACRO under conditions you determine.

    This is not difficult, and you clarify exactly the conditions you want, we can help.

    Remember - or is that rude of me to even ask? - macros are fired by something. If you have the macro code in the document, but not give the opportunity to fire it...then it will not fire.

    You can also, easily - or is that an ego trip for me to suggest? - have your code determine the user of the document, and say...oh you are Joe Blo...nope, this macro will not run for you; but oh, say...you are Anne Whoever, and yes, this macro will run for you.

    So yes, it can be done with VBA with no need for API.

    Not sure about the comment that an ego trip is "long awaited". It seems that anyone on an ego trip does not await anything.

  9. #9
    Since we are at least maintaining a semblance of civility here, my thanks to all who took the time to reply.

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Glad we could help!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Actually, I was sincerely trying to. I apologize it seemed otherwise.

  12. #12
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Hi Gerry,
    I'm still not sure whether he attacked you or me.

    Daniel

Posting Permissions

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