Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check for file during archive - vb from excel

Posted on 2008-06-16
4
Medium Priority
?
142 Views
Last Modified: 2010-05-03
I have a macro which copies files from one directory to another only if the following condition exists:
Costs*.xls.  If there are no files in the source directory, the vb script produces an error message that I do not want the users to see.  Is there any way to display a message box that says there are no files that meet the archive criterion?  I have attached my macro for your review.
Sub ArchiveFiles()
    Dim fso As Object
    
    Dim sSourceDir As String
    Dim sTargetDir As String
    
    sSourceDir = "C:\my documents\"
    sTargetDir = "C:\my documents\backup\"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    fso.movefile Source:=sSourceDir & "Costs*.xls", Destination:=sTargetDir
    fso.movefile Source:=sSourceDir & "Status*.xls", Destination:=sTargetDir
    
End Sub

Open in new window

0
Comment
Question by:ilrosebud26
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
peetm earned 500 total points
ID: 21794713
The filesystemobject doesn't have a files collection that uses wildcards as far as I know - so, you'd first have to test using dir$, e.g.,

If Len(Dir$(fso.buildpath(sSourceDir, "Costs*.xls")) > 0 then

   ' do the move.

    fso.movefile Source:=sSourceDir & "Costs*.xls", Destination:=sTargetDir

else

    MsgBox "No matching files"

endif
0
 

Author Comment

by:ilrosebud26
ID: 21794831
Peetm-
    Thanks, I will try this after lunch and let you know.

Rosemary
0
 

Author Comment

by:ilrosebud26
ID: 21797390
That worked great!

Thanks!
Rosemary

0
 

Author Closing Comment

by:ilrosebud26
ID: 31467655
Thanks for getting back so quickly.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question