Compare and copy and open file using VBA

S

Song Su

My SourceFile is S:\Apps\PhoneBook\Phone.pdf
My TargetFile is "%AppData%" & \elac\PhoneBook\Phone.pdf

How to use VBA to compare these two files:

If SourceFile is newer than TargetFile or TargetFile does not exist Then
Copy from SourceFile to TargetFile
End If
Open TargetFile
 
D

Douglas J. Steele

Dim strSource As String
Dim strTarget As String

strSource = "S:\Apps\PhoneBook\Phone.pdf"
strTarget = Environ("AppData") & "\elac\PhoneBook\Phone.pdf"

If Len(Dir(strTarget)) > 0 Then
If FileDateTime(strSource) > FileDateTime(strTarget) Then
FileCopy strSource, strTarget
End If
Else
FileCopy strSource, strTarget
End If

Application.FollowHyperlink strTarget
 
S

Song Su

Great! Not only your code works without modification, I also learned a great
lesson. Thank you.

I used to use DOS batch file which gave me a nasty black DOS window.
 

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