VBA Dir () function

  • Thread starter Thread starter Edward
  • Start date Start date
E

Edward

Hi every one,
I use the following code to find and populate a combobox with *.ppt files

myfile=Dir(file pathe\+"*.ppt")

but this return not only *.ppt file but also *.pptx file !
Anything I'm missing here?
Thanks for help.
 
I believe that is by default. because you used *.ppt it will return all
files where the file extension begins with ppt.
 
=?Utf-8?B?RWR3YXJk?= said:
Hi every one,
I use the following code to find and populate a combobox with *.ppt
files

myfile=Dir(file pathe\+"*.ppt")

but this return not only *.ppt file but also *.pptx file !
Anything I'm missing here?

This is surprising. Have you checked how your code works with other
suffixes, for example does "*.do" also return .doc files? If it does,
your version of VBA (and/or Windows) has a bug.

But if you have Office version 2007 or later, this could as well be a
deliberate kludge to get PPT macros to list all PowerPoint files.

Or it could be just a random bug in your macro, triggered by the phase
of the moon or day of the month...

You ask if you are missing something. One thing is this: being ready for
_anything_ when writing Office macros! No matter how the code works,
don't be surprised, don't get upset, just work around it.

This one is easy to handle. Just add a check for what was returned:
if myfile <> "" then
if lcase(right(myfile, 4)) = ".pptx" then ... or something
similar...
 
Edward said:
Hi every one,
I use the following code to find and populate a combobox with *.ppt files

myfile=Dir(file pathe\+"*.ppt")

Okay, first off, use & to concatenate strings, rather than +. It'll
save you grief down the road.
but this return not only *.ppt file but also *.pptx file !
Anything I'm missing here?

Yeah, you'll find the same thing happens if you drop to a command
window. There's some ancient plumbing at work in Windows, from back in
the day when 3 chars was the max for extensions. You really always
need to post-process Dir results, to be sure they are what you expect.
For example, if you use a ".*" extension, you'll also get directories!
 
Thanks. Now that I'm sure it wasn't from my side , I can go ahead and remove
*.potx files.

By the way if you use myfile=Dir(file pathe\+"*.pptx") it will only return
pptx file and not ppt files .

However I think this is an ugly defect in Dir() function, and MS should fix
this.
Best regards,
Edward
 
Matti said:
This is surprising. Have you checked how your code works with other
suffixes, for example does "*.do" also return .doc files? If it does,
your version of VBA (and/or Windows) has a bug.

No, it's just the way Windows has been, ever since Win95. Actually, NT
3.5x probably acted this way too, but I'm too lazy to test. Try this:

D:\Docs\PowerPoint>copy con temp.pptx
adsf
^Z
1 file(s) copied.

D:\Docs\PowerPoint>dir *.ppt
Volume in drive D has no label.
Volume Serial Number is FA0F-40AB

Directory of D:\Docs\PowerPoint

02/23/2010 03:36 PM 82,432 CitizenCommunications.ppt
08/01/2000 08:28 AM 25,600 pptEvent.ppt
03/03/2010 03:17 PM 6 temp.pptx
3 File(s) 108,038 bytes
0 Dir(s) 739,961,786,368 bytes free

D:\Docs\PowerPoint>

(I had to actually create a file with the PPTX extension, as I don't
use that version. That's why it's so small.)

Anyway, you see, this is just the way Windows works. Basically, if you
hand it a 3-character extension, it appends an asterisk to it.
 
Edward said:
By the way if you use myfile=Dir(file pathe\+"*.pptx") it will only return
pptx file and not ppt files .

Right. That's the essential algorithm, near as I can tell. Windows
only appends the undesired asterisk when you hand it a 3-character
extension.
However I think this is an ugly defect in Dir() function, and MS should fix
this.

The defect is actually in the FindFirstFile and FindNextFile API
functions.
 
Back
Top