VBA select image from open dialogue box and rename selected image with content of a textbox

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
The challenge gets tougher and I have to call on Tech support:)

When I click on a command button, I want to access the open dialogue box where I select an image file from a folder - this could be anywhere on the computer.

Then I rename that image with the content in textbox1 (for example).

Then I later copy that image to a folder, "MyLocation", located at the same location of the workbook.

So here, it could happen that the selected image is located in the " MyLocation" folder.

And in this case, we just rename it but we don't copy.

Can someone help me with it?

Thanks in advance
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I have managed somehow half-way to be able to come to what I wanted to achieve. I did this by doing some addition and subtraction of some pieces of codes. If there is anything that needs to be cleaned and make the algorithm prettier, then, I am looking up to that. If there are issues with my variable usage, I will like to be informed as well.

Where I have placed those comments I want them fix for me.

Those parts became more than I could process ATM.

Thanks for having a look at this for me.


Code:
Option Explicit
Sub GetImportFileName()
    Dim NewFileName, FileExists$, FileName$, FileNameOnly$
    Dim fso As Object, FileToCopy$, FileAtDest$, Ext$
    Dim srcPath$, dstPath$, myFile, Fileselected, sFile$
    
    NewFileName = "NewImage"


    Set myFile = Application.FileDialog(msoFileDialogOpen)
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' I want to set to filters to only image files
    
    With myFile
        .Title = "Please select the image file"
        .AllowMultiSelect = False
        If .Show <> -1 Then
            If MsgBox("No file was selected. Will you try again?", vbYesNo, _
            "Canceled Alert") <> vbYes Then Exit Sub
  
            ' I want to keep looping for the show dialog until i select "No"
            'myFile.Show
        End If
        FileName = .SelectedItems(1)
        srcPath = .InitialFileName
        dstPath = ThisWorkbook.Path & "\PASSPORT OFFLINE\"
        FileNameOnly = Left(FileName, InStr(FileName, ".") - 1)
        FileToCopy = Dir(srcPath & NewFileName & ".*")
            If Len(FileToCopy) Then
                If MsgBox("The selected file already exists. Will you try again?", vbYesNo, "File Exists Alert") <> vbYes Then Exit Sub
                
                'myFile.Show
                ' I want to keep looping for the show dialog until i select "No"
            Else
                MsgBox "The selected file does not exist"
                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
            End If
        End If
    End With
End Sub
 
Last edited:
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...ialogue-selection-code-adjustment-needed.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...ialogue-selection-code-adjustment-needed.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.


Okay thanks for the details and reminder. I did try to post the link at the other forum but that didn't work. I had alerts that I can't do that. That diverted my attention to get that link back here.

I promise to do the right thing afterwards.


Thanks again.
 
Upvote 0

Forum statistics

Threads
1,214,426
Messages
6,119,414
Members
448,895
Latest member
omarahmed1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top