.NET Framework Bookmark and Share   
 index > JScript for the .NET Framework > Microsoft jscript runtime error: Automation server can't create object
 

Microsoft jscript runtime error: Automation server can't create object

HI,

I am using VS.net 2005 on vista home premium. when i tred to create activex object in javascript it is showing the error "microsoft jscript runtime error: Automation server can't create object". I am giving the code snippet. Please go through and suggest me the solutions.

var fso = new ActiveXObject ('Scripting.SystemObject');

var fileobject = fso.OpenTextFile(Filename,1,true,0);

var line,i=0;

while((line=fileobject.ReadLine()) != null)

{

i++;

}

thanks&regards

sudheer.

sudheer Bondalapati

The first line should be:-

var fso = new ActiveXObject("Scripting.FileSystemObject");

Are you sure you want to force the creation of the file if it doesn't exist?

The code has a bug also where it will attempt to read past the end of the file and I'm not sure ReadLine will ever return a value that is equal to null anyway.

I suspect you are trying to count the number of lines in a file, correct?

Try this:-

Code Snippet

var Filename = "c:\\somefolder\\sometextfile";

//Returns number lines in a file and -1 if the file doesn't exist
function GetLineCount(filename)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");

var i = -1;

if (fso.FileExists(filename))
{
i = 0
var stream = fso.OpenTextFile(filename);

while (!stream.AtEndOfStream)
{
stream.ReadLine();
i++;
}
}

return i;
}

WScript.echo(GetLineCount(Filename));

Anthony Jones

The first line should be:-

var fso = new ActiveXObject("Scripting.FileSystemObject");

Are you sure you want to force the creation of the file if it doesn't exist?

The code has a bug also where it will attempt to read past the end of the file and I'm not sure ReadLine will ever return a value that is equal to null anyway.

I suspect you are trying to count the number of lines in a file, correct?

Try this:-

Code Snippet

var Filename = "c:\\somefolder\\sometextfile";

//Returns number lines in a file and -1 if the file doesn't exist
function GetLineCount(filename)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");

var i = -1;

if (fso.FileExists(filename))
{
i = 0
var stream = fso.OpenTextFile(filename);

while (!stream.AtEndOfStream)
{
stream.ReadLine();
i++;
}
}

return i;
}

WScript.echo(GetLineCount(Filename));

Anthony Jones
sudheer Bondalapati wrote:

HI,

I am using VS.net 2005 on vista home premium. when i tred to create activex object in javascript it is showing the error "microsoft jscript runtime error: Automation server can't create object". I am giving the code snippet. Please go through and suggest me the solutions.

var fso = new ActiveXObject ('Scripting.SystemObject');

var fileobject = fso.OpenTextFile(Filename,1,true,0);

var line,i=0;

while((line=fileobject.ReadLine()) != null)

{

i++;

}

thanks&regards

sudheer.

ter22

There is no geniune problem with the code the problem is with internet explorer browser security settings.
Generally you face this type of error when you want to open a text file or some excel files on a remote server
you can remove the problem but that's not highly appreciated ,try to do the following and i think your error will be removed

go to internetoptions<security<customlevel<initialize and script active x controls not marked safe for scripting and mark them enabled and i think your problem will be removed


Beni
  • Proposed As Answer byAjay Beni Wednesday, June 11, 2008 1:21 PM
  •  
Ajay Beni
Ajay Beni said:

There is no geniune problem with the code the problem is with internet explorer browser security settings.
Generally you face this type of error when you want to open a text file or some excel files on a remote server
you can remove the problem but that's not highly appreciated ,try to do the following and i think your error will be removed

go to internetoptions<security<customlevel<initialize and script active x controls not marked safe for scripting and mark them enabled and i think your problem will be removed


Beni



That advice should come with with a "You are putting your computer at serious risk" health warning. My advice is don't do this, that security setting is there for a very good reason.

Sudheer, do you want client side browser host javascript to read a local file? If so why? Can you re-design to avoid it? The only way to do this is to use the above advice. IMO opinion the risks are to high even for personal use and its not something you can ask users to do.

Anthony Jones - MVP ASP/ASP.NET
Anthony Jones
Thanks, now works fine, for me is a good solution.
msp.netdev

You can use google to search for other answers

Custom Search

More Threads

• delete XML node with JavaScript/JScript
• Send variable to wsf file
• Accessing a Custom Control via JavaScript
• IE WebControls Treeview: manual postback?
• How Can I Set PageSize property In Page Printing with CSS
• print() works in IE7 but not in IE6
• I want to refresh the parent window
• Server side XMLHttpRequest sequential usages of same object
• Limiting characters displayed from an array
• how to display Time on page in asp.net 2.0 without using AJAX Control.