Solved

VBScript......CreateObject("Excel.Application") is failing when run in web page.

Posted on 2004-04-08
22
221,794 Views
Last Modified: 2011-08-18
I wrote a vbscript that I need to run (client side) everytime someone visits an intranet page. Basically the vbs creates an excel.application object writes some data to the spreadsheet and then saves it. I created a test html page and inserted the vbscript and it works fine when I run it from my pc. But if I try to run it from a server, or even another computer it fails when it tries to create the excel.application object. It says the activeX object couldn't be created. Why? Can someone help please?? Here is some test vbs code I inserted into a test page, it fails when I run it from anywhere but my pc.

<Script language="vbscript">

Dim objexcel

Set objExcel = createobject("Excel.application")   <----------- This is where it's failing

objexcel.Visible = True

objexcel.Workbooks.add

objexcel.Cells(1, 1).Value = "Testing"

objexcel.ActiveWorkbook.SaveAs("c:\exceltest.xls")
objexcel.Quit

</SCRIPT>
0
Comment
Question by:jlininger
  • 7
  • 7
  • 2
  • +5
22 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10784073
VBscript is a client side scripting language.
You wont have permissions to save a file on client side.
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10784102
Also the object should objExcel  be installed on the client system
Or else you will get error.
0
 
LVL 1

Author Comment

by:jlininger
ID: 10784157
I am trying to save the file on the client side, for example, the script resides in an html on a server, I want to be able to open the html from any pc on the network using internet explorer, and have the vbs run and save the file on the client pc. Is there any way to accomplish this? Do I need to use something other than vbs?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10784212
Check this example. It creates a simple txt file...
Before writing you will get a security message.........

<SCRIPT LANGUAGE="VBScript">
<!--
dim filesys, filetxt, getname, path
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.CreateTextFile("c:\somefile.txt", True)
path = filesys.GetAbsolutePathName("c:\somefile.txt")
getname = filesys.GetFileName(path)
filetxt.WriteLine("Your text goes here.")
filetxt.Close
If filesys.FileExists(path) Then
  document.Write ("Your file, '" & getname & "', has been created.")
End If

//-->
</SCRIPT>
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10784233
Like I said the client system must have excel.
Or else it wont be able to create the object.

Ofcourse, even after that you will get atleast one security/warning message.
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10784237
One alternative is create a .csv (comma seperated file) file.
It is just a plain text file but the values are seperated by commas.
this file can be opened from excel.
0
 
LVL 1

Author Comment

by:jlininger
ID: 10784290
The client system does have excel. And the script works on the client system when the html resides on the client system, but when I move it up to the server and run it from there, it won't create the excel.application object. I also tried your example script above to create a txt file. Same problem, it runs fine when the script is on my pc. But when I move it up to the server and try to run it from there I get "cannot create activeX object "scripting.fileSystemObject"... Same thing as excel, either way, it's not creating the object. Any idea why?
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10784833
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10784874

Many systems will have their Filesystemobjects disabled for security reasons.
0
 
LVL 1

Author Comment

by:jlininger
ID: 10784987
Thanks for trying to help. I looked that document over and it doesn't really have anything to do with my problem.

... and the filesystemobject is not disabled. I can run the script on all of these pc's and it works fine. The problem is not with the script. The problem is when I copy the script over to another computer and try to run it from another computer.

Just to be clear on what I am trying to accomplish.... I have inserted vbscript into an html document, the vbscript is supposed to open excel (or even a txt file would be fine for now), enter some data, and then save the document. the script works great, I have tested it, and it even works fine when I insert the vbscript into an html document and open it with IE, but here's the problem. It only works if I open the html document with the same computer that it resides on. In other words, if I map a drive to some server and run the script from there, or if I try to open the html document from our web server the vbscript fails. I get an error saying the excel.application object could not be created.. Actually I have tried other things and I get an error when trying to create any COM object. Does anyone know why this is happening? Am I approaching this the wrong way by trying to use vbs. Is there some other way I can get an intranet page to open excel enter data and save the document on a client side pc?
0
 
LVL 16

Expert Comment

by:warturtle
ID: 10785127
Hi,

I think this would depend on the webserver rights, as to, if it can create any files on the server's own file system and/or client.

Regards,

Warturtle
0
 
LVL 10

Expert Comment

by:Kavar
ID: 10785856
have the user add your server as a trusted site, then try again
0
 
LVL 1

Author Comment

by:jlininger
ID: 10786106
warturtle,  yeah that makes sense. although, the script is failing at the point where it is trying to create the COM object "excel.application", and before it is even trying to save the file. However, I did check the permissions and made sure everything was correct. Also to eliminate file system permissions as the possible cause I temporarily allowed "everyone" full access to the entire file system on the client pc and still had the problem.

Maybe it's something with internet explorer permissions, because I can run the vbscript by itself from another pc and it works fine, it only fails when I insert it into an html doc and try to run it with IE. I've noticed some quirky things about adding vbscript to html, like when you try to use wscript.quit, or wscript.sleep it says "wscript object not found" you can only seem to use automation objects that you created a reference to.

Kavar, I can't add it as a trusted site because it's not secured, (not https).. but also a good idea.
0
 
LVL 10

Expert Comment

by:Kavar
ID: 10786861
yes you can, just uncheck the box for secure
0
 
LVL 1

Author Comment

by:jlininger
ID: 10786947
yeah, I realized that. Thanks =)  Adding the site as trusted fixed a cross domain problem I was having. But the vbscript is still erroring out when it tries to create the excel.application object. I figured out a semi-solution. Instead of inserting the script in the hrml, I just linked the script so someone clicks on it and it runs the script. They have to click "open" on the popup box but it's no big deal. Anyone know a way to keep the "open/save" prompt from popping up for a vbs? probably impossible. =) Thanks for all of the suggestions. I'll leave this open for a little while incase anyone has anything to add, then I'll split the points up. Thanks again!
0
 
LVL 2

Accepted Solution

by:
Ivan_Skrinjaric earned 250 total points
ID: 10790277
One more thing.

Under trusted sites / security click Custom Level and enable Initalizing and Scripting ActiveX controls that are not marked safe. That should enable createobject on client.

0
 
LVL 1

Author Comment

by:jlininger
ID: 10791068
Ivan, Your're totally right. Unsafe ActiveX controls were disabled, that's why it wouldn't create the excel object. I enabled it and the script is running fine in the browser, just the way I wanted it. The drawback is I'm going to have to change that setting for anyone that is going to be using it, but oh well. Do you by chance know how to "sign" a script to mark it as safe?
0
 

Expert Comment

by:TEAMnetwork Systems
ID: 13513441
You need to use group policy to configure the security settings for IE on the clients.  This is big change though if you haven't got your head around group policy.

Even for small organisations, group policy rocks.  The only downside is that *all* targeted clients get exactly the same settings and only those settings so there's not much room for customising individual browsers if this is required.  You can do this with policy inheritance but it gets messy real fast.
0
 
LVL 4

Expert Comment

by:icanhelp
ID: 13697470
Try using Server.CreateObject("Excel.Application"), instead of Createobject("Excel.Application").  Here is a link explaining.

http://www.4guysfromrolla.com/webtech/043099-1.shtml#postadlink

Please reward me with points.

thanks.
0
 

Expert Comment

by:WebmastuhB
ID: 13741112
jlininger,

I have written a number of webpages that have created Excel documents, but I have never had the specific need to use client side VB Script to specifically create the Excel Spreadsheet on the client site.

Is there a reason why you need to specifically run Excel on the client side?  It seems to me the problems you are having stem from permissions issues or security issues or trusted site issues.

I have found it easier to create an .asp page that runs server side (on IIS).  Usually I have the .asp page create a .csv file, save it to disk, and then redirect the end user to the created .csv file.  Its a great way to export data from a database with a web interface.

Also, I have had a few specific instances whereI needed to create a spreadsheet complete with data in four separate tabs.  My solution was to use the  Server.CreateObject("Excel.Application") mentioned by icanhelp, populate the data, save the speadsheet, and redirect to the file.  Its slow and cumbersome, but its faster than cutting and pasting by hand, and the end result is what I want.

Hope this helps.
0
 
LVL 4

Expert Comment

by:icanhelp
ID: 13752086
Did you distribute points yet?
0
 
LVL 1

Author Comment

by:jlininger
ID: 13754871
Yeah, I distributed the points when I closed it a year ago. Notice the date is 4/9/04?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question