Process.Start("EXCEL.EXE", filepath)

Hello,

Process.Start("EXCEL.EXE", filepath)

If filepath contain any spaces then the excel open in wrong way(say that the file is not exist).

Example:
"C:\Test App.xls" -----> fail
"C:\TestApp.xls" ------> success

Note:
Process.Start("IExplore.EXE", filepath)

works in any case.


How to solve that to make this line of code open the excel with the right file?
LVL 1
amrelgarhy81Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

landerdCommented:
Put some qualifiers around your filename (see extra " in second example)
string testpath = @"C:\test.xls";
System.Diagnostics.Process.Start("EXCEL.EXE", testpath);
testpath = @"""C:\test 1.xls""";
System.Diagnostics.Process.Start("EXCEL.EXE", testpath);
Fernando SotoRetiredCommented:
Hi amrelgarhy81;

The Excel program takes the argument list and when it finds a space it thinks that the full file name has been given. So then following statement :

Visual Basic
Dim filepath As String = "C:\Test App.xls"
Process.Start("EXCEL.EXE", filepath)

C#
string filepath = @"C:\Test App.xls"
Process.Start("EXCEL.EXE", filepath)

Will try to open the files C:\test.xls and App.xls and not the file "Test App.xls. To correct the problem any time the filepath has spaces place " around the string like so

Visual Basic
Dim filepath As String = """C:\Test App.xls"""
Process.Start("EXCEL.EXE", filepath)

C#
string filepath = @"""C:\Test App.xls"""
Process.Start("EXCEL.EXE", filepath)

Excel will now see the filepath as "C:\Test App.xls"

Fernando
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
You can just skip the first parameter and then you don't need to worry about the quotes:

    Process.Start(filepath)
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

amrelgarhy81Author Commented:
I have a small problem in doing that:
the filepath came to the function as  a parameter and i have no access to it.

Public Sub RunExcel(filepath)
   Process.Start("EXCEL.EXE", filepath)
End Sub
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
???

Public Sub RunExcel(filepath)
   Process.Start(filepath)
End Sub

You can't do that?
amrelgarhy81Author Commented:
I can but i need to open a new instance of the excel so i need to do it like that:
Process.Start("EXCEL.EXE", filepath)
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Ok...put quotes around your filepath as both landerd and Fernando have already suggested.

Or...use the StartInfo() property of the process class:

    Public Sub RunExcel(ByVal filepath)
        Dim p As New Process
        p.StartInfo.FileName = "Excel.exe"
        p.StartInfo.Arguments = filepath
        p.Start()
    End Sub
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
I was mistaken...you still need qoutes around the filename with either method:

    Public Sub RunExcel(ByVal filepath As String)
        Dim p As New Process
        p.StartInfo.FileName = "Excel.exe"
        If Not filepath.StartsWith(Chr(34)) Then
            filepath = Chr(34) & filepath & Chr(34)
        End If
        p.StartInfo.Arguments = filepath
        p.Start()
    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.