VBA FileSystemObject(FSO)| FileSystemObjectにアクセスする方法は?

Excel VBA FileSystemObject(FSO)

VBA FileSystemObject (FSO)はFileDialogと同様に機能し、作業中のコンピューターの他のファイルにアクセスするために使用されます。これらのファイルを編集することもできます。つまり、ファイルの読み取りまたは書き込みを行います。FSOを使用すると、ファイルにアクセスしたり、ファイルを操作したり、ファイルやフォルダーを変更したりできます。FSOは、VBAでアクセスできる重要なAPIツールです。VBAプロジェクトの一環として、作業を完了するために、コンピューター内のいくつかのフォルダーとファイルにアクセスする必要がある場合があります。

FSOを使用すると、「フォルダーが使用可能かどうかを確認する」、新しいフォルダーを作成する、既存のフォルダーの名前を変更する、フォルダー内のすべてのファイルのリストとサブフォルダー名を取得する、などの多くのタスクを実行できます。最後に、ある場所から別の場所にファイルをコピーできます。

フォルダやファイルを操作するために利用できる他の関数がありますが、FSOは、VBAコードをきちんとまっすぐに保つことにより、フォルダやファイルを操作する最も簡単な方法です。

FileSystemObjectを使用して4種類のオブジェクトにアクセスできます。以下はそれらです。

  1. ドライブ:このオブジェクトを使用して、言及されたドライブが存在するかどうかを確認し、パス名、ドライブのタイプ、およびドライブのサイズを取得できます。
  2. フォルダ:このオブジェクトを使用すると、特定のフォルダが存在するかどうかを確認できます。このオブジェクトを使用して、フォルダを作成、削除、変更、コピーできます。
  3. ファイル:このオブジェクトを使用すると、特定のファイルが存在するかどうかを確認できます。このvbaオブジェクトを使用して、ファイルを作成、削除、変更、コピーできます。
  4. テキストストリーム:このオブジェクトを使用すると、テキストファイルを作成または読み取ることができます。

上記のすべてのメソッドには、使用する独自のメソッドがあります。要件に基づいて、各オブジェクトのメソッドを選択できます。

FileSystemObjectを有効にする方法は?

VBAでは簡単にアクセスできません。ファイルやフォルダーへのアクセスはExcelの外部タスクであるため、FileSystemObjectを有効にする必要があります。有効にするには、以下の手順に従います。

ステップ1:[ツール]> [参照]に移動します。

ステップ2–「MicrosoftScriptingRuntime」オプションを選択します

下にスクロールして、[MicrosoftScriptingRuntime]オプションを選択します。オプションを選択したら、[OK]をクリックします。

これで、vbaのFileSystemObject(FSO)にアクセスできます。

FileSystemObjectのインスタンスを作成する

オブジェクトライブラリから[MicrosoftScripting Runtime]オプションを有効にしたら、コーディングによってファイルシステムオブジェクト(FSO)のインスタンスを作成する必要があります。

インスタンスを作成するには、最初に変数をFileSystemObjectとして宣言します。

ご覧のとおり、FileSystemObjectはVBAのIntelliSenseリストに表示されています。「MicrosoftScriptingRuntime」を有効にするまでは、これは利用できませんでした。

FSOはオブジェクトであるため、新しいインスタンスを作成するために設定する必要があります。

これで、FSO(FileSystemObject)のすべてのオプションにアクセスできます。

VBAFileSystemObjectの使用例

このVBAFileSystemObject Excelテンプレートはここからダウンロードできます– VBA FileSystemObjectExcelテンプレート

例1-総ドライブスペースを見つける

以下のコードは、ドライブの総容量を示しています。

コード:

 Sub FSO_Example1()Dim MyFirstFSO As FileSystemObject Set MyFirstFSO = New FileSystemObject Dim DriveName As Drive Dim DriveSpace As Double Set DriveName = MyFirstFSO.GetDrive( "C:") '新しいドライブオブジェクトを作成するDriveSpace = DriveName.FreeSpace'これにより空き領域が取得されますドライブ "C"のDriveSpace = DriveSpace / 1073741824 'これにより、空き領域がGBに変換されますDriveSpace = Round(DriveSpace、2)'合計領域を丸めま​​すMsgBox "Drive"&DriveName& "has"&DriveSpace& "GB" Endサブ 

コードの内訳。

まず、FSOのインスタンスを作成しました。

 Dim MyFirstFSO As FileSystemObject Set MyFirstFSO = New FileSystemObject

Next, we have declared two variables.

 Dim DriveName As Drive Dim DriveSpace As Double 

Since DriveName is an Object variable we need to set this to FSO one of the FSO method. Since we need the characteristic of the drive we have used Get Drive option and mentioned the drive name

 Set DriveName = MyFirstFSO.GetDrive("C:")

Now for another variable DriveSpace, we will assign the free space method of the drive we are accessing.

DriveSpace = DriveName.FreeSpace

As of now, the above equation can get us free space of the drive “C”. So to show the result in GB we have divided the free space by 1073741824

DriveSpace = DriveSpace / 1073741824

Next, we will round the number.

DriveSpace = Round(DriveSpace, 2)

Finally, show the result in Message Box.

MsgBox "Drive " & DriveName & " has " & DriveSpace & "GB"

When we run the code manually or through shortcut key F5, then in message box we will get the free space of the drive “C”.

So, in my computer Drive C has 216.19 GB of free space memory.

Example #2 – Check Whether the Folder Exists or Not

To check whether the particular folder exists or not use the below code.

If the mentioned folder is available then it will show us the message box as “The Mentioned Folder is Available”, if not it will show the VBA message box as “The Mentioned Folder is Not Available”.

Code:

 Sub FSO_Example2() Dim MyFirstFSO As FileSystemObject Set MyFirstFSO = New FileSystemObject If MyFirstFSO.FolderExists("D:\Excel Files\VBA\VBA Files") Then MsgBox "The Mentioned Folder is Available" Else MsgBox "The Mentioned Folder is Not Available" End If End Sub 

Run this code through the excel Shortcut key F5 or manually, then see the result.

Example #3 – Check Whether the File Exists or Not

Below code will check whether the mentioned file is available or not.

Code:

 Sub FSO_Example3() Dim MyFirstFSO As FileSystemObject Set MyFirstFSO = New FileSystemObject If MyFirstFSO.FileExists("D:\Excel Files\VBA\VBA Files\Testing File.xlsm") Then MsgBox "The Mentioned File is Available" Else MsgBox "The Mentioned File is Not Available" End If End Sub 

Run this code manually or using the F5 key, then see the result.