Solved

Check for file during archive - vb from excel

Posted on 2008-06-16
4
136 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 125 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

776 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