PDA

View Full Version : AutoOpen to remove Protection



wadiohead
03-11-2005, 08:38 AM
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.

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


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



/edit/ Word 2003 /edit/

sandam
03-14-2005, 02:29 AM
Why not create the sub with the neccessary code in it and then call it from the autoopen sub?



Sub MySub

'Put your code in here

End Sub

Sub AutoOpen

Call MySub

End Sub

wadiohead
03-14-2005, 08:34 AM
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.

sandam
03-14-2005, 08:45 AM
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;?

wadiohead
03-14-2005, 08:59 AM
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

sandam
03-14-2005, 09:16 AM
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
ActiveDocument.Unprotect

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.

wadiohead
03-14-2005, 09:23 AM
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

sandam
03-14-2005, 09:37 AM
No problem. Sorry I couldn't give you some more insight. Let me know if you figure it out.

Andrew;?