0

I am trying to start excel with a file. It works fine when I run it with the same user. But with different user, only excel starts and that also with unknown error.

private void button1_Click(object sender, EventArgs e)
        {
            SecureString securePwd = new SecureString();

            string password = "P@ssw0rd1";
            SecureString sec_pass = new SecureString();
            Array.ForEach(password.ToArray(), sec_pass.AppendChar);
            sec_pass.MakeReadOnly();

            ProcessStartInfo ps = new ProcessStartInfo();
            ps.FileName = "c:\\Program Files\\Microsoft Office\\Office15\\EXCEL.EXE";
            ps.Arguments = "c:\\test_folder\\test.xlsx";
            ps.WorkingDirectory = "c:\\test_folder";
            ps.Domain = "test.local";
            ps.UserName = "testuser";
            ps.Password = sec_pass;
            ps.UseShellExecute = false;
            Process.Start(ps);
        }

enter image description here

The very same code works perfectly fine by changing the process from ps.FileName = "c:\Program Files\Microsoft Office\Office15\EXCEL.EXE"; to ps.FileName = "notepad.EXE";

had there be any rights issue even notepad.exe also should not work.

2
  • This code is not going to get you anywhere. Actually logout of Windows, log back in and use the "testuser" account. And start Excel. If you don't get a better error message then you'd at least have created the profile you'd need to run Excel like that with your code. Commented May 21, 2017 at 12:11
  • It works with notepad.exe Commented May 21, 2017 at 14:10

4 Answers 4

3

Knowing that it's an old post, still leaving a reply for someone like me with a similar problem. Looking at your code, it seems that you may need to set the "ps.UseShellExecute" as "true". I tried a similar code (given below) with a button in my WPF app, and it opens the excel file without issues.

private void Button_Click(object sender, RoutedEventArgs e)
    {
        string myPath = @"C:\Users\Lenovo\Documents\MyFile.xlsx";
        ProcessStartInfo ps = new ProcessStartInfo();
        ps.FileName = "excel"; // "EXCEL.EXE" also works
        ps.Arguments = myPath;
        ps.UseShellExecute = true;
        Process.Start(ps);
    }

And of course, don't forget to add the following line at the top of your .cs script.

using System.Diagnostics;

Well then, Happy coding :)

Sign up to request clarification or add additional context in comments.

Comments

1

There doesn't seem to be any problem withe the code. As without changing a bit, it just started working fine again. Simply nothing. This just opens up a question like what the problem was.?

Any suggestions.?

Comments

0

This is very normal thing. if you working for example in company and you open share Excel file with your friend one of you will get information "File is open by another user" you can resolve this situation copy this file to for example C:/Temp and later replace it on share space.

1 Comment

Doesn't seem to be an issue. Bcz the same file is opened normally if opened with notepad.exe instead.
0

Starting Excel Process using C# code with a non-admin user.

    public void StartExcelProcessAsDifferentUser(string filePath)
        {
            Process proc = new Process();
            proc.StartInfo = new ProcessStartInfo();
            System.Security.SecureString securePwd = new System.Security.SecureString();

            // Path where Excel application is installed. (Might be different for you)
            proc.StartInfo.FileName = "C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCEL.EXE" 
            proc.StartInfo.Arguments = filePath;
            proc.StartInfo.UseShellExecute = false;

            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.LoadUserProfile = true;

            proc.StartInfo.UserName = "TestUser1";
            string pwd = "your_password";

            for (int x = 0; x < pwd.Length; x++)
            {
                securePwd.AppendChar(pwd[x]);
            }

            pwd = "";
            proc.StartInfo.Password = securePwd;
            proc.Start();
        }
  1. Refer to this blog here to learn more about the 3 properties that are set to true.
  2. For LoadUserProfile=true the user's profile in the HKEY_USERS registry key is loaded.
  3. For the filePath argument, pass in the Excel file path you want to open as a different user. If null, Excel application will open to a new blank workbook.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.