Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to save a Word .doc with password

Status
Not open for further replies.

RemyS

Technical User
Jul 3, 2001
100
GB
I'm trying to write a quick app which will browse through a folder, find the word document files and apply a standard password to each file.

I've exhausted my searches on Word VBA but can't get the password to stick.

This is what I have so far:

Dim iword As New Word.Application, idoc as Word.Document

Set idoc = iword.Documents.Open(WordPath & WordFile)
idoc.Password = Password
idoc.Close


I even incorporated this if clause, which indicated that the password was being applied:
If idoc.HasPassword = True Then MsgBox "Password set for: " & WordFile, vbInformation, "PASSWORD"

I've also tried:
idoc.SaveAs WordPath & WordFile, , , Password, False
but to no avail.

I would expect the Modified date/time to be updated by this last command, but the save doesn't appear to be workiing.


Any suggestions?
Remy Still new to DB's and enjoying learning day by day
 
Isn't there a writepassword boolean param you can set in the saveas method? JHall
 
Unfortunately the idoc.WritePassword is a String param to prevent changes to an open document.

There is a similar idoc.Protect wdAllowOnlyComments, , Password but this only locks the documents change history.

idoc.Password does apply the password necessary to open the document.
The problem is that when the idoc document object is closed, the Password changes are lost.

I have also noticed that the idoc.save commands are doing nothing.

Have I missed something? 101 ways to do it with VB, 99 more things to learn.
 
I found a sollution to this problem.

For some reason the idoc.Password = password was not enough to force the document to save.
I needed to protect and unprotect the document to provide enough activity for the save.

Hence:
idoc.Password = Password
idoc.Protect wdAllowOnlyRevisions 'necessary to force save
idoc.Unprotect
idoc.Save 'May be unnecessary, but better safe than sorry
101 ways to do it with VB, 99 more things to learn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top