Update image file name based on textbox content

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
When I enter the textbox, I want to store its content to a variable "MyEnter". Then when I change the content I store the changed content to " MyChange".

Then inside a folder , "MyPics", located at the location of the workbook, if there is an image file called same as the content of the " MyEnter" variable, then I will compare "MyEnter" with "MyChange". If they are different, then I rename the image with " MyChange" , by the click of a button on a userform. The textbox too is on the userform as well.

I am stacked and I need some support with the code

Thanks in advance
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi

The code adds the extension ".jpg" when textbox value is saved to variable
- is that the correct extension ?

Declare these public variables at top of STANDARD module (above all procedures)
Code:
Public MyEnter As String
Public MyChange As String

USERFORM code
Code:
Private Sub CommandButton1_Click()
    Dim fPath As String
    fPath = ThisWorkbook.Path & "\MyPics\"
    On Error Resume Next
    Name fPath & MyEnter As fPath & MyChange
    If Err.Number > 0 Then MsgBox Err.Description
End Sub

Private Sub TextBox1_Change()
    MyChange = TextBox1.Text & ".jpg"
End Sub

Private Sub TextBox1_Enter()
    MyEnter = TextBox1.Text & ".jpg"
End Sub
 
Upvote 0
Hi

The code adds the extension ".jpg" when textbox value is saved to variable
- is that the correct extension ?

Declare these public variables at top of STANDARD module (above all procedures)
Code:
Public MyEnter As String
Public MyChange As String

USERFORM code
Code:
Private Sub CommandButton1_Click()
    Dim fPath As String
    fPath = ThisWorkbook.Path & "\MyPics\"
    On Error Resume Next
    Name fPath & MyEnter As fPath & MyChange
    If Err.Number > 0 Then MsgBox Err.Description
End Sub

Private Sub TextBox1_Change()
    MyChange = TextBox1.Text & ".jpg"
End Sub

Private Sub TextBox1_Enter()
    MyEnter = TextBox1.Text & ".jpg"
End Sub

Yes extension is correct.

And the code works very brilliantly!!!

For now I don't think I will have a problem with the file extension. But for future sake is there a way to use the file extension from the file itself instead of the hard coded .jpg?

Thanks for the code
 
Upvote 0
ok - will post code tomorrow when back at PC
 
Upvote 0
Test this with different extensions to see if it achieves what you want

in STANDARD module
Code:
Public MyEnter As String
Public MyChange As String

in USERFORM module
Code:
Private Sub CommandButton1_Click()
    Dim fPath As String, sFile As String, Ext As String
    fPath = ThisWorkbook.Path & "\MyPics\"
    sFile = Dir(fPath & MyEnter & ".*")
    Do While Len(sFile) > 0
        Ext = Right(sFile, Len(sFile) + 1 - InStrRev(sFile, "."))
        Name fPath & MyEnter & Ext As fPath & MyChange & Ext
        sFile = Dir
    Loop
End Sub

Private Sub TextBox1_Change()
    MyChange = TextBox1.Text
End Sub

Private Sub TextBox1_Enter()
    MyEnter = TextBox1.Text
End Sub
 
Upvote 0
Great!!!!

I have tested it for five different extensios and it worked so great and faster...

I am very grateful for your time thanks so much.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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