working with images

unluckyuser

New Member
Joined
Jan 12, 2025
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hey everyone! I'm working on a vba application that contains records of patients. Each record has a field in it which is an image. The image can only be posted from the windows clipboard, as it cannot be a file. My idea is for the user to clip an image into the clipboard (the patient picture), which will then be posted to the excel sheet as a field in a record when the user clicks the "paste" button in a form. Clipping is the only way the user can access the patient's picture.

Using VB forms, it seems one of the attributes of an image is that it is from an image file, so I apparently cannot have the clipboard contents inserted directly in the image control. I cannot create files other than the spreadsheet file. I want to have the record editable with a form. The question is how can I work with the image within a VBA form? When creating a new patient record, the user needs to be able to enter an image. If they post it in the windows clipboard, I can read the image out of that into a cell easily enough. The user also needs to be able to change the image or delete it.

I feel like I'm stepping on my on tongue with this convoluted post. Any suggestions, I'd be grateful :).
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Here is code from a project that uses a Form for displaying images. It also has a delete image option as well. Hopefully this will assist you.

VBA Code:
Private Sub CommandButton1_Click()
Dim i As Integer, resim, cevap As String

If ListBox1.ListIndex = -1 Then
    MsgBox "The listbox item isn't selected to delete !", vbCritical, ""
   Exit Sub
   End If
   resim = ThisWorkbook.Path & "\"
    If ListBox1.ListIndex >= 0 Then
     cevap = MsgBox("Entry will be deleted. ... Are you sure ?", vbYesNo)
       If cevap = vbYes Then

For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) Then
    Kill ThisWorkbook.Path & "\" & ListBox1.Value
    ListBox1.RemoveItem (i)
    If ListBox1.ListCount = 0 Then
     Image1.Picture = LoadPicture("")
     End If
    End If
Next i
      
    End If
    End If
End Sub

Private Sub CommandButton2_Click()
Dim dosya, cevap, resim As String
    ChDir "C:\"
    dosya = Application.GetOpenFilename(FileFilter:="," & "*.jpg", _
    Title:="Lütfen resim seçimi yapýnýz")
    resim = ThisWorkbook.Path & "\"
  
    If dosya = False Then
    Exit Sub
    Else
  
    Image3.Picture = LoadPicture(dosya)
    cevap = MsgBox("The selected picture will be added to the folder... Are you sure ?", vbYesNo)
    If cevap = vbYes Then
    SavePicture Image3.Picture, resim & GetFileName(CStr(dosya))
    Else
     Image3.Picture = LoadPicture("")
    Exit Sub
   End If
    End If
    ListBox1.Clear
    UserForm_Initialize
    Image3.Picture = LoadPicture("")
End Sub

Private Sub Image1_Click()
Unload Me
UserForm1.Show
End Sub

Function GetFileName(dosya As String)
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    GetFileName = fso.GetFileName(dosya)
End Function

Private Sub Image3_Click()
Unload Me
UserForm1.Show
End Sub

Private Sub ListBox1_Click()
    Image1.Picture = LoadPicture(ThisWorkbook.Path & "\" & ListBox1.Value)
  
End Sub

Private Sub SpinButton1_SpinDown()
On Error Resume Next
If ListBox1.ListIndex = ListBox1.ListCount - 1 Then Exit Sub
With Me.ListBox1
        .ListIndex = .ListIndex + 1
    End With
 End Sub

Private Sub SpinButton1_SpinUp()
On Error Resume Next
If ListBox1.ListIndex = 0 Then Exit Sub
With Me.ListBox1
        .ListIndex = .ListIndex - 1
    End With
    End Sub

Private Sub UserForm_Initialize()
Dim fso As Object, dosyam As Variant
    Set fso = CreateObject("Scripting.FileSystemObject")
    For Each dosyam In fso.GetFolder(ThisWorkbook.Path).Files
    evn = fso.GetExtensionName(ThisWorkbook.Path & "/" & dosyam.Name)
    If evn = "jpg" Then ListBox1.AddItem dosyam.Name
    If ListBox1.ListCount > 0 Then
    ListBox1.Value = ListBox1.List(0)
    End If
    Next
    Set fso = Nothing
End Sub

Download workbook : Internxt Drive – Private & Secure Cloud Storage
 
Upvote 0
Okay, I think I've got a strategy. Userform displays a record. Userform takes the image in a cell and writes it to a jpg file. Image control in userform displays image.

When the user is capturing an image, on clicking "insert" button on VBA form, the image in the clipboard is written to the spreadsheet's activeX image control.
 
Upvote 0
It is not the case that images can only be loaded from a file, and you can indeed get image data stored on a clipboard. I suspect that most steps in your process are already published in one form or another somewhere on this forum, but I would defer to Jaafar on that. I may be misunderstanding the task.
 
Upvote 0

Forum statistics

Threads
1,225,626
Messages
6,186,089
Members
453,336
Latest member
Excelnoob223

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