Lisa
Lisa

Reputation: 3

Prevent user from edit a word file when open in vb.net

I have an application that lists multiple Word and Excel files so when the user double clicks on one, it opens and can be edited. I want to give the user permission to edit or not.

I searched a lot and what I came up with so far is

ProtectedViewWindows.Open(mFolderPath) 
Process.Start(myDocPath)

But it gives me the error:

Reference to a non-shared member requires an object reference.

Upvotes: 0

Views: 467

Answers (3)

Rashid Khan
Rashid Khan

Reputation: 69

Document object offers Protect method which take Protect Type argument and this Type derived from WdProtectionType enum which has following protection types can consider to prevent editing in a word document: code reference is taken from Protect document code example

  • wdAllowOnlyComments
  • wdAllowOnlyFormFields
  • wdAllowOnlyReading
  • wdAllowOnlyRevisions
  • wdNoProtection

Following is the code example that you may consider based on protection

oDocument.Protect(WdProtectionType.wdAllowOnlyReading,vbNull,"password")

Upvotes: 0

Cindy Meister
Cindy Meister

Reputation: 25693

ProtecfedViewWindow is not something you're supposed to use to prevent the user from editing the document. This is functionality built into the Office application to open documents from untrusted sources. The explanation in the Excel language reference is somewhat clearer on this point; you might want to read What is Protected View, also.

So the answer to your implied question is: You can't do what you want using this part of the object model.

Neither Word nor Excel is designed to function as a "Reader" - they're editors. If you want users to read, only, then you need to look for a Reader. For example, save the files in PDF format so that they open in Acrobat Reader.

FWIW it's possible to protect all or parts of Word and Excel files using their respective protection mechanisms. This has to be applied to the opened files using the object models (or by editing the Open XML of the closed files).

Upvotes: 0

Nostromo
Nostromo

Reputation: 1274

This error message means, that Open isn't a shared function in the ProtectedViewWindows class, so you have to create a ProtectedViewWindows object, before calling Open on it.

Something like:

Dim pvw As ProtectedViewWindows = New ProtectedViewWindows
pvw.Open(mFolderPath)
Process.Start(myDocPath)

Maybe the constructor of ProtectedViewWindows expects one or several parameters.

Upvotes: 0

Related Questions