VBA and WMI

bfreescott

Board Regular
Joined
Aug 6, 2015
Messages
115
Hi guys

So, like many, my goal is to try and determine the name of a network user who has an excel file on a mapped network drive currently open.

I've chased many rabbits on this and have come to what I hope is a solution using the WMI library, but I'm getting an invalid path error with my function.

Has anyone had success with WMI?

Code:
Function GetFileOwner(strFileName)
'
    Set objWMIService = GetObject("winmgmts:")
    Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFileName & "'")
    intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)

    If intRetVal = 0 Then
       GetFileOwner = objSD.Owner.Name
    Else
       GetFileOwner = "Unknown"
    End If
'
End Function
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Are you sure you are passing the correct full path and name of the file ?
 
Upvote 0
yes, in my rush I forgot to mention I am passing the full path including mapped drive and file extension.
 
Upvote 0
And let me add since I've seen it as an issue with vbs that my path does contain single quotes. Not sure if that is causing the error.
 
Upvote 0
That is indeed what the issue was, so in case anyone else has the same issue with Excel and WMI while trying to ID the user, make sure you pass the full path and use the following if your path contain single quotes:

Code:
Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting=""" & Replace(strFileName, "\", "\\") & """")
 
Upvote 0
I tried your code but no luck. Were you able to get this done with unc path? I was successful in getting it done with map drives but was looking to use unc path.

I have tried both of these:

'objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFileName & "'")

Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting=""" & Replace(strFileName, "") & """")


I was able to get it to work with map drive but was looking to do it unc.

Dim mypath As String


'testWorkbookLockFile = "L:\~$filename.xlsx"
mypath = "\\1.2.3.4\SharedFolder\~$filename.xlsx"
 
Upvote 0
I tried your code but no luck. Were you able to get this done with unc path? I was successful in getting it done with map drives but was looking to use unc path.

I have tried both of these:

'objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFileName & "'")

Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting=""" & Replace(strFileName, "") & """")


I was able to get it to work with map drive but was looking to do it unc.

Dim mypath As String


'testWorkbookLockFile = "L:\~$filename.xlsx"
mypath = "\\1.2.3.4\SharedFolder\~$filename.xlsx"



If you look at my function, you'll notice I had to use a Replace function to change any single forward slash in the path string to a double forward slash.
 
Upvote 0
hmmm not sure what I'm missing..I think I've tried it with your function as well and says it cant be found but when you mouse over strFileName in debug it appears its getting the whole path. here is the code I'm using maybe you can see it better than me lol. Thanks for your help!

Sub Test()


Dim mypath As String


'testWorkbookLockFile = "L:\~$FileName.xlsx"
mypath = "\\IP\FolderShare\~$FileName.xlsx"
Set objFSO = CreateObject("Scripting.FileSystemObject")


If objFSO.FileExists(mypath) Then


Set TxtRng = ActiveWorkbook.Sheets("Sheet1").Cells(1, 1)

TxtRng.Value = "The file is locked by " & GetFileOwner(mypath)
Else
Set TxtRng = ActiveWorkbook.Sheets("Sheet1").Cells(1, 1)
TxtRng.Value = "The file is available"
End If
End Sub


Function GetFileOwner(strFileName)

Set objWMIService = GetObject("winmgmts:")
'Set objFileSecuritySettings = _
'objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFileName & "'")

Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting=""" & Replace(strFileName, "", "") & """")
intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)


If intRetVal = 0 Then
GetFileOwner = objSD.Owner.Name
Else
GetFileOwner = "Unknown"
End If




End Function
 
Upvote 0

Forum statistics

Threads
1,215,364
Messages
6,124,507
Members
449,166
Latest member
hokjock

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top