Results 1 to 6 of 6

Thread: VBA select image from open dialogue box and rename selected image with content of a textbox
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

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

    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
    There Is Always A Better Way

  2. #2
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

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

    Bump
    There Is Always A Better Way

  3. #3
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

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

    Bump.

    Still need help with this.
    There Is Always A Better Way

  4. #4
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

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

    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 by kelly mort; Sep 20th, 2019 at 05:43 PM.
    There Is Always A Better Way

  5. #5
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,177
    Post Thanks / Like
    Mentioned
    470 Post(s)
    Tagged
    47 Thread(s)

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

    Cross posted https://www.excelforum.com/excel-pro...nt-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.
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  6. #6
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,510
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

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

    Quote Originally Posted by Fluff View Post
    Cross posted https://www.excelforum.com/excel-pro...nt-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.
    There Is Always A Better Way

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •