FileSystemObject help please.

G

Guest

I have a check box, and when checked I want to open a document. I know where
this document will be, but I want the latest version of it.

All of the file names will be the same except that it will have a marker to
denote what version. What I can't figure is how to do this properly.

I know that I can use the FileSystemObject and the FileExists method, but
can't seem to get it working.

Here's the code ...

Private Sub chkBMHFups_Click()

strRequired = "BMHFups"
intCounter = 1
blnExists = True

If chkBMHFups Then

Set fsoFileExists = CreateObject("Scripting.FileSystemObject")

Do While blnExists

strFullPath = fctWhereToLook & Left(strRequired, Len(strRequired) -
4) & intCounter & Right(strRequired, 4)

Set strFileToLookFor = fsoFileExists.fileexists(strFullPath)

intCounter = intCounter + 1
blnExists = strFileToLookFor

Loop

End If

Set fsoFileExists = Nothing
strRequired = ""
intCounter = 1
blnExists = False

End Sub

fctWhereToLook is working fine, but it errors when I get to the ...

Set strFileToLookFor = fsoFileExists.fileexists(strFullPath)

The error is this...

Run-time error '424':

Object required.

I'm not sure how to use this funtion so any help would be gratefully received.

Thanks.
 
B

Bob Phillips

Dave,

This works for me

chkBMHFups = True
fctWhereToLook = "C:\myTest\"
If chkBMHFups Then

Set fsoFileExists = CreateObject("Scripting.FileSystemObject")

Do
stemp = fctWhereToLook & Left(strRequired, Len(strRequired) - 4) &
intCounter & Right(strRequired, 4) & ".xls"
blnExists = fsoFileExists.fileexists(stemp)
If blnExists Then
strFullPath = stemp
intCounter = intCounter + 1
End If
Loop Until Not blnExists

End If
 
T

Tom Ogilvy

Set strFileToLookFor = fsoFileExists.fileexists(strFullPath)
should be

Dim strFileToLookFor as Boolean
strFileToLookFor = fsoFileExists.fileexists(strFullPath)

or you can skip the intermediate variable and do
blnExists = fsoFileExists.fileexists(strFullPath)
 
G

Guest

Thanks for the help guys,

had a think over the weekend and realised I needed a Boolean rather than a
string!
 

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