How to add password to word document from VBA?

T

Tim

Hi everyone,

I posted my question in Access's newsgroup, but someone
referred me to here. I have the following code to add
password to my word document from access.

Dim oWord As Object

Set oWord = CreateObject("word.application")
oWord.Visible = True
oWord.Documents.Open "c:\TempDoc\test.doc"
oWord.ActiveDocument.SaveAs
filename:="c:\tempdoc\test.doc", Password:="123"
oWord.ActiveDocument.Close
oWord.Quit
Set oWord = Nothing

End Sub

The code looks like fine, but it doesn't add the password
to my document. If I changed oWord.ActiveDocument.SaveAs
filename:="c:\tempdoc\test.doc", Password:="123" to
oWord.ActiveDocument.SaveAs filename:="test.doc",
Password:="123", then it will add the password to test.doc
in word's default folder not my original folder -TempDoc.

Does anyone know how to add password
to"c:\tempdoc\test.doc"?

Thanks in advance.

Tim.
 
D

Doug Robbins - Word MVP

Hi Tim,

Because your code has made no change to the document, Word is lazy and
doesn't bother saving it.

Try inserting a line

oWord.ActiveDocument.Saved = False

before the save to try and convince Word that the document needs saving.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
T

Tim

Doug,

It works fine. Thanks for your help.

Tim.
-----Original Message-----
Hi Tim,

Because your code has made no change to the document, Word is lazy and
doesn't bother saving it.

Try inserting a line

oWord.ActiveDocument.Saved = False

before the save to try and convince Word that the document needs saving.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP



.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top