Link to home
Start Free TrialLog in
Avatar of amrelgarhy81
amrelgarhy81Flag for Egypt

asked on

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?
Avatar of landerd
landerd

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);
Avatar of Fernando Soto
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
You can just skip the first parameter and then you don't need to worry about the quotes:

    Process.Start(filepath)
Avatar of amrelgarhy81

ASKER

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
???

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

You can't do that?
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)
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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial