kelly mort
Well-known Member
- Joined
- Apr 10, 2017
- Messages
- 2,169
- Office Version
- 2016
- Platform
- Windows
I was able to come up with this code after adding pieces together. The only thing is that I don't really understand what most of the lines are doing but it's working for me so I am cool with it.
Then I later had issues beyond what I can handle!
When I turn the alert off, I get an error on this line when I want to change the image in the control.
The funny part is that this happens when I want to change the image immediately after uploading one. But when I close the form and trying changing the image, it works.
I am sure I am missing something but can't seem to figure it out yet.
I should be able to update the image anytime I want - but it looks as if that's not working for me now.
Can someone pull it up for me?
Code:
Sub UploadImage()
Dim NewFileName, FileExists$, FileName$, FileNameOnly$
Dim fso As Object, FileToCopy$, FileAtDest$, Ext$
Dim srcPath$, dstPath$, myFile, Fileselected, sFile$
NewFileName = freg2.Text
Set myFile = Application.FileDialog(msoFileDialogOpen)
Set fso = CreateObject("Scripting.FileSystemObject")
With myFile
.Title = "Please select the image file"
.AllowMultiSelect = False
.Filters.Add "Images", "*.jpg; *.jpeg", 1
If .Show <> -1 Then
MsgBox "No image file was selected. Try again", , "Canceled Alert"
Exit Sub
End If
FileName = .SelectedItems(1)
srcPath = .InitialFileName
dstPath = ThisWorkbook.Path & "\PASSPORT PICTURES\"
FileNameOnly = Left(FileName, InStr(FileName, ".") - 1)
FileToCopy = Dir(srcPath & NewFileName & ".*")
If Len(FileToCopy) Then
MsgBox "The selected image already exists. Try again", , "File Exists Alert"
Exit Sub
Else
sFile = Dir(FileNameOnly & ".*")
While Len(sFile) > 0
Ext = Right(sFile, Len(sFile) + 1 - InStrRev(sFile, "."))
Name FileNameOnly & Ext As NewFileName & Ext
sFile = Dir
Wend
End If
If srcPath <> dstPath Then
FileAtDest = Dir(dstPath & NewFileName & ".*")
FileExists = Dir(srcPath & NewFileName & ".*")
If Len(FileExists) Then
If Len(FileAtDest) Then Kill dstPath & NewFileName & ".*"
'fso.movefile Source:=srcPath & NewFileName & ".*", Destination:=dstPath
fso.COPYfile Source:=srcPath & NewFileName & ".*", Destination:=dstPath
End If
End If
End With
End Sub
Then I later had issues beyond what I can handle!
When I turn the alert off, I get an error on this line when I want to change the image in the control.
Code:
Name FileNameOnly & Ext As NewFileName & Ext
The funny part is that this happens when I want to change the image immediately after uploading one. But when I close the form and trying changing the image, it works.
I am sure I am missing something but can't seem to figure it out yet.
I should be able to update the image anytime I want - but it looks as if that's not working for me now.
Can someone pull it up for me?