Results 1 to 3 of 3

Thread: Open office document, but prevent "Save As"

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,802

    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?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Open office document, but prevent "Save As"

    Quote Originally Posted by CVMichael View Post
    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

  3. #3
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    558

    Re: Open office document, but prevent "Save As"

    From another thread and code is in VB.NET:
    Quote Originally Posted by peterst View Post
    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:
    1. Private _saveActive As Boolean = False
    2.  
    3. Private Sub DocumentBeforeSave(Doc As Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean)
    4.     If _saveActive Then
    5.         Return      ' If save is active then just return
    6.     End If
    7.  
    8.     _saveActive = True
    9.     If SaveAsUI Then
    10.         ' Show UI when SaveAs or new file is saved - needs user interaction to select file
    11.         Dim saveDialog = _word.Dialogs.Item(Word.WdWordDialog.wdDialogFileSaveAs)
    12.         saveDialog.Show()
    13.     Else
    14.         Doc.Save()
    15.     End If
    16.  
    17.     Dim filename = Doc.FullName     ' Grab the file name of the document
    18.  
    19.     '
    20.     ' Do whatever you want with the file - upload to network storage, save in db BLOB, ...
    21.     '
    22.  
    23.     _saveActive = False
    24.     Cancel = True                   ' Cancel the save operation as document was just saved
    25. 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
  •  



Click Here to Expand Forum to Full Width