Open a Excell file

mtagliaferri

Registered User.
Local time
Today, 09:54
Joined
Jul 16, 2006
Messages
550
Hi I have been looking around if I could find a simple way to open a specific excell file from access: I need to open a excell file from access located in a specific location "C:\Users\....." from a comand in Access, when then I close the excel file I should return to my Access page.
Thanks
M
 
You can add a reference to the Excel object model and learn how to use that. This is the route to go if your code needs to close the Excel file.

But in most cases the user will close the file when he is ready. If so, you can just use Shell to open the file. Here's my notes on shell.


similar to: System.diagnostics.Process.start

Here's one usage of Shell, which opens an HTML file even without knowing the path to IExplorer.
Dim A As Long
A = Shell "RUNDLL32.EXE URL.DLL,FileProtocolHandler " & "C:\Menu.html", vbMaximizedFocus
With the 2nd usage of Shell:
(1) You cannot do
Shell "C:\Notes.txt"
because you haven't named the executable (in this case Wordpad).
(2) You must specify the ENTIRE, FULLL path to the executable
Dim exe As String
exe = "C:\Program Files\Windows NT\Accessories\wordpad.exe"
(3) Then surround that path with dblQuotes
Dim dblQuote As String
dblQuote = """"
exe = dblQuote & exe & dblQuote
(4) Now add a space to separate the executable from the filename
Shell exe & " " & "C:\notes.txt"
 
Thanks for your hel...but I am now bit confused....
this is the file an the path I need to open: C:\Users\User 1\Documents\Annual Leave Planner.xls
which I will launch by a push button named "cmdAnnualLeave" on a form ...
what would the VB string?
 
Thanks for your hel...but I am now bit confused....
this is the file an the path I need to open: C:\Users\User 1\Documents\Annual Leave Planner.xls
which I will launch by a push button named "cmdAnnualLeave" on a form ...
what would the VB string?


I don't understand your confusion. The example I gave you launches Wordpad.Exe. I assume you wanted to launch Excel.Exe, in this case you would begin with finding out the path to Excel.Exe which, on my computer, apparently, is

C:\Program Files\Microsoft Office\OFFICE11\Excel.Exe


Have you tried this?
 
mtagliaferri,

You can open a file that has associations to an application without invoking the application. One way to do this is in VB:
Code:
Shell "C:\Users\User 1\Documents\Annual Leave Planner.xls"
 
Looks like I cannot get it to work!
I have plased this code on a pushbutton on a mask
Code:
Private Sub CmdAnnualLeavePlanner_Click()
Dim A As Long
A = Shell("RUNDLL32.EXE URL.DLL,FileProtocolHandler " & "C:\Users\User 1\Documents\Applications\Annual Leave Planner.xls", vbMaximizedFocus)
End Sub
But it wont run
Thanks
M
 
Looks like I cannot get it to work!
I have plased this code on a pushbutton on a mask
Code:
Private Sub CmdAnnualLeavePlanner_Click()
Dim A As Long
A = Shell("RUNDLL32.EXE URL.DLL,FileProtocolHandler " & "C:\Users\User 1\Documents\Applications\Annual Leave Planner.xls", vbMaximizedFocus)
End Sub
But it wont run
Thanks
M

The path to the excel file has spaces,so you might need to wrap the excel-file path in the dblQuotes that I suggested above.
 
I have modified the code as:
Code:
Dim exe As String
exe = "C:\Program Files\Microsoft Office\Office12\Excel.exe"
Dim dblQuote As String
dblQuote = """"
exe = dblQuote & exe & dblQuote
Shell exe & " " & "C:\Users\User 1\Documents\Applications\Annual Leave Planner.xls"
But still give me a error!
 
I actually opens EXCEL application with a blanc form, but not the specific file! and how ca I get it to open at full size!
Thanks
 
I actually opens EXCEL application with a blanc form, but not the specific file! and how ca I get it to open at full size!
Thanks

I'm sorry you're having so much trouble with this. I just recreated your "Users" directory on my CDrive and then used the following code (I tested it and it works) - the only difference being that my Excel file is Office11 folder not Office12 so you may have to change that part of my code:

Dim exe As String
exe = "C:\Program Files\Microsoft Office\Office11\Excel.exe"
Dim dblQuote As String
dblQuote = """"
exe = dblQuote & exe & dblQuote
Dim pathToSheet As String
pathToSheet = "C:\Users\User 1\Documents\Applications\Annual Leave Planner.xls"
pathToSheet = dblQuote & pathToSheet & dblQuote
Shell exe & " " & pathToSheet
 
Thanks, I finally got it to work!
It does open the specific excell file, but something strange is happening:
the excel file has a autorun macro which opens the file removing toolbars and other excel bits to make it more final user easy, when launching the comand from the VB instruction in Access it will open the file without running the autorun and it is not maximazed on the screen, how can that possible be?
thanks again
Marco
 
Thanks, I finally got it to work!
It does open the specific excell file, but something strange is happening:
the excel file has a autorun macro which opens the file removing toolbars and other excel bits to make it more final user easy, when launching the comand from the VB instruction in Access it will open the file without running the autorun and it is not maximazed on the screen, how can that possible be?
thanks again
Marco

As for not being maximized, make sure you called it this way:

Shell exe & " " & pathToSheet, vbMaximizedFocus

I don't know about the autorun. I had some code in the Workbook_Open event which worked fine for me, if this what you mean.

How is your "autorun" set up? In the workbook_open event?

But I doubt i can help here, due to lack of experience on this issue.
 
Thanks, it works now, and it maximise excell and runs the autorun macro.
I created thecode uder a pushbutton on a form, I would like to run it from a pushbutton on the switchboard, I have edited trought the switchboard vizard chosing the oprtion "Run Code" and in "Function Name" I have placed the path
Code:
Private Sub CmdAnnualLeavePlanner_Click()
at the top ov the VB code, but i will not work, I believe because it is related to that specific form. I hae thn crewated a module placeing te code in the module and in the "Function Name" I have the name of the module, but when I run that it give me an error highlighting the initial part of the code!
Thanks!
 

Users who are viewing this thread

Back
Top Bottom