Opening a password protected word document (1 Viewer)

kbreiss

Registered User.
Local time
Today, 03:12
Joined
Oct 1, 2002
Messages
228
I have all my code working...However I wanted to set a password to a word document to open. I want to be able to pass the password in my code, but can't figure it out...Here's my code...

With Appword
.Visible = True
?????supply the password here to open document???????
.Documents.Add "E:\restored folderProjects4-28\Seniors\Seniors Database\PopulateQry.doc"
.ActiveDocument.Bookmarks("bkDate").select
.selection.Text = rstMerge![Event_Date]
.......................................................................
________
Motorcycle Tires
 
Last edited:

freakazeud

AWF VIP
Local time
Yesterday, 23:12
Joined
Sep 10, 2005
Messages
221
Hi,
check this sample code:

Option Explicit

Public Sub ProcessFile()

Dim strFileName As String
Dim strFilePath As String
Dim oDoc As Document

' Set Directory for file
strFilePath = "C:\Test\"

' Get Name of .doc File from Directory
strFileName = Dir$(strFilePath & "*.doc")


While Len(strFileName) <> 0

' Set Error Handler
On Error Resume Next

' Attempt to Open the Document
Set oDoc = Documents.Open( _
FileName:=strFilePath & strFileName, _
PasswordDocument:="YourPassword")

Select Case Err.Number
Case 0
' Document was Successfully Opened
Debug.Print strFileName & " was processed."

Case 5408
' Document is Password-protected and was NOT Opened
Debug.Print strFileName & " is password-protected " & _
"and was NOT processed."
' Clear Error Object and Disable Error Handler
Err.Clear
On Error GoTo 0

Case Else
' Another Error Occurred
MsgBox Err.Number & ":" & Err.Description
End Select

' Disable Error Handler
On Error GoTo 0

'Perform Action on Document Here


' Close Document
oDoc.Close

' Clear Object Variable
Set oDoc = Nothing

End Sub

You might need to set a reference to microsoft scripting runtime. Untested, but worth a try I guess :)
HTH
Good luck
 

kbreiss

Registered User.
Local time
Today, 03:12
Joined
Oct 1, 2002
Messages
228
Document.Open vs Document.Add?

Thanks for the info...I've got it working w/ your sample code of using "Document.Open..........", but why wouldn't it work the same way with "Document.Add..........."
________
Macumba Dicussion
 
Last edited:

freakazeud

AWF VIP
Local time
Yesterday, 23:12
Joined
Sep 10, 2005
Messages
221
You're welcome.
Glad I could assist.
Good luck on future projects!
 

Bat17

Registered User.
Local time
Today, 03:12
Joined
Sep 24, 2004
Messages
1,687
Document.add creates a new document from a template rather than opening an existing one

Peter
 

Users who are viewing this thread

Top Bottom