|
-
May 10th, 2022, 10:54 AM
#1
Open office document, but prevent "Save As"
I am making a WinForms app using C#. The app has a database with documents (word, excel, pdf, etc...)
My requirement is that the user can save the document (which in turn the file will be updated in the database), but the user cannot make a copy / "Save As"
Ideally I need to do this for any file type, but for now let's start with Office documents.
I don't want to use macros.
Is there a parameter / argument that I can pass to Excel or Word to tell it to disable "Save As" option in the menu ?
Right now I use this code to open the files regardless of file type:
Code:
public static void OpenWithDefaultProgram(string path)
{
Process fileopener = new Process();
fileopener.StartInfo.FileName = "explorer";
fileopener.StartInfo.Arguments = "\"" + path + "\"";
fileopener.Start();
}
What are my options?
-
May 10th, 2022, 11:45 AM
#2
Re: Open office document, but prevent "Save As"
 Originally Posted by CVMichael
What are my options?
Probably none.
If I'm understanding your project, you essentially have a document repository in a database, your program interacts with that database and lets a user view documents inside of the repository, and you do so by opening the file in whatever the default application is for that file type on the user's computer. And you are trying to prevent that application from allowing the user to save a copy of that file? For any and all applications that might be used to open these files (keeping in mind that not all users open .docx files in Microsoft Word, or .pdf files in Adobe Reader, etc.)?
I can't see this working as you've described.
Good luck
-
May 10th, 2022, 11:50 AM
#3
Re: Open office document, but prevent "Save As"
From another thread and code is in VB.NET:
 Originally Posted by peterst
Inside DocumentBeforeSave event management you have to simulate what Word do and you will have to do the save yourself so you can catch it. Something like this:
VB.NET Code:
Private _saveActive As Boolean = False
Private Sub DocumentBeforeSave(Doc As Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean)
If _saveActive Then
Return ' If save is active then just return
End If
_saveActive = True
If SaveAsUI Then
' Show UI when SaveAs or new file is saved - needs user interaction to select file
Dim saveDialog = _word.Dialogs.Item(Word.WdWordDialog.wdDialogFileSaveAs)
saveDialog.Show()
Else
Doc.Save()
End If
Dim filename = Doc.FullName ' Grab the file name of the document
'
' Do whatever you want with the file - upload to network storage, save in db BLOB, ...
'
_saveActive = False
Cancel = True ' Cancel the save operation as document was just saved
End Sub
If file is new or SaveAs is performed, it is required some user interaction to select destination file name so save dialog is shown. In the other case when no UI is required, the save is just performed. Then you can get from Word.Document object the file name and do whatever you want.
Set Cancel to false if you want to prevent saving. You have to add your logic as the example is from different thread and requirement.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|