Quality Testing

Quality is delighting customers

 

1.I have created a text file "srinivas".

2.I save that text file in  "E" drive ( "E:\srinivas")

3.I open the taxt file .

 

and I wrote this code

 

Set fso = CreateObject("Scripting.FileSystemObject")

Set MyFile = fso.getFile("e:\srinivas.txt")

 

MyFile.Close

 

 

But I am unable to close the file ....and getting error.

 

 

can any one correct  my code and give the suggetions

Views: 165

Reply to This

Replies to This Discussion

Hi Srinivas,

 

You can close notepad window with below line of code:

Window("text:=test1.txt - Notepad").Close        'test1 is the filename

 

Thanks,

Aruna

What is the error you are getting?

You have not opened the file, you "got" the file. I don't believe the file is in a state where .close() can be executed successfully.

 

If you run the following code, you will get an error...

CreateAFile()
Sub CreateAfile()
   Dim fso, MyFile
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set MyFile = fso.GetFile("C:\Users\boniface\Documents\testfile.txt")
   MyFile.WriteLine("This is a different test.")
   MyFile.Close()
End Sub

 

You can't execute open or write against the object you retrieved with the GetFile command. You can do this...

CreateAFile()
Sub CreateAfile()
   Dim fso, MyFile
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set MyFile = fso.CreateTextFile("C:\Users\boniface\Documents\testfile.txt", True)
   MyFile.Close()
   Set MyFile = fso.OpenTextFile("C:\Users\boniface\Documents\testfile.txt", 8, true)  
   MyFile.WriteLine("This is a different test.")
   MyFile.Close()
End Sub

 

If you don't close the file after the create, you can't execute an open command. You do not need the close and open lines (5 and 6 of the method) to do the writes. It is just an example so you see you can't open a file that's already open. And, you can't write to files that are not yet open.

 

This also works just fine...

CreateAFile()
Sub CreateAfile()
   Dim fso, MyFile
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set MyFile = fso.CreateTextFile("C:\Users\boniface\Documents\testfile.txt", True)
   MyFile.WriteLine("This is a different test.")
   MyFile.Close()
End Sub

 

Here is a good resource...

http://msdn.microsoft.com/en-us/library/6tkce7xa(v=VS.85).aspx

 

Set fso = CreateObject("Scripting.FileSystemObject")

Set MyFile = fso.opentextfile("e:\srinivas.txt")

 MyFile.Close

 

u r using getfile() function, but u r accessing to close da window,how will u get file close if u not open da file

1st u have to open da file then close

to open u have to use this statement;

Set MyFile = fso.opentextfile("e:\srinivas.txt")

then close file

MyFile.Close

 

There is one more efficient way.

Use Desktop.ChildObjects and extract only Notepad APP then close it.

 

Subbu.

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2021   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service