Consulting

Results 1 to 18 of 18

Thread: Excel Image Viewer and Rename its File Name

  1. #1

    Excel Image Viewer and Rename its File Name

    Dear Experts

    I would like to view image in Excel and rename the current file name. In excel I can’t view the file in full size. Please help me to view the image in full size. Also please put an DELETE button to remove the image in the folder.

    Attached herewith the fotos folder and the excel file for your kind perusal.

    Thanking you in advance.

    Yours sincerely
    Buvanamali

    p.s. I had already posted here. But no response. https://www.excelforum.com/excel-pro...file-name.html
    Attached Files Attached Files

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Should include Option Explicit in every module header. In the VBE > Tools > Options > check "Require Variable Declaration". You will have to type it into existing modules.

    Define "full size" and display where in Excel?

    A delete procedure should be simple. Use Kill statement https://learn.microsoft.com/en-us/of...kill-statement
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Dear Experts

    Option Explicit is included in the module.
    The picture is not diplayed in its full size.
    Please help me.

    Yours sincerely
    Buvanamali

  4. #4
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Option Explicit was not in module of file I downloaded. That's why I mentioned it. I added it and found a variable that had not been declared.

    I still don't know what you mean by "full size". Looks big enough to me. If you want to view in an external image app, managing that would be more difficult.

    Need to add code that disables/enables Prev/Next buttons when on first or last file.
    Also make sure Filelist is cleared before reading in file names from selected folder.

    Consider this revised code:
    Option Explicit
    Dim v_row As Integer
    Dim v_filecount As Integer
    
    Sub LoadNextImage()
        If v_row < v_filecount Then
            LoadImage 1
        Else
            MsgBox "This is last image"
        End If
    End Sub
    
    Sub LoadPrevImage()
        If v_row > 3 Then
            LoadImage -1
        Else
            MsgBox "This is first image"
        End If
    End Sub
    
    Sub LoadImage(intDirection)
        Sheets("Update").Range("K9").Value = Sheets("Update").Range("K9").Value + intDirection
        v_row = Sheets("Update").Range("K9").Value
        Sheets("Update").Image1.Picture = LoadPicture(Sheets("Filelist").Range("B1").Value & Sheets("Filelist").Range("B" & v_row).Value)
        Sheets("Update").Range("K7").Value = Sheets("Filelist").Range("B" & v_row).Value
    End Sub
    
    Function GetImageDirectory() As String
        Dim v_imagefolder As FileDialog
        Dim v_imageitem As String
        Set v_imagefolder = Application.FileDialog(msoFileDialogFolderPicker)
        With v_imagefolder
            .Title = "Select the Image Folder"
            .AllowMultiSelect = False
            .InitialFileName = Application.DefaultFilePath
            If .Show <> -1 Then GoTo NextCode
            v_imageitem = .SelectedItems(1)
        End With
    NextCode:
        If Right(v_imageitem, 1) <> "\" Then
            v_imageitem = v_imageitem & "\"
        End If
        GetImageDirectory = v_imageitem
        Set v_imagefolder = Nothing
    End Function
    
    Sub ListImageFiles()
        Dim v_fldrpath As String, v_pth As String, Filename As String
        v_fldrpath = GetImageDirectory
        Sheets("Filelist").Range("B1").Value = v_fldrpath
        v_pth = v_fldrpath
        Filename = Dir(v_pth)
        Sheets("Filelist").Range("A3:B" & Sheets("Filelist").Cells.SpecialCells(xlCellTypeLastCell).Row).Clear
        Do While Filename <> ""
            v_filecount = v_filecount + 1
            Sheets("Filelist").Range("A" & v_filecount + 2).Value = v_filecount
            Sheets("Filelist").Range("B" & v_filecount + 2).Value = Filename
            Filename = Dir()
        Loop
        Sheets("Update").Range("K9").Value = 3
        LoadImage 0
    End Sub
    
    Sub ChangeName()
        Dim oldname As String, newname As String
        oldname = Sheets("Filelist").Range("B1").Value & Sheets("Update").Range("K7").Value
        newname = Sheets("Filelist").Range("B1").Value & Sheets("Update").Range("K11").Value
        Name oldname As newname
        v_row = Sheets("Update").Range("K9").Value
        Sheets("Filelist").Range("B" & v_row).Value = Sheets("Update").Range("K11").Value
        LoadImage 0
    End Sub
    Last edited by June7; 03-22-2024 at 12:35 AM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Dear Experts
    Thanks for your kind assistance.
    I mean that by "full size" ie. the image which we are seeing in XL sheet is not the full image. And it is partially displayed. May be enlarged.

    Yours sincerely
    Buvanamali

  6. #6
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Oh, I get it now. I had not looked at image outside Excel. Just change the image control PictureSizeMode property to 3 - frmPictureSizeModeZoom. Resize the control to whatever dimensions you want. I went with 400 Height x 300 Width.

    What do you think of revised code so far?

    Ooops, found error in my new code - change

    If v_row < v_filecount Then


    to

    If v_row < v_filecount + 2 Then


    Last edited by June7; 03-22-2024 at 11:49 AM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Dear Experts
    The display size was now expanded as per my requirement. I am enclosing the file for your kind perusal and for others who follow the thread.
    Now I am getting error no.52 " Bad File Name or Number "while renaming the file in other than English.

    Thanking you

    Yours Sincerely
    Buvanamali
    Attached Files Attached Files

  8. #8
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Provide example of non-English filename that causes error.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Dear Experts

    I enclose here with the sample file and zip folder containing few .jpg files to be renamed in non-english file name (Tamil). The non-English file name is in C Column of Sample File - Filelist Sheet.

    Please Provide me a Solution.

    Thanking you

    Yours sincerely
    Buvanamali
    Attached Files Attached Files
    Last edited by buvanamali; 03-23-2024 at 06:11 PM.

  10. #10
    Dear Experts

    With the following macro I am able to change Non English File Name without any problem. I do not know how to use this macro rename multiple files.
    Please help me.
    ChangeFileName(sFile, tFile)
        On Local Error GoTo errors
        Dim fs, f, s
        Set fs = CreateObject("Scripting.FileSystemObject")
        fs.MoveFile sFile, tFile
        Set f = Nothing
        Exit Sub
    errors:
        MsgBox "File: " & sFile & vbCrLf & Err.Description
        Err.Clear
    End Sub
    
    Sub test()
        Const sDir = "C:\Users\TOSHIBA\Desktop\Fotos\"
        ChangeFileName sDir & Range("B3").Value, sDir & Range("C3").Value
    End Sub
    Also the above code doesn't save the file name as "*.jpg"
    Thanking you

    Yours sincerely
    Buvanamali

  11. #11
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    You want user to enter a value into textbox and use that to change name? Replace code in the ChangeName procedure.

    None of the new names have .jpg extension. Need to make sure the same extension as original is used. Assuming users will not type file extension.

    Sub ChangeName()
        Dim fs As FileSystemObject, ext As String, oldName As String, newName As String, sDir As String
        
        sDir = Sheets("Filelist").Range("B1").Value
        oldName = sDir & Sheets("Update").Range("K7").Value
        newName = Sheets("Update").Range("K11").Value
        ext = Mid(oldName, InStrRev(oldName, "."))
        
        Set fs = CreateObject("Scripting.FileSystemObject")
        fs.MoveFile oldName, sDir & newName & ext
        
        Sheets("Filelist").Range("B" & v_row).Value = newName & ext
        LoadImage 0
    
    End Sub
    However, I am now having issue with loading images with this new name.

    It works if changing to English name. Sorry, I don't have a solution.
    Last edited by June7; 03-23-2024 at 08:09 PM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    Dear Experts

    The same error as posted in post#7. Please advise.

    Yours sincerely
    Buvanamali

  13. #13
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    I know it has same error with non-English name. File name is changed and then code cannot load image.

    I have no solution for this.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    Quote Originally Posted by buvanamali View Post
    Dear Experts

    With the following macro I am able to change Non English File Name without any problem. I do not know how to use this macro rename multiple files.
    Please help me.
    ChangeFileName(sFile, tFile)
        On Local Error GoTo errors
        Dim fs, f, s
        Set fs = CreateObject("Scripting.FileSystemObject")
        fs.MoveFile sFile, tFile
        Set f = Nothing
        Exit Sub
    errors:
        MsgBox "File: " & sFile & vbCrLf & Err.Description
        Err.Clear
    End Sub
    
    Sub test()
        Const sDir = "C:\Users\TOSHIBA\Desktop\Fotos\"
        ChangeFileName sDir & Range("B3").Value, sDir & Range("C3").Value
    End Sub
    Also the above code doesn't save the file name as "*.jpg"
    Thanking you

    Yours sincerely
    Buvanamali
    The above Code changes non english names only for single row. I wanted to rename multiple files. please try to help me.

  15. #15
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Can loop through a list of files. So if you have old name and new name in two columns of worksheet as in your current version of workbook, like:

    Sub test()
        Const sDir = "C:\Users\TOSHIBA\Desktop\Fotos\"
        Dim x As Integer, rows As Integer, ext As String, strName As String
        rows = Sheets("Filelist").Cells.SpecialCells(xlCellTypeLastCell).Row
        For x = 3 To rows
            strName = Sheets("FileList").Range("B" & x).Value
            ext = Mid(strName, InStrRev(strName, "."))
            If Not IsEmpty(Sheets("Filelist").Range("C" & x).Value) Then
                ChangeFileName sDir & strName, sDir & Sheets("Filelist").Range("C" & x).Value & ext
            End If
        Next
    End Sub
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  16. #16
    Dear Experts

    It is working as desired. Now I request for 3 more requirements.
    1) The value whatever I type in K11 shall be appended to (Filelist) sheet "C" Column.
    2) If the file name repeated in Col C then Filename 1 , Filename 2 , File Name 3 likewise renamed
    3) If I put delete in C Col, while renaming the files, delete row should delete images in the folder.

    Thanking you for your efforts.

  17. #17
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    @buvanamali, please start a new thread. Your initial issue has been solved as you have indicated.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  18. #18
    Dear Experts

    I marked the thread is solved.

    I started a new thread for rest of my requirements.

    Thanking you

    Yours sincerely
    Buvanamali

Posting Permissions

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