Loadpicture - check if file exists

T

t0ny

Hi friends,

I have a picture box (image1) and I use loadpicture to fill it with a
jpg image. The path is created in a cell from information inserted by
the user. The jpg images (some thousands!) are stored in a folder in
C:\. Everything works fine but there is one problem. In some occasions
it is normal for my application to call an image that is not yet
created. In that case I get error 53 'file not found'. I need a
condition to check if the file requested by the user exists. If this
condition is TRUE then my code works. If it is False I need it to
ignore the user's request. Please help me if you can.

Here is one part of my code

For i = 1 To 10


If Range("code").Offset(i - 1, 0).Value <> blank Then
filename(i) = Range("filename").Offset(i - 1, 0).Value
image(i).Picture = LoadPicture(filename(i))
Else
image(i).Picture = LoadPicture("c:\Photos\blank.jpg")

End If

Next i
 
S

Susan

tony - change code to this:
For i = 1 To 10

on error resume next
If Range("code").Offset(i - 1, 0).Value <> blank Then
filename(i) = Range("filename").Offset(i - 1, 0).Value
image(i).Picture = LoadPicture(filename(i))
Else
msgbox "Sorry, that picture is not available"

i think this should work, or at least point you in the right direction.
susan, not-a-guru
 
R

Ron de Bruin

Hi t0ny

You can use Dir in your code to check if a file exist

If Dir(filename(i)) <> "" Then
 
T

t0ny

Thank a lot. You saved me from a lot of trouble!

Happy new year


Ï/Ç Susan Ýãñáøå:
 

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