I am creating an excel link on my webpage, but i would like to open it using the excel application and not in the browser. Is there a way i can do this using javascript? any help would be appreciated....
Thanks for your tip... however, my excel application still opens in IE and not in MS excel. The reason why i need my excel spreadsheet to open in excel is because i have macros running. The macros will run VB script that will output a user input screen (over the actual excel spreadsheet) and upon completion close the excel worksheet. Currently, upon completion, the excel worksheet remains open in the browser window. Does this make sense?
ok... well, you're requesting it via http so i'm guessing it's up to the browser how it wants to display the file. sounds like you might be on an intranet - perhaps make it a network path or ftp path instead?
This is some HTA code I wrote for another project.
This sample is VBScript which you can use but it can be converted to Javascript you just have to look up some minor syntactical differences.
It will prompt the user before executing the code since it relies on an activeX component.
<hta:application/>
<SCRIPT LANGUAGE="VBScript">
Sub makedoc(filename)
Set WSHShell = CreateObject("WScript.Shell") ' Get a reference to the Excel Application object.
Set appExcel = CreateObject("Excel.Application") ' Display the application.
appExcel.Visible = TRUE ' Open a sample document.
appExcel.Workbooks.Open(filename)
Set appExcel = Nothing
End Sub
>[tt]Set WSHShell = CreateObject("WScript.Shell") ' Get a reference to the Excel Application object.[/tt]
This is not needed, the comment misleading and wrong.
Actually, it looks like the comments are shifted from their actual lines.
I just grabbed the code from the sample and now that you point it out I think I actually re-wrote those comments when I used them myself.
Here it is in Javascript.
NOTE that the file path has double slashes.
The one drawback is I have not been able to find a way to make certain that the Excel window gets focus when it opens. Much of the time it does, sometimes it does not.
If you find a way, let me know.
<script language="JavaScript">
function startExcel(strFile)
{
var myApp = new ActiveXObject("Excel.Application");
if (myApp != null)
{
myApp.Visible = true;
myApp.Workbooks.Open(strFile);
myApp = nothing;
}
else
{
alert("Failed to open object");
}
}
startExcel("C:\\Book1.xls");
</SCRIPT>
I do not know if the syntax is correct but this is what is used in all the sample code I have seen.
Just like when you create a database object with something like: rs = myconn.open
You would do
rs.close
rs = nothing
to destroy the object and remove it from memory.
Okay, I do not know what i am doing wrong here, but i have inserted the below coding and my excel spreadsheet is not working.
<script language="JavaScript">
function startExcel(strFile)
{
var myApp = new ActiveXObject("Excel.Application");
if (myApp != null)
{
myApp.Visible = true;
myApp.Workbooks.Open(strFile);
myApp = null;
}
else
{
alert("Failed to open object");
}
}
}
</SCRIPT>
<a href="javascript:startExcel('Basic.xls')">Basic Whole Life Illustration</a>
The reason why i have not inserted the direct path of my excel sheet is because i am running this application off a CD. I have designed a web-based software, for our agents, which is distributed by CD. If i where to code the path to the D: then i may run into problems with drives with different names. I.e. E: or F: I hope I'm not confusing anyone.
The full path IS required for it to open. There are ways of retrieving drive letters and testing the file exists but I do not have code for it here.
What you COULD do though it is a bit sloppy, is to setup a loop of D to Z for the drive letters, attempt opening file at D:\\yourpath\\Basic.xls and if it fails then you increment the loop and try the next letter until you either find it or reach the end of the loop.
thank you for your suggestions, i really appreciate it. I look at the coding yahoo uses when i sent myself my excel spreadsheet, and i noticed that they use "window.location.href". I'm attempting to find a quick fix to my problem and i thought if my spreadsheet works in yahoo then i can duplicate the code to work for me. Do you have an insight on this "window.location.href"?
Good call on that. window.location.href returns the full path of the currently running page.
Based on that I came up with this code.
Unfortunately it is not as easy as just grabbing the path.
Since it will be running locally it will append file:/// to the beginning of the path. So in this script I grab the path, convert all the forward slashes to backslashes (adding extras as escape characters so javascript will not eat them), remove the file:///, then remove the name of the currently executing file.
All you have to do then is provide the name of the file.
If the file is in a sub folder then you have to put the foldername, use two backslashes (one is an escape character) and then the filename.
There may be a more elegant way to do this but this is the best I could do. These regular expressions are a PITA.
BTW, since you know where the file will be I removed passing the filename into the function. If you are going to add more than one file to be opened you could always put it back in and append it to the myfile variable.
<script language="JavaScript">
function startExcel()
{
//Grab path of current executing file.
var fullpath = new String(parent.location);
//Replace all forward slashes with back slashes.
var replacetext = /\//g
fullpath = fullpath.replace(replacetext,"\\");
//Replace %20 with spaces.
fullpath = fullpath.replace(/%20/g," ");
//Remove the name of the currently executing file from the path.
var curfile = fullpath.substr(fullpath.lastIndexOf('\\')+1);
var programpath = fullpath.substring(8,fullpath.length-curfile.length);
//Set name of file to open (ie. myfile.xls or subfolder\\myfile.xls
//Make sure to use two backslashes between folder and filename.
var myfile = "mytest.xls";
var strFile = programpath + myfile;
var myApp = new ActiveXObject("Excel.Application");
if (myApp != null)
{
myApp.Visible = true;
myApp.Workbooks.Open(strFile);
myApp = nothing;
}
else
{
alert("Failed to open object");
}
}
startExcel();
</SCRIPT>
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.