Results 1 to 5 of 5

Thread: counting documents in windows explorer using VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Location
    Milwaukee, WI
    Posts
    9

    Question counting documents in windows explorer using VBA

    HELP!!! Before I go completely insane....I have a macro that is set up to count the number of files in a folder so if all my files are there it will print the files to a prn file and print the entire batch. However it appears to be counting the files that start with the ~. Is there a way to set it to only count the documents that are not temporary and only .doc files?
    Thanks!
    Tina

  2. #2
    Addicted Member
    Join Date
    Aug 2002
    Location
    Luton, UK
    Posts
    178
    This will do the job :-

    Code:
    '------------------------------
    Sub test()
        Dim MyCount As Integer
        Dim MyName As String
        MyName = Dir("c:\*.doc")
        Do While MyName <> ""
            MyCount = MyCount + 1
            MyName = Dir
        Loop
        MsgBox (MyCount)
    End Sub
    '-------------------------------
    Regards
    BrianB
    -------------------------------

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Location
    Milwaukee, WI
    Posts
    9
    Thanks for your response. I tried your code. tweeked it a little because I pass in the path and I come up with 0 even though there are 2 files in there. Here's what it looks like:

    Dim MyCount As Integer
    Dim MyName As String
    MyName = Dir(target & "*.doc")
    Do While MyName <> ""
    MyCount = MyCount + 1
    MyName = Dir
    Loop
    MsgBox (MyCount)

    Here what I was using...

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set fc = fs.GetFolder(target)
    intFileCount = fc.files.Count

    This wasn't working, so I tried this...

    With Application.FileSearch
    .LookIn = (target)
    .FileType = msoFileTypeWordDocuments
    If .Execute() > 0 Then
    intFileCount = .FoundFiles.Count
    End If
    End With

    It appeared to work, but now I have complaints again today...

    Help!!!

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Originally posted by tgrant29
    VB Code:
    1. Dim MyCount As Integer
    2.     Dim MyName As String
    3.     MyName = Dir(target & "*.doc"[color=red], 63)
    4.     Do While MyName <> "" and left(MyName,1)<>"~" and not(MyName="." or MyName="..")[/color]
    5.         MyCount = MyCount + 1
    6.         MyName = Dir
    7.     Loop
    8.     MsgBox (MyCount)
    The other two methods, dunno
    Prolly FSO would work, but you'd need to read up on methods/properties.

    As to Application.FileSearch, it may only find certain filetypes and not all filetypes (it is part of the VBA App...)


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Location
    Milwaukee, WI
    Posts
    9
    Thanks Vince! Much appreicated!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width