locked
Unable to reliably open Excel Read Only RRS feed

  • Question

  • I have a VB routine that opens Excel using the /r parameter

                                Dim psi As New ProcessStartInfo
                                Dim fn As String = LocalTemporaryDirectory & UniqueFileName
    
                                With psi
                                    .FileName = "excel.exe"
                                    .Arguments = "/r """ & fn.Trim() & """"
                                End With
                                Process.Start(psi)

    Very rarely the excel process opens read-only, most of the time it opens in protected mode and will then allow the person to click on the enabling editing button and update the spreadsheet.

    Am I missing something, or is there an explanation as to why the above would only open read only some of the time?

    Tuesday, November 1, 2016 7:37 PM

All replies

  • Hi Martin,

    I've tried almost the same code as you, and succeeded. Your code is correct.
    I suspect that target Excel files have something. Try to create new Excel books and then open them with your program.
    ' --- [Select & Open]
    Private Sub btn_Select_Open_Click(sender As Object, e As EventArgs) Handles btn_Select_Open.Click
    	With Me.OpenFileDialog1
    		.Title = "Select Excel file(book)"
    		.CheckFileExists = True
    		.InitialDirectory = "X:\00 北窓舎\00 PCサポート\00 MSDN_TechNet"
    		.Filter = "Excel Book|*.xlsx;*.xlsm;*.xls"
    	End With
    	' ---
    	If (Me.OpenFileDialog1.ShowDialog = DialogResult.OK) Then
    		Me.txt_Excel_FileName.Text = Me.OpenFileDialog1.FileName
    		Call Me.prc_FileOpen_Process
    	End If
    End Sub
    ' --- excecute Excel opening a file with ReadOnly
    Private Sub prc_FileOpen_Process()
    	Dim psi As New ProcessStartInfo
    	Dim fn As String = Me.txt_Excel_FileName.Text
    	' ---
    	With psi
    		.FileName = "excel.exe"
    		.Arguments = "/r """ & fn.Trim() & """"
    	End With
    	Process.Start(psi)
    End Sub
    



    Regards,
    Ashidacchi
    Tuesday, November 1, 2016 11:26 PM
  • Thanks Ashidacchi

    That is very confusing then.

    I have just tried using a batch file and tried opening a newly created Excel file that is located under the %LocalAppData% directory structure and it opens in Preview mode.

    I copied the same test.xlsx to the desktop and tried it from there and it opened as a read only.

    The original spreadsheet was originally located on a network server, although before opening it I made a copy by copying it to the %LocalAppdata% directory structure and tried opening the copy as read only, however it too opens in preview mode when located in the %LocalAppData% directory and strangely "Read only" when the same file is copied to the desktop.

    It seems that "Preview Mode" somehow trumps "Read Only" mode, the problem being you can override the preview mode.  

    Wednesday, November 2, 2016 12:12 AM
  • I have been into my Excel options/Trust center/Preview and unchecked all the preview check boxes and now when I open the Excel read only it opens as read only regardless of where the file is located.

    Is this just my machine, is there any reason why this behaviour would be considered correct? 

    Wednesday, November 2, 2016 12:32 AM
  • Hi Martin,

    I've tested to open files in my network drive.

    Could you provide a full-path of %LocalAppData%?
      # I suppose it is C:\Users\<account>\AppData.

    If I specify that directory, I will try to test using that directory.

    Regards,
    Ashidacchi
    Wednesday, November 2, 2016 1:07 AM
  • Hi Martin,

    I've tested to open files in my network drive.

    Could you provide a full-path of %LocalAppData%?
      # I suppose it is C:\Users\<account>\AppData.

    If I specify that directory, I will try to test using that directory.

    Regards,
    Ashidacchi

    My %LocalAppData% Directory is in the format:

    C:\Users\UserName\AppData\Local\ApplicationName\Temporary

    My Excel trust center settings are:

    Trust Center

    Wednesday, November 2, 2016 7:47 AM
  • Hi Martin,

    Thanks for the information.

    Why do you save your data files in the folder "C:\Users\UserName\AppData\Local\ApplicationName\Temporary"?
    I recommend save them in "C:\Users\<user name>\Documents", if %LocalAppData% is not "must".

    Regards?
    Ashidacchi
    Wednesday, November 2, 2016 11:39 PM
  • In this particular case it is because the application that is running is keeping all of its data files hidden and mostly out of reach from the user - I also wanted the spreadsheet in a folder that I could possibly clean out from time to time without fear of deleting anything important, as a workaround I guess I could create a directory under the user's document folder structure but I would rather have it stored out of sight.

    I'm sure the behaviour has been thought through but in this case something that is trying to be secure is in effect being unsecure.

     
    Thursday, November 3, 2016 12:45 AM
  • Hi Martin,

    I've tried using a folder "C:\Users\<myName>\AppData\Local\Excel_Test\Temporary".
    When I opened Excel files in the folder, every file could be opened with ReadOnly.

    I ran my VB program on my computer.
    Which computer does your program run on?

    Regards,
    Ashidacchi
    Thursday, November 3, 2016 2:19 AM
  • Hi Martin,

    I've tried using a folder "C:\Users\<myName>\AppData\Local\Excel_Test\Temporary".
    When I opened Excel files in the folder, every file could be opened with ReadOnly.

    I ran my VB program on my computer.
    Which computer does your program run on?

    Regards,
    Ashidacchi
    I confirm that my application is also running on my computer, and can you confirm that your Excel's Trust Center Protected View settings are all checked the same as mine?
    Thursday, November 3, 2016 6:30 AM
  • Hi,

    This is mine (Excel's Trust Center > Protected View).

      (This is default setting.)

    Regards.
    • Edited by Ashidacchi Thursday, November 3, 2016 6:35 AM
    Thursday, November 3, 2016 6:33 AM
  • That makes things even more confusing, in order to take stock of things I have gone back and double checked the behaviour.

    This is what I have found.

    The Excel files that I am trying to open were originally sent via email as attachments and then stored on a server.

    I have gone back to the beginning and tried to open them read-only on the server.

    What I have found is that some of the files open consistently as read-only, others consistently in protected mode.

    I think I have now worked out why, when I was first testing that the read only worked - when the file opened in protected mode I checked that I could in fact save the file, the files that now open read only on the server are therefore the files that I have opened first in protected mode and then saved.

    This explains why some open read only and some in protected mode.

    So I guess I could in theory run an application that opens and saves all the files on the server and then they would all open read-only.

    If I copy one that opens on the server as read-only to my local drive it then opens in protected mode. 

    I think I can ignore the LocalAppDaa directory as being of any significance.

    I think I will have to live with the fact that in order that the user doesn't change the original file I will make a copy and open the copy.

    I have called the copy "Do Not Save This File" but my concern is that people still might make changes to the information and think that they are making changes to the original when they are nowhere near the original as they are working on a copy of a copy, but maybe I shouldn't worry.


    Thursday, November 3, 2016 8:35 AM
  • Hi,

    I'll be glad, if you would provide some files via cloud storage such as DropBox or OneDrive.

     # I'm a Japanese, and poor at English. I prefer files.

    Regards.

    Thursday, November 3, 2016 9:03 AM
  • Hi,

    I'll be glad, if you would provide some files via cloud storage such as DropBox or OneDrive.

     # I'm a Japanese, and poor at English. I prefer files.

    Regards.

    It doesn't seem to matter what the files are, it seems to be where they came from, so sending you the actual files is probably not going to help.

    If the file has originated from another computer then the file will open in Protect mode.

    Remote

    I am not sure why if a file is saved and then reopened why the originating from another computer condition is no longer respected, as the file is still identified as coming from another computer but maybe the ownership of the file changes.

    I think I am just going to have to live with it.

    You have been a great help in giving me feedback on the behaviour of your computer and also confirming that the /r code is not the problem. 

    Thursday, November 3, 2016 9:36 AM
  • Hi,

    >>It doesn't seem to matter what the files are, it seems to be where they came from, so sending you the actual files is probably not going to help.

    Maybe this is the actual reason. I test with the code too, but I don’t have the files like yours, so sorry that we could not reproduce your issue.

    Since the issue is not related to the code or Office object model, so I suggest you post new thread in the following forum.

    https://social.technet.microsoft.com/Forums/office/en-US/home?forum=excel

    Thanks for your understanding.

    Regards,

    Celeste



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, November 6, 2016 8:06 AM