Error in code to save loaded pictures in userform to specific path

YingFa

Board Regular
Joined
Nov 4, 2019
Messages
63
Hello, I have the following code that I got with the help of this wonderful forum. I have modified a little but it is not working. Would it be possible to get your help?
Code:
Private Sub PicToSheet1(picControl, sht As Worksheet)
    Dim PCAR As String
    CAR = Me.TextBox1.Value
    Dim p As String, L As Double, T As Double, H As Double
    path = "C:\Users\Anahi\Desktop\PCAR Project\Folders" & CAR
    
'save temporary image to folder
    SavePicture picControl.Picture, path
'embed image in sheet
    L = sht.Cells(13, 1).Left: T = sht.Cells(13, 1).Top
    With sht.Shapes.AddPicture(Filename:=p, linktofile:=msoFalse, savewithdocument:=msoCTrue, Left:=L, Top:=T, Width:=290, Height:=220)
        .Placement = xlMove
        .OLEFormat.Object.PrintObject = msoTrue
        .OLEFormat.Object.Locked = msoTrue
    End With
'delete temporary file
   
End Sub

When I run the above code, it gives me error in this line: SavePicture picControl.Picture, path. I would like the picture loaded in userfomer1 Image1 saved with that name and Image 2 saved with that name as well in the path that depends on CAR #. Can you help me please?
 
Last edited by a moderator:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
is CAR your file name?
- insert path separator \ before CAR in string path

 
Upvote 0
Yes CAR is the file name. The above code is wrong in that sense. I just need the image to be saved in that path. I do not need to be place in a worksheet cell after it is saved. But I cannot make it work. Would it be possible to have your help?

Code:
[COLOR=#333333]Private Sub PicToSheet1(picControl)[/COLOR]
[COLOR=#333333]Dim CAR As String[/COLOR]
[COLOR=#333333]CAR = Me.TextBox1.Value[/COLOR]
[COLOR=#333333]Dim p As String, L As Double, T As Double, H As Double[/COLOR]
[COLOR=#333333]path = "C:\Users\SS1590\Desktop\CAR Project\Folders" & CAR[/COLOR]

[COLOR=#333333]'save temporary image to folder[/COLOR]
[COLOR=#333333]SavePicture picControl.Picture, path[/COLOR]
[COLOR=#333333]'embed image in sheet[/COLOR]
[COLOR=#333333].OLEFormat.Object.PrintObject = msoTrue[/COLOR]
[COLOR=#333333].OLEFormat.Object.Locked = msoTrue[/COLOR]
[COLOR=#333333]End Sub[/COLOR]

Thank you.
 
Last edited:
Upvote 0
Code:
[COLOR=#333333]path = "C:\Users\Anahi\Desktop\PCAR Project\Folders\" & CAR[/COLOR]
 
Upvote 0
I tried by adding the \ but it is not working, the image is not save to the path. The picture is loaded in a userform image control box "Image1".
 
Upvote 0
Does the folder exist ?
C:\Users\Anahi\Desktop\PCAR Project\Folders
 
Upvote 0
How are you calling sub PicToSheet1?
 
Last edited:
Upvote 0
What is the error message?
 
Upvote 0
What is the error message?

Hi Rory, this is the code that I have which i tried to modify from a code
kindly help me with.

Code:
Private Sub CommandButton1_Click()


PicToSheet1 Me.Image1
PicToSheet2 Me.Image2

End Sub

Private Sub PicToSheet1(picControl)
Dim CAR As String
CAR = Me.TextBox1.Value
Dim p As String, L As Double, T As Double, H As Double
path = "C:\Users\SS1590\Desktop\CAR Project\Folders" & CAR


'save temporary image to folder
SavePicture picControl.Picture, path
'embed image in sheet
.OLEFormat.Object.PrintObject = msoTrue
.OLEFormat.Object.Locked = msoTrue
End Sub

This is the original code:

rivate Sub UserForm_Activate()
    PicToSheet Me.Image1, ActiveSheet
End Sub

Private Sub PicToSheet(picControl, sht As Worksheet)
    Dim p As String, L As Double, T As Double, H As Double
    p = ThisWorkbook.Path & "\" & Format(Now, "yymmdd hhmmss") & "bmp"
'save temporary image to folder
    SavePicture picControl.Picture, p
'embed image in sheet
    L = sht.Cells(1, 12).Left: T = sht.Cells(1, 12).Top
    With sht.Shapes.AddPicture(Filename:=p, linktofile:=msoFalse, savewithdocument:=msoCTrue, Left:=L, Top:=T, Width:=123, Height:=49.5)
        .Placement = xlMove
        .OLEFormat.Object.PrintObject = msoTrue
        .OLEFormat.Object.Locked = msoTrue
    End With
'delete temporary file
    Kill p
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
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