?
Solved

Open Passworded Document with Macro

Posted on 2008-11-19
8
Medium Priority
?
219 Views
Last Modified: 2012-05-05
I on occassion use a macro to open a doc that is passworded.  The problem is that the macro includes the password.  Ideally, I would want the macro to stop at the password prompt, but the stop button, in creating the macro, is locked off.  I can only stop after entering the password.  Looking at the script of the macro, I can't figure what to edit to have the macro stop at the prompt allowing the user to simply type in the password.  Any suggestions?
Sub macOpenDoc()
'
' macOpenDoc Macro
'
  Documents.Open FileName:="document.doc", ConfirmConversions:=False, ReadOnly:= _
        False, AddToRecentFiles:=False, PasswordDocument:="rex", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
    ActiveDocument.Shapes("Picture 9").Select
    Selection.Copy
End Sub

Open in new window

0
Comment
Question by:rolandjvalverde
  • 4
  • 4
8 Comments
 
LVL 23

Expert Comment

by:irudyk
ID: 22999191
Try using the following:
Sub macOpenDoc()
'
' macOpenDoc Macro
'
 
    On Error Resume Next
 
OpenFile:
    Documents.Open FileName:="document.doc", ConfirmConversions:=False, ReadOnly:= _
        False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
 
    If InStr(1, ActiveDocument.Name, "document.doc", vbTextCompare) = 0 Then
        If MsgBox("You didn't enter the correct password." & vbCr & vbCr & "Try again?", vbInformation + vbRetryCancel, "Incorrect Password") = vbRetry Then GoTo OpenFile
        Exit Sub
 
    End If
 
    On Error GoTo 0
 
    ActiveDocument.Shapes("Picture 9").Select
    Selection.Copy
End Sub

Open in new window

0
 

Author Comment

by:rolandjvalverde
ID: 23004284
Well (irudyk) your code gave me the msgbox with your info in it and a choice of buttons, however since I am trying to open a passworded document, I was expecting a msgbox where I could type in my password to open the document.
Could you make the change to your code to allow for that?  In my research I came across a "dialog" code that would allow the user (me) to interact, like typing in the password.  I may be way off on this, but I thought it was worth mentioning.
Thanks for your help.
0
 
LVL 23

Expert Comment

by:irudyk
ID: 23004365
Okay, you got me confused a bit here...The dialog box that appears is the standard Word dialog that you would see if you were to manually do a file open of a password protected document (i.e. it is not anything special I created).  I'm not clear as to how does this not achieve what you are looking for (you are prompted for the password, if it is entered correctly the document opens and the remainder of your code continues, if not either try again or stop processing the remainder of the code)?
Also, why make a special dialog form to have you enter a password to open a document when Word already has it built right-in for you to work with (i.e. as I did in the above posted code)?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 

Author Comment

by:rolandjvalverde
ID: 23005940
I think it's the corresponding back and forth, it allows for a little bit of mis-interpretation.
I've attached a doc that shows the images of what dialog box i get when i manually open a passworded document.  The second image is what i get with macro with irudyk's code in place.  I'm also sending the current code, to make sure i've entered it in correctly.
My goal is to:  use a macro in one document to open another passworded doc and be prompted to key in the password, that's all.
Our goal was not to "make a special dialog" <that is where the mis-interpretation comes in>    it is to only build a macro that takes me as far as the password prompt, and that's all.
Hope this helps, if not maybe we can setup a phone call...on my dime of course.
Sub macOpenDoc()
'
'
    On Error Resume Next
OpenFile:
    Documents.Open FileName:="document.doc", ConfirmConversions:=False, ReadOnly:= _
        False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
    If InStr(1, ActiveDocument.Name, "document.doc", vbTextCompare) = 0 Then
        If MsgBox("You didn't enter the correct password." & vbCr & vbCr & "Try again?", vbInformation + vbRetryCancel, "Incorrect Password") = vbRetry Then GoTo OpenFile
        Exit Sub
    End If
    
    On Error GoTo 0
    
    ActiveDocument.Shapes("Picture 9").Select
    Selection.Copy
End Sub

Open in new window

explanation.doc
0
 
LVL 23

Expert Comment

by:irudyk
ID: 23006394
Okay, so lets go through this code...
If you run the above macro, the first thing you should see is the 1st image that you have shown in the explanation.doc (code lines 6-9)
If you enter anything other than the correct password, document.doc will not be the active window in Word and as such you will get the Retry/Cancel prompt (i.e. the 2nd image shown in the explanation.doc) (code lines 10-13)
If you enter the correct password, document.doc will open and the Picture 9 shape will be selected and copied into the clipboard (code lines 17-18)
From your comments, are you saying that when you use the macro you don't get prompted for a password and it goes right to the Retry/Cancel prompt?, Or, you do get the password prompt, but when you enter the correct password the file does not open and you then get the Retry/Cancel prompt?
I ask because when I run the above code I see the prompt for the password to open the document and after I enter the correct password the file opens and the applicable shape object is selected and copied into the clipboard.
0
 

Author Comment

by:rolandjvalverde
ID: 23007367
ok, we're making ground.
you were correct when you said:
"when you use the macro you don't get prompted for a password and it goes right to the Retry/Cancel prompt"
We have to figure out why when you run the macro, you get the password prompt.
I am running MS Word 2003.  
Is there any other issue, software or otherwise, that would have the kind of effects we're experiencing?
0
 
LVL 23

Accepted Solution

by:
irudyk earned 1200 total points
ID: 23007443
I know what the issue is...it's the On Error Resume Next on Line 4.  This was put there in case the user clicked Cancel or entered the wrong password.
So an error is occuring on the Document.Open (line 5) thus taking you directly to line 10 in the code.  Try adding a path to the document name like "C:\document.doc" as opposed to "document.doc"
See revised code:

Sub macOpenDoc()
'
'
    On Error Resume Next
OpenFile:
    Documents.Open FileName:="c:\document.doc", ConfirmConversions:=False, ReadOnly:= _
        False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
    If InStr(1, ActiveDocument.Name, "document.doc", vbTextCompare) = 0 Then
        If MsgBox("You didn't enter the correct password." & vbCr & vbCr & "Try again?", vbInformation + vbRetryCancel, "Incorrect Password") = vbRetry Then GoTo OpenFile
        Exit Sub
    End If
    
    On Error GoTo 0
    
    ActiveDocument.Shapes("Picture 9").Select
    Selection.Copy
End Sub

Open in new window

0
 

Author Closing Comment

by:rolandjvalverde
ID: 31518466
Thanks for your patients in helping me with my coding.  As you can tell, I know enough to get myself in trouble.  It's easy to see why your ranked so high in the Hall of Fame.  
Take Care and thanks again
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question