VBA macro to obtain file names using WIN32 API

M

Mudd

I have a problem trying to get an Excel VBA macro to use a Win32 API
function.
In the macro, I defined the variable structBuffer in the Declarations
section as;
Type WIN32_FIND_FILE_DATA
FileAttributes As Long
CreationTime As String
AccessTime As String
WriteTime As String
SizeHigh As Long
SizeLow As Long
Reserved0 As Long
Reserved1 As Long
FileName As String
FileAltName As String * 14
End Type
to match the Win32 struct which is defined as;
typedef struct _WIN32_FIND_DATA { // wfd
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
TCHAR cFileName[ MAX_PATH ];
TCHAR cAlternateFileName[ 14 ];
} WIN32_FIND_DATA;

and I defined the Win32 API function FindFirstFile() like this;
Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" (ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_FILE_DATA) _
As Long

I define the structBuffer variable like this;
Dim stuctBuffer As WIN32_FIND_FILE_DATA

I have these two lines in the macro.
strFullPath = strPath & "\*.*"
lnHandle = FindFirstFile(strFullPath, structBuffer)

strPath is the drive and directory path to the Temporary Internet Files
directory (that holds cookies, shortcuts, etc).

When I try to run the macro, it flags this line
lnHandle = FindFirstFile(strFullPath, structBuffer)
as a Compile error: ByRef argument type mismtach as it highlights
structBuffer.
I don't understand what I have done wrong. I'm sure I'm on the right track
here so hopefully it's something quite simple.
I have been using the VBA Dir() function to obtain file names in other
directories but the Temporary Internet Files directory returns nothing to
the Dir() function. A DIR command at the command prompt in this directory
also shows nothing, but Windows Explorer is quite capable of listing all the
cookies, URLs etc that fill up this directory.
This is why I am trying to use the Win32 API instead of the VBA functions.
 
B

Bob Phillips

According to my reading of the typedef structure, the

Type WIN32_FIND_FILE_DATA
FileAttributes As Long
CreationTime As String
AccessTime As String
WriteTime As String
SizeHigh As Long
SizeLow As Long
Reserved0 As Long
Reserved1 As Long
FileName As String
FileAltName As String * 14
End Type

should be

Type WIN32_FIND_FILE_DATA
FileAttributes As Long
CreationTime As FILETIME
AccessTime As FILETIME
WriteTime As FILETIME
SizeHigh As Long
SizeLow As Long
Reserved0 As Long
Reserved1 As Long
FileName As String
FileAltName As String * 14
End Type

with FILETIME defined as another structure aka

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

Mudd said:
I have a problem trying to get an Excel VBA macro to use a Win32 API
function.
In the macro, I defined the variable structBuffer in the Declarations
section as;
Type WIN32_FIND_FILE_DATA
FileAttributes As Long
CreationTime As String
AccessTime As String
WriteTime As String
SizeHigh As Long
SizeLow As Long
Reserved0 As Long
Reserved1 As Long
FileName As String
FileAltName As String * 14
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
Accordin cAlternate As String * 14
End Type
to match the Win32 struct which is defined as;
typedef struct _WIN32_FIND_DATA { // wfd
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
TCHAR cFileName[ MAX_PATH ];
TCHAR cAlternateFileName[ 14 ];
} WIN32_FIND_DATA;

and I defined the Win32 API function FindFirstFile() like this;
Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" (ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_FILE_DATA) _
As Long

I define the structBuffer variable like this;
Dim stuctBuffer As WIN32_FIND_FILE_DATA

I have these two lines in the macro.
strFullPath = strPath & "\*.*"
lnHandle = FindFirstFile(strFullPath, structBuffer)

strPath is the drive and directory path to the Temporary Internet Files
directory (that holds cookies, shortcuts, etc).

When I try to run the macro, it flags this line
lnHandle = FindFirstFile(strFullPath, structBuffer)
as a Compile error: ByRef argument type mismtach as it highlights
structBuffer.
I don't understand what I have done wrong. I'm sure I'm on the right track
here so hopefully it's something quite simple.
I have been using the VBA Dir() function to obtain file names in other
directories but the Temporary Internet Files directory returns nothing to
the Dir() function. A DIR command at the command prompt in this directory
also shows nothing, but Windows Explorer is quite capable of listing all the
cookies, URLs etc that fill up this directory.
This is why I am trying to use the Win32 API instead of the VBA functions.
 
M

Mudd

I have made the change in the typedef as described but I still get the
compile error for the structBuffer argument.

Bob Phillips said:
According to my reading of the typedef structure, the

Type WIN32_FIND_FILE_DATA
FileAttributes As Long
CreationTime As String
AccessTime As String
WriteTime As String
SizeHigh As Long
SizeLow As Long
Reserved0 As Long
Reserved1 As Long
FileName As String
FileAltName As String * 14
End Type

should be

Type WIN32_FIND_FILE_DATA
FileAttributes As Long
CreationTime As FILETIME
AccessTime As FILETIME
WriteTime As FILETIME
SizeHigh As Long
SizeLow As Long
Reserved0 As Long
Reserved1 As Long
FileName As String
FileAltName As String * 14
End Type

with FILETIME defined as another structure aka

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

Mudd said:
I have a problem trying to get an Excel VBA macro to use a Win32 API
function.
In the macro, I defined the variable structBuffer in the Declarations
section as;
Type WIN32_FIND_FILE_DATA
FileAttributes As Long
CreationTime As String
AccessTime As String
WriteTime As String
SizeHigh As Long
SizeLow As Long
Reserved0 As Long
Reserved1 As Long
FileName As String
FileAltName As String * 14
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
Accordin cAlternate As String * 14
End Type
to match the Win32 struct which is defined as;
typedef struct _WIN32_FIND_DATA { // wfd
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
TCHAR cFileName[ MAX_PATH ];
TCHAR cAlternateFileName[ 14 ];
} WIN32_FIND_DATA;

and I defined the Win32 API function FindFirstFile() like this;
Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" (ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_FILE_DATA) _
As Long

I define the structBuffer variable like this;
Dim stuctBuffer As WIN32_FIND_FILE_DATA

I have these two lines in the macro.
strFullPath = strPath & "\*.*"
lnHandle = FindFirstFile(strFullPath, structBuffer)

strPath is the drive and directory path to the Temporary Internet Files
directory (that holds cookies, shortcuts, etc).

When I try to run the macro, it flags this line
lnHandle = FindFirstFile(strFullPath, structBuffer)
as a Compile error: ByRef argument type mismtach as it highlights
structBuffer.
I don't understand what I have done wrong. I'm sure I'm on the right track
here so hopefully it's something quite simple.
I have been using the VBA Dir() function to obtain file names in other
directories but the Temporary Internet Files directory returns nothing to
the Dir() function. A DIR command at the command prompt in this directory
also shows nothing, but Windows Explorer is quite capable of listing all the
cookies, URLs etc that fill up this directory.
This is why I am trying to use the Win32 API instead of the VBA functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top