Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open excel outside browser 1

Status
Not open for further replies.

goober808

Programmer
Oct 14, 2003
27
US
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....
 
here's an xhtml-compliant way (the target attribute is deprecated, but using script to set it is legal):

<a href="myExcelFile.xls" onclick="this.target = '_blank';">click me</a>

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Jemminger,

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?

Thanks,
goober
 
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?

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
goober808

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

makedoc("C:\Book1.xls")

</SCRIPT>
 
>[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>
 
>[tt]myApp = nothing;[/tt]
nothing? Maybe null.
 
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.
 
I don't want to be picky at all. I've done my due in the name of science.
 
THANK YOU all for your input. i'm going to give it a whirl and see what my out come is. I'll keep you posted. Thank you again!!!
 
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.

Thanks in advance for your help!!!
 
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.



Paranoid? ME?? WHO WANTS TO KNOW????
 
theniteowl,

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

Thanks,
Goober
 
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>

Paranoid? ME?? WHO WANTS TO KNOW????
 
theniteowl,

THANK YOU SO VERY MUCH!!!! you are the greatest. I tried your coding and it works perfectly!!!

Thank you,
Goober
 
You are welcome.
Just remember the double slashes if you add the file into a sub folder.

Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top