Find author of file (1 Viewer)

aziz rasul

Active member
Local time
Today, 10:13
Joined
Jun 26, 2000
Messages
1,935
I get error "Invalid procedure call or argument" in the following code as I try to obtain the author of strFile which can any file type.

Code:
Public Function GetAuthorofFile(strFile As String) As String

    Dim securityUtility As Object
    Dim securityDescriptor As Object
    
    Set securityUtility = CreateObject("ADsSecurityUtility")
    Set securityDescriptor = securityUtility.GetSecurityDescriptor(strFile, 1, 1) 'ERROR HERE
    GetAuthorofFile = securityDescriptor.Owner
    
End Function
 

BeeJayEff

Registered User.
Local time
Today, 02:13
Joined
Sep 10, 2013
Messages
198
Confess I really don't understand what you're trying to do here, but the specification of CreateObject says : "The class argument uses the syntax appname.objecttype".
And I think you might mean IADsSecurityUtility ?
 

sneuberg

AWF VIP
Local time
Today, 02:13
Joined
Oct 17, 2014
Messages
3,506

aziz rasul

Active member
Local time
Today, 10:13
Joined
Jun 26, 2000
Messages
1,935
If I change the code to

Code:
Public Function GetAuthorofFile(strFile As Variant) As String

then I get no error but it gives me the same author i.e. "BUILTIN\Administrators" rather than the actual author that appears in Windows Explorer.
 

sneuberg

AWF VIP
Local time
Today, 02:13
Joined
Oct 17, 2014
Messages
3,506
Edit: Actually this thread has better ideas and I think what you are looking for.


I found some code near the end of this thread and modified it a bit to come up with

Code:
Sub GetAuthor()

Dim sFile As Variant
Dim oShell: Set oShell = CreateObject("Shell.Application")
Dim oDir:   Set oDir = oShell.Namespace("C:\Users\sneuberg\Documents\Excel") 'Replace with desire folder path
Dim i As Long
For Each sFile In oDir.Items
    Debug.Print sFile & "    "; oDir.GetDetailsOf(sFile, 20)
Next
End Sub

When I run this code I get the file names and authors in the folder "C:\Users\sneuberg\Documents\Excel". Note that Excel and Word files have authors but many other types of file don't. Anyway this looks more like what you have been looking for.
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:13
Joined
Feb 28, 2001
Messages
27,194
Actually, I'm surprised this approach works at all. The owner of a file (in a shared folder) is usually the owner of the folder or the System account, one or the other, and depending on just how tight the domain setup is crafted. Could also be a member of a group that has Modify rights to the folder. The owner of a file in Windows terms usually depends on just how the author got the file there - via privilege, group membership, or individual permissions.

The author of the file (to my admittedly sketchy memory) is found by opening the file via the application object and asking it for information on the author of the file.

For Word: Once you have the document open, you have to visit the Comments collection, of which Author is a labeled property; or you might try collection CoAuthors (in which collection there might only be one coauthor... the original author). Because the original author can allow others the right to update a document, the CoAuthors collection MIGHT have more than one entry. From the GUI, this is often referred to using document Collaboration.

For Excel: Once you have the workbook open and have selected a worksheet, there is a .Comments class that is part of the document, one element of which is the string .Author (used as ActiveSheet.Comments.Author, I believe).

You'd have to look up any other utility like PowerPoint or OneNote, but I'm sure the great Google-Brain can help with them.
 

static

Registered User.
Local time
Today, 10:13
Joined
Nov 2, 2015
Messages
823
Code:
Sub test()
    Const fol As String = "C:\"
    Const fil As String = "DiabloSwingOrchestra-PandorasPinata.jpg"

    Debug.Print "name        ", fnGetDetailsOfVB(fol, fil, GetPropByName(fol, fil, "name"))
    Debug.Print "owner        ", fnGetDetailsOfVB(fol, fil, GetPropByName(fol, fil, "owner"))
    Debug.Print "authors      ", fnGetDetailsOfVB(fol, fil, GetPropByName(fol, fil, "authors"))
    Debug.Print "author       ", fnGetDetailsOfVB(fol, fil, GetPropByName(fol, fil, "author"))
    Debug.Print "size         ", fnGetDetailsOfVB(fol, fil, GetPropByName(fol, fil, "size"))
    Debug.Print "date modified", fnGetDetailsOfVB(fol, fil, GetPropByName(fol, fil, "date modified"))
    Debug.Print "type         ", fnGetDetailsOfVB(fol, fil, GetPropByName(fol, fil, "type"))
    Debug.Print "rating       ", fnGetDetailsOfVB(fol, fil, GetPropByName(fol, fil, "rating"))
    Debug.Print "camera maker ", fnGetDetailsOfVB(fol, fil, GetPropByName(fol, fil, "camera maker"))

End Sub

Private Function fnGetDetailsOfVB(fpath As String, fname As String, prop As Integer) As String
'stolen from https://msdn.microsoft.com/en-us/library/windows/desktop/bb787870(v=vs.85).aspx
    If prop < 0 Then
        fnGetDetailsOfVB = "property not found"
        Exit Function
    End If
    Dim objShell, objFolder, objFolderItem
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(CVar(fpath))

    If (Not objFolder Is Nothing) Then
        Set objFolderItem = objFolder.ParseName(fname)
        If (Not objFolderItem Is Nothing) Then
            fnGetDetailsOfVB = objFolder.GetDetailsOf(objFolderItem, prop)
            Set objFolderItem = Nothing
        End If

    End If
    
    Set objFolder = Nothing
    Set objShell = Nothing
End Function

Private Function GetPropByName(fpath As String, fname As String, prop As String) As Integer
    Dim objFolder
    Set objFolder = CreateObject("Shell.Application").Namespace(CVar(fpath))
    GetPropByName = -1
    If (Not objFolder Is Nothing) Then
        For i = 0 To 300 'how to check #items???
            s = objFolder.GetDetailsOf(fname, i)
            If StrComp(s, prop, vbTextCompare) = 0 Then
                GetPropByName = i
                Exit Function
            End If
        Next
    End If
End Function


Code:
name          DiabloSwingOrchestra-PandorasPinata.jpg
owner         Administrators
authors       bob; fred; bill
author        property not found
size          512 KB
date modified 27/01/2017 10:24
type          JPEG image
rating        4 Stars
camera maker  polaroid
 

sneuberg

AWF VIP
Local time
Today, 02:13
Joined
Oct 17, 2014
Messages
3,506
@static I alway use Option Explicit. To get your code to compile I added
Code:
 Dim i As Long
 Dim s As Variant

to the GetPropByName function. Otherwise the code worked fine on Windows 7, Access 2013.
 

static

Registered User.
Local time
Today, 10:13
Joined
Nov 2, 2015
Messages
823
That's good practice.

I would probably have just removed the s var anyway after testing since it's only used once

If StrComp(objFolder.GetDetailsOf(fname, i), prop, vbTextCompare) = 0 Then

but variables are easier to debug.
 

Users who are viewing this thread

Top Bottom