tags:

views:

244

answers:

3

Hi, I am realkly new to vbscripting.So this is my code below which reates a text file and attaches an object to it

        Set objExcel = CreateObject("Scripting.FileSystemObject")
        objExcel.CreateTextFile ("C:\mine.txt")

           Now Can anyone please tel me how am i supposed to use the

getObject(Pathname,[class]) function as i was trying a lot but nothing worked out well.

         Thanks and regards
         Maddy
A: 

Read this artical CreateObject Vs. GetObject

Tester101
@Tester,thanks for that input,but i had read that link before and just tried the same type of work which had been done for Excel repeated the same with .txt file ,but i am just getting some errors
What errors are you getting?
Tester101
"ActiveX component can't create object" is the error i am getting.How am i supposed top use this getObject in terms of Scripting.FileSystemObject.Is it like GetObject(,"Scripting.FileSystemObject")??
I'm not sure if it can be used with Scripting.FileSystemObject I've never seen it done. What are you trying to accomplish?
Tester101
A: 

@Tester, Since i am just a beginner ,i was trying with a lot of functions in VBscripts.I had seen from the net that GetObject() being used with excel sheets,just wann try with .txt files the same way.Any solution??

+2  A: 

VBScript GetObject documentation can be found here. Here is a VBScript sample:

Set objExcelFile = GetObject("C:\Scripts\Test.xls")
WScript.Echo objExcelFile.Name
objExcelFile.Close

This code will get you the Excel Workbook object contained in C:\Scripts\Test.xls. You can use TypeName() to confirm that:

Set objExcelFile = GetObject("C:\Scripts\Test.xls")
WScript.Echo objExcelFile.Name
WScript.Echo TypeName(objExcelFile)
objExcelFile.Close

The output will be:

test.xls
Workbook

If the specified Excel workbook doesn't exist the script will return an error. If an instance of Excel is already running you can use this code to get a reference to it:

Set objExcel = GetObject( , "Excel.Application")

For Each objWorkbook In objExcel.Workbooks
    WScript.Echo objWorkbook.Name
Next

objExcel.Quit

Notice the comma before 'Excel.Application'. The script gets a reference to a running Excel application, lists open workbooks names and quits Excel. If there is no Excel instance running you will receive an error. This is also why you couldn't use GetObject() to get an instance of Scripting.FileSystemObject - there was no running instance.

You could use GetObject if there is an instance og Scripting.FileSystemObject already running in memory. Try this code:

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFso1 = GetObject("", "Scripting.FileSystemObject")

WScript.Echo TypeName(objFso)
WScript.Echo TypeName(objFso1)

You can see that you first need to use CreateObject() to and when FileSystemObject is running it is possible to get a reference to it, but this doesn't make much sense because you already have a reference to it (objFso). So, use CreateObject() to create an instance of FileSystemObject.

Uros Calakovic