Open Passworded Document with Macro

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

rolandjvalverdeAsked:
Who is Participating?
 
irudykConnect With a Mentor Commented:
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
 
irudykCommented:
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
 
rolandjvalverdeAuthor Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
irudykCommented:
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
 
rolandjvalverdeAuthor Commented:
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
 
irudykCommented:
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
 
rolandjvalverdeAuthor Commented:
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
 
rolandjvalverdeAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.