Password protect a word doc using vb.net

J

Justin

I use the following routine to open a word doc and protect it with password.
Presently the file is not getting closed properly. I admit that I am new to
Office development. What is wrong in the following code? Will it work with
any version of Word? Also give me the website address which has sample codes
related to Office development using VB.NET

dim strFilename as string = "c:\doc1.doc"
dim oWord as new word.application
dim oWordDoc as new word.document
oWordDoc=oWord.Documents.Open(strFileName)
with oWordDoc
..protect(word.wdprotectiontype.wdAllowOnlyomments,False,"xyz")
..saved=true
..close(true)
end with
oWordDoc=Nothing
oWord.quit
 
J

Jezebel

The mistake is the line

..Saved = true

This tells Word that the document is already saved, so your following line
closes the document without saving. Since you've made changes, you can
probably just delete that line, or to be certain of what's happening, change
it to

..Saved = false
 

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