What is the purpose of objFSO?

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

What is the purpose of objFSO?

Post by Rudi »

Hi,

In the code below, what is the purpose of the object objFSO?
Do I need it if all I require is the path selected by the user so I can save the file. (I got this code somewhere and I've just used it as is, but in stepping through the code I see that it really just assigns the save string of strFolderPath to objFolder? What is the reason for this??
IOW: Can I scrap the entire Set objFSO in the red square and in the code above the red line just say: Filename:=strFolderPath & "\" & FName?
SP3-Tue,27-8.jpg
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: What is the purpose of objFSO?

Post by HansV »

I can't tell whether objFile is used further down, but in the part of the code that you posted, there is no need to use objFSO and objFolder. You could indeed use

NewBook.SaveAs FileName:=strFolderPath & "\" & FName
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: What is the purpose of objFSO?

Post by Rudi »

HansV wrote:I can't tell whether objFile is used further down, but in the part of the code that you posted, there is no need to use objFSO and objFolder. You could indeed use

NewBook.SaveAs FileName:=strFolderPath & "\" & FName
No, its not used further down. Just for the saving path.
Could you tell me what is the CreatObject("Scripting.FileSystemObject")? What is its purpose in the original code that I got...as attached below?
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: What is the purpose of objFSO?

Post by HansV »

FileSystemObject is an object from VBScript that presents an interface to the file system (drives, folders and files). It also lets you manipulate text files.
Since it is not included in VBA, you need to create an instance of FileSystemObject to work with it.
In the code from your text file, the line

Code: Select all

    Set objFolder = objFSO.GetFolder(strFolderPath & "\")
creates a folder object that refers to the folder whose path is strFolderPath.
objFolder.Files is the collection of all files in this folder.
The code loops through these files and lists their names in a worksheet.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: What is the purpose of objFSO?

Post by Rudi »

Ah, so properties of the Folder object are being used in the attached file code, hence the need for an object definition.
In my code, I just need a string for the path to the save folder. I do not need properties of the folder object, so hence no need to have it in the code. (Phew! - got it!)
TX :thumbup:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.