Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am using word automation in Word 2007.
I have a normal .docx file which I want to password protect and latter on show the same to user.

When I open the generated .docx with password, word asks if you want to see the read only or not. There is a button to open as read only. The whole purpose of password protecting the file is lost.

Can someone tell me if the below code is right to generate the password protected file. Not sure if I have to do some setting while saving the file or opening the file to show it to user.
VB
Dim value As Object = System.Reflection.Missing.Value
Dim app As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()
Dim doc As Document
Dim missing As Object = System.Reflection.Missing.Value
Dim readOnly1 As Object = False
Dim visible As Object = True
Dim _Password As Object = "abc"
Dim fileToOpen As Object = text

doc = app.Documents.Open(fileToOpen, missing, readOnly1, missing, missing, missing, missing, missing, missing, missing, missing, visible, visible, missing, missing, missing)

doc.Activate()
doc.SaveAs(fileToOpen, missing, readOnly1, missing, missing, _Password, False, missing, missing, missing, missing, visible, visible, missing, missing, missing)
doc.Close(False, False, False)
Posted
Updated 2-Apr-15 9:01am
v2

1 solution

Is it really so hard to check the documentation?

VB
expression .SaveAs(FileName, FileFormat, LockComments, Password, AddToRecentFiles, WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter, Encoding, InsertLineBreaks, AllowSubstitutions, LineEnding, AddBiDiMarks)


Have a look here: SaveAs Method[^]

On the bottom of related page, you'll find best practices to save document with password.
 
Share this answer
 
Comments
Member 11408598 3-Apr-15 3:26am    
Hi,
I have used the method expression .SaveAs(FileName, FileFormat, LockComments, Password, AddToRecentFiles, WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter, Encoding, InsertLineBreaks, AllowSubstitutions, LineEnding, AddBiDiMarks)

a mentioned above like I have posted in my question.
The doc is getting generated as well with password. But when I open the same in word , I get option of opening it as Raed only as well apart from giving password.
I somehow want to disbale that readonly option as it defeats the whole purpose

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900