• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Rename the pictures using excel

jcs

New Member
Hi Guys,

I need your help. I'm using mac. I try to figured out for this code for change the old names of picture to new name.

Here the the code:
Code:
Sub PictureNametoExcel()
'UpdatebyExtendoffice201709027
    Dim I As Long
    Dim xRg As Range
    Dim xAddress As String
    Dim xFileName As String
    Dim xFileDlg As FileDialog
    Dim xFileDlgItem As Variant
    On Error Resume Next
    xAddress = ActiveWindow.RangeSelection.Address
    Set xRg = Application.InputBox("Select a cell to place name list:", "KuTools For Excel", xAddress, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Set xRg = xRg(1)
    xRg.Value = "Picture Name"
    With xRg.Font
    .Name = "Arial"
    .FontStyle = "Bold"
    .Size = 10
    End With
    xRg.EntireColumn.AutoFit
    Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    I = 1
    If xFileDlg.Show = -1 Then
        xFileDlgItem = xFileDlg.SelectedItems.Item(1)
        xFileName = Dir(xFileDlgItem & "/")
        Do While xFileName <> ""
            If InStr(1, xFileName, ".jpg") + InStr(1, xFileName, ".png") + InStr(1, xFileName, ".img") + InStr(1, xFileName, ".ioc") + InStr(1, xFileName, ".bmp") > 0 Then
                xRg.Offset(I).Value = xFileDlgItem & "/" & xFileName
                I = I + 1
            End If
            xFileName = Dir
        Loop
    End If
    Application.ScreenUpdating = True
End Sub

Sub RenameFile()
'UpdatebyExtendoffice20170927
    Dim I As Long
    Dim xLastRow As Long
    Dim xAddress As String
    Dim xRgS, xRgD As Range
    Dim xNumLeft, xNumRight As Long
    Dim xOldName, xNewName As String
    On Error Resume Next
    xAddress = ActiveWindow.RangeSelection.Address
    Set xRgS = Application.InputBox("Select Original Names(Single Column):", "KuTools For Excel", xAddress, , , , , 8)
    If xRgS Is Nothing Then Exit Sub
    Set xRgD = Application.InputBox("Select New Names(Single Column):", "KuTools For Excel", , , , , , 8)
    If xRgD Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    xLastRow = xRgS.Rows.Count
    Set xRgS = xRgS(1)
    Set xRgD = xRgD(1)
    For I = 1 To xLastRow
        xOldName = xRgS.Offset(I - 1).Value
        xNumLeft = InStrRev(xOldName, "/")
        xNumRight = InStrRev(xOldName, ".")
        xNewName = xRgD.Offset(I - 1).Value
        If xNewName <> "" Then
            xNewName = Left(xOldName, xNumLeft) & xNewName & Mid(xOldName, xNumRight)
            Name xOldName As xNewName
        End If
    Next
    MsgBox "Congratulations! You have successfully renamed all the files", vbInformation, "KuTools For Excel"
    Application.ScreenUpdating = True
End Sub

Now when I run this code in excel. The error is appear this code area.
"Dim xFileDlg As FileDialogFileDialog". I need your help guys how to run this in mac.

In window, everything is fine. Only the excel in mac the problem.
 
Please use code tag when posting codes. I fixed it this time.
upload_2018-1-16_15-37-19.png

MAC (OSX) does not have FileDialog (it's Windows specific library). You'll need some other method.

Have a read of articles in link. It's one of the best resource for working with files/folders in MAC through VBA.
http://www.rondebruin.nl/mac/section3.htm
 
Please use code tag when posting codes. I fixed it this time.
View attachment 48961

MAC (OSX) does not have FileDialog (it's Windows specific library). You'll need some other method.

Have a read of articles in link. It's one of the best resource for working with files/folders in MAC through VBA.
http://www.rondebruin.nl/mac/section3.htm


Thanks you for advice. Any solution how to fix that code so can I copy at paste to my files.
 
Sorry, I don't have MAC and don't like Apple products in general and am unable to test.

Try experimenting with the code in Ron's site.
 
jcs
You wrote: In window, everything is fine. Only the excel in mac the problem.

It is not any problem ... maybe only a minor challenge!

You could try use ex next with this challenge with both OS:
Code:
    If Left(Application.OperatingSystem, 3) = "Mac" Then
        On Error Resume Next
            FilePath = MacScript("(choose file) as string")
    Else
        With Application.FileDialog(msoFileDialogOpen)
            .allowmultiselect = False
            If .Show = -1 Then FilePath = .selecteditems(1)
        End With
    End If
It works with my cases!
Without FULL code & other cannot test even with my Mac!
 
Back
Top