Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    test if a DOCument is locked?

    How can I test if a DOCument is locked? Issuing a Documents.Open on a password-protected document fails.


    This is how I test for a Template document being locked. It's a different process. I can open the template and then perfom tests on the components. I stole this code from PayneConsultingGroup who wrote the VBA Code Cleaner.


    <pre>Public Function boolTestLocked(doc As Document)
    ''' The VBComponents collection of the project being cleaned.
    ''' These must be declared at the module-level so they can be
    Dim mcmpComponents As VBComponents
    On Error Resume Next
    Set mcmpComponents = Nothing
    Set mcmpComponents = doc.VBProject.VBComponents
    ''' If true, the project was protected.
    If mcmpComponents Is Nothing Then
    boolTestLocked = True
    Else
    boolTestLocked = False
    End If
    'Sub TESTboolTestLocked()
    'MsgBox boolTestLocked
    'End Sub
    End Function
    </pre>



    Here is a simple sub which is trying to open a Word DOCument (P.DOC). To the best of my limited knowledge there is no way to get around the stupid, STUPID little InputBox that pops up and requires manual intervention.

    Manual Intervention is not an option when one is trying to batch process numbers of documents.

    <pre>Sub TEST()
    On Error GoTo locked
    Documents.Open FileName:="dersp.doc", passworddocument:="", writepassworddocument:=""
    GoTo notlocked
    locked:
    On Error GoTo 0
    MsgBox "locked"
    notlocked:
    End Sub
    </pre>


    Even
    <pre> Documents.Open FileName:="dersp.doc", ReadOnly:=True
    </pre>

    doesn't circumvent the InputBox. I had hoped I might essay by first opening it read-only (don't ask me for the password) and then performing a subterfugean (?) test.

  2. #2
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    Chris -
    I'm not quite sure what you are requesting?
    are you asking for some code? -as I have some.
    or
    are you just posting some code to test for if doc has protection?
    Diana

  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: test if a DOCument is locked?

    The following solution is ugly-but-works, in the true hacking tradition.

    Use a bogus password such as PasswordDocument:="xYz"

    If the document is not protected, the bogus password is ignored. If the document is protected, VBA throws up error 5408 Incorrect Password, which you can trap.

    Tested only in Word 2000.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    > ugly-but-works

    True, and thanks.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    Both.

    I was posting my code for any one else to modify on the road to a solution.

    The problem lay in avoiding the GUI box that pops up.

    Note that i can already test for a locked template.

  6. #6
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    Hi Chris - I have the following code to test if protected
    - you may/may not find useful - regards Diana

    'obtain protection status of document & set flag
    If ActiveDocument.ProtectionType = wdNoProtection Then
    blnDocProtected = False
    Else
    blnDocProtected = True
    End If

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    > If ActiveDocument.

    Thanks Diana for the snippet, but note that you are testing an ActiveDocument, that is, you are working on an already-opened document.

    How do you test for a password-protected document WITHOUYT opening it, so as to avoid the pop-up GUI that insists on haveing manual intervention?

    Manual Intervention is a mess when one is trying to provcess a batch of documents. It is necessary to test prior to opening in order to avoid the need for human intervention.

  8. #8
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    Chris,

    I'm not really up with protection, so forgive me for asking.

    It sounds as if the term "protection" is ambiguous anyway- which doesn't surprise me with Mocrosoft at all.

    What Diana posted will check a document if it is protected for forms, tracked changes or comments. That sort of protection will allow you to open the form, and then to check it.

    Does the sort of protection you're talking about originate at the OS level- and is therefore out of reach of Word?
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    >It sounds as if the term "protection" is ambiguous anyway- which doesn't surprise me with Mocrosoft at all.

    By "protection" I meant password protection , as in (Word97SR2): "File, saveAs, Options, Password to open".

    If you create a new document and then save it as above (I use "password" during tests) you will find, I believe, that you cannot open it in VBA, nor can you test (in VBA) prior to opening it.

    Thus, if you were writing VBA code to open document files and process them, your code would cause to be thrown up a GUI box soliciting the password - which, of course, your VBA code can't provide. On your return from your 2-week vacation, 3 of the 2,000 files would have been processed, the fourth file being the password protected document.


    There appears to be no way around this.


    My original query came about because I'd found that I could test to see if a template was locked - once I had opened the template! If the template is password-protected you're SOL.




    >Does the sort of protection you're talking about originate at the OS level- and is therefore out of reach of Word?

    It's MSWord at work. Again.

  10. #10
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    OK, that helps.

    Is it something you can trap in an "on error" condition?

    I'd try it if I weren't already in the doghouse...
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  11. #11
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    Chris,

    Have a read of Q224351. Then set a reference inn your project and run something like :<pre>Sub File_IsReadOnly()
    Dim dsoDPR As New DSOleFile.PropertyReader
    Dim fName As String
    Dim fIsReadOnly As Boolean
    Dim fProp As Variant
    Set dsoDPR = DSOleFile.PropertyReader
    fName = "C:ReadOnlyTest.Doc"
    fIsReadOnly = dsoDPR.GetDocumentProperties(fName).IsReadOnly
    MsgBox fIsReadOnly & " " & fProp
    End Sub</pre>


    As i say, I think that is only useful if the file is ReadOnly, and that may or may not apply in your circumstances.

    Andrew

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    > Is it something you can trap in an "on error" condition?

    No.

    In answer to your next question: "because it's not an error".

    It's by design. (tm) MSoft


    When we switched from WordBasic to VBA, MSoft removed the ability to lock functions, remember? This is a part of that "design policy", that never under no circumstances, not even when one can or cannot hear the sound of knees-jerking, should we offer any assistance, no way, no how, to anyone who might have thought at any time, ever, this world or in their previous existence as a candy-bar wrapper, about opening (shudder) password-protected documents.

    I think that where MSoft have fallen down on design yet again.

    Thesis 1: It's not necessarily bad to inhibit the opening of password protected documents.

    Thesis 2: It's bad to thwart people who are trying to AVOID OPENING password protected documents.

    See also soapbox

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    > Have a read of Q259971 and see if it can be of assistance.

    Thanks, Andrew. I'm printing it as we squeak. See also my response to Geoff, posted a few moments ago.

    For me the crux is in the referenced article's statement that "Office applications were not designed for unattended execution".


    That's a shame.


    So much power to drive a computer, and we have to have a man with a flag walk in front of it .....



    I will essay with the article's suggestion. However, since we are talking password-protection, I'll bet that this is one of those dialogue boxes that won't be cured by a bit of fiddling with VBA.

    Once a document is open, I can do almost anything with it. It's getting the thing opened in unattended mode, or bypassing it if It can't be opened, that's the problem.

  14. #14
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    Would it be feasible to set all your protected templates to ReadOnly and use the OLE File Properties approach to deal with the problem.

    A grasping at straws approach I know.

    Andrew

  15. #15
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: test if a DOCument is locked?

    Straws indeed. I'm not familiar with the OLE File properties. Is that going to tell me that the document is password protected?

    Note from my original post that once I have a document/template opened, I can test to see if it is locked. Payne Consulting showed me that trick, and I posted sample code for that.

    What I can't do is to open a document that is password-protected.

    Documents that are password-protected seem to insist on manual user intervention.


    I could go to the MSoft definitions of the layout of a wpord document, locate the "passwords" bit and test that, I suppose. That would mean opening the document as a binary file, obtaining pointers, navigating through the crude, sampling the bit, and then making a decision.

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
  •