find file

paulevans

Registered User.
Local time
Today, 14:54
Joined
Mar 7, 2006
Messages
79
Hi all can someone help me with this one?

I have written the following code but I can not get it to work.

fname = pname & "_" & ia & "_" & onmae & ".doc"

Set fs = application.FileSearch
With fs
.LookIn = application.CurrentProject.Path & "\database files\letter"
.FileName = fname

While Len(.FileName) > 0

If Len(.FileName) > 0 Then
ia = ia + 1
fname = pname & "_" & ia & "_" & onmae & ".doc"
.LookIn = application.CurrentProject.Path & "\database files\letters"
.FileName = fname
Else
'filename exits we need to create another filename
strtest = MsgBox("We are about to create document " & fname & " is this ok?", vbYesNo, "Warning")
If strtest = vbNo Then
Exit Sub
End If

End If
wend

End With


This just loops.

What I want to do is look in a folder. If the exists then create another file which is incremented by the counter ia.

Any ideas as to how this should be done
 
in order to use the FileSearch, you need to call the .Execute method, which will return the number of files found. try:

Code:
Set fs = application.FileSearch
With fs
     .LookIn = application.CurrentProject.Path & "\database files\letter"
     .FileName = fname
    If .Execute > 0 then 
[COLOR="SeaGreen"]         //File exists[/COLOR]
   .
   .
   .
 
Wow that worked. Thanks for the help

Paul
 
Seems to me (and I could be wrong - I have been known to be wrong before :D ) that a quick

If Dir(FilePathAndNameHere) <> "" Then

would check for the existence of the file with less coding. If it returns a value, you could then look for a numbered version to see what number you need to make it.

Just a suggestion to cut down on the code required.
 

Users who are viewing this thread

Back
Top Bottom