problem with spaces in opening Excel file

ebodin

Registered User.
Local time
Yesterday, 18:53
Joined
Apr 25, 2005
Messages
15
I want to be able to launch a certain excel file from a button in Access. I've searched the forum and have found that there are problems passing filepaths with spaces in them. The solution is using more quotes around the filepath to pass the spaces too. However, I'm still having trouble. Can you spot any other problems with this code? It launches an excel shell, then gives me a "file not found" error.
thanks for your help. Please speak slowly in small words :D I don't have much experience in Access/VBA.

Code:
Private Sub cmd_OpenExcel_Click()
On Error GoTo Err_cmd_OpenExcel_Click

    Dim oApp As Object
    Dim TaskID As Variant 'for holding return of shell
    Set oApp = CreateObject("Excel.Application")
    oApp.Visible = True
    'Only XL 97 supports UserControl Property
    
    TaskID = Shell("""EXCEL.EXE C:\Documents and Settings\eb30774\Desktop\Book2.xls""", vbMaximizedFocus)

    On Error Resume Next
    oApp.UserControl = True

Exit_cmd_OpenExcel_Click:
    Exit Sub

Err_cmd_OpenExcel_Click:
    MsgBox Err.Description
    Resume Exit_cmd_OpenExcel_Click
    
End Sub
 
got it

I got it with this code I found on another post. Don't know why the extra quotes didn't give it to me.

Code:
Dim MyFileShell As String
    MyFileShell = ""C:\Documents and Settings\Desktop\Book2.xls""
    MyFileShell = Chr$(34) & "C:\Program Files\Microsoft Office\OFFICE11\excel.exe " & Chr$(34) & " " & Chr$(34) & MyFileShell & Chr$(34)
    Call Shell(MyFileShell, 1)
 
I prefer to use the ShellExecute method to open a file. It does not have the problems that the Shell() function does [as you have discovered].

I have a working example in my Browse [Find a directory or file] sample.
 

Users who are viewing this thread

Back
Top Bottom