Consulting

Results 1 to 8 of 8

Thread: AutoOpen to remove Protection

  1. #1

    Question AutoOpen to remove Protection

    I have a bit of weird problem. In my work, we have a Word document launching via ActiveX from IE. The problem is, they idiotically protected it--not password, just protection.

    That's easy enough to get around by creating a new document and inserting the protected file in. Voila. No protection.

    I'd like to automate this via vba and AutoOpen, but I'm encountering annoying problems. When run as a regular Sub, the code works just fine. However, as soon as it's run as AutoOpen, it absolutely refuses to recognize the document as protected.

    [VBA] Sub AutoOpen()

    Dim FullyNamed As String
    Dim DocumentTitle As String

    FullyNamed = ActiveDocument.FullName
    DocumentTitle = ActiveDocument.Name

    Application.Visible = True
    Application.WindowState = wdWindowStateMaximize


    '*****
    ' THIS WOULD GENERATE AN ERROR IF THE DOC WAS PROTECTED: IT DOESN'T IN AUTOOPEN
    '*****

    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = ""
    .Replacement.Text = "asdf"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    '**************

    If ActiveDocument.ProtectionType <> wdNoProtection Then
    Documents.Add DocumentType:=wdNewBlankDocument

    Selection.InsertFile FileName:=FullyNamed, Range:="", ConfirmConversions:= _
    False, Link:=False, Attachment:=False
    On Error Resume Next
    Windows(DocumentTitle).Close
    End If
    End Sub
    [/VBA]

    Thanks so much for your help.
    I'm utterly confused.



    /edit/ Word 2003 /edit/

  2. #2
    Knowledge Base Approver
    Space Cadet
    VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    Why not create the sub with the neccessary code in it and then call it from the autoopen sub?

    [vba]

    Sub MySub

    'Put your code in here

    End Sub

    Sub AutoOpen

    Call MySub

    End Sub

    [/vba]
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  3. #3
    Why not create the sub with the neccessary code in it and then call it from the autoopen sub?
    Occam's razor, eh? I had tried that. Unfortunately, same result. Word seems to treat Protected Documents differently until AFTER the AutoOpen process has finished.

    Thanks, anyway, sandam.

  4. #4
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    Rats. Ah well, nothing ventured, nothing ventured. I don't know how massive your project is but you could also try creating a toolbar with a button in the AutoOpen sub and having that button refer to you sub. Then when the toolbar runs the sub, you can also have the toolbar delete itself. It adds an extra click to the whole process which might be okay if you have to do this procedure a few times a day but ifs got to happen for one person like a hundred times a day then it might be annoying. Its the only way I can think of to have the AutoOpen complete and thus alter the status of the protected document. Let me know if you'd like to try it this way and I can mangle some code together to help you do it.

    Lata
    Andrew;?
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  5. #5
    Laugh.

    Yeah, in the inbetween my reply and your reply, Andrew, I had pretty much given up and decided to create a button. It will be done a bunch of times a day, but it'll do. Mostly, I just hate letting something beat me, you know?

    I guess I don't really understand the advantage of the button deleting itself... The user won't be able to run it on non-protected documents, anyway.

    But thanks for the help. I do appreciate it.

    David

  6. #6
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    No worries. the advantage of having the toolbar delete itself each time is that, Word being a funny animal and toolbars even stranger, you dont have the toolbar appearing multiple times (each time the document is created). I saw something while browsing around and I'm not sure if you've come across it or not. Could be useful, could be something thats already made you pull your hair out
    [vba]ActiveDocument.Unprotect[/vba]

    Andrew;?

    Just thought I'd add - if it works it may save you the trouble of inserting all that code. Then on AutoClose you can protect the document again. I may be misunderstanding your original intentions but hopefully this helps.
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  7. #7
    Yeah, I've worked around that problem before--it should be fine.

    ActiveDocument.Unprotect doesn't do the job either.

    I guess I'm pretty resigned to just using a toolbar button. Oh well.

    Thanks for your help.

    David

  8. #8
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    No problem. Sorry I couldn't give you some more insight. Let me know if you figure it out.

    Andrew;?
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


Posting Permissions

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