0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EXCEL VBA:ファイルの保存場所がローカルディスクかどうか判別する

Last updated at Posted at 2020-08-17

ファイルの保存場所をチェックするマクロのメモ書きです。

Option Explicit
'動作テスト用
Sub CallCheckLocalDisk()

    Dim filePath As String: filePath = ThisWorkbook.Path
    Dim isLocalDrive As Boolean: isLocalDrive = CheckLocalDisk(a_filePath:=filePath)
    
    If isLocalDrive Then
        MsgBox ("ファイルの保存場所はローカルディスクです。")
    Else
        MsgBox ("ファイルの保存場所はローカルディスクではありません。")
    End If
    
End Sub

'ファイルの保存されている場所がローカルディスクかどうか判別してbool値を返す。
Function CheckLocalDisk(ByVal a_filePath As String) As Boolean
    
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim driveName As String: driveName = fso.GetDriveName(a_filePath)
    Dim driveType As Long
        
    'ドライブの種類を判別
    If driveName = "" Then
        driveType = 0 '不明
    Else
        driveType = fso.GetDrive(driveName).driveType
    End If
    
    'ローカルディスクの場合True、それ以外はFalseを返す
    Select Case driveType
        Case 1: CheckLocalDisk = True 'リムーバブルディスク
        Case 2: CheckLocalDisk = True 'ハードディスク
        Case Else: CheckLocalDisk = False '不明、ネットワークドライブ、CDドライブなど
    End Select
        
End Function
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?