Last half of my problem. Using a VBA form to copy the windows clipboard to an image file.

unluckyuser

New Member
Joined
Jan 12, 2025
Messages
30
Office Version
  1. 2019
Platform
  1. Windows
First, I need to check to see that the end user has indeed snipped an image to the clipboard, and that it's not so large as to generate a memory error. If so, then I need to copy the windows clipboard to a file.

I could either do this directly with VBA, or I could run a command line to execute a program to achieve this. Running in an environment where a large clipboard file will create memory issues.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Solved it but need to play with it to ensure that I can get it to work.


VBA Code:
' NOTE: MyChart is the new chart where the image will eventually be copied to
'       in order to then be exported as a jpg file
Dim MyChart As Chart


' NOTE: The workbook sheet "Sheet1" will be made the active sheet
Sheet1.Activate

' NOTE: This will past whatever is currently in the clipboard to the active sheet
'       So, make sure that your image is what was most recently copied
'
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range("c1")

' NOTE: This is creating the new Chart
    Set MyChart = Charts.Add

' NOTE: This is name the new chart
MyChart.Name = "HELLO"

' NOTE: This is moving the chart to the sheet where the picture is
Set MyChart = MyChart.Location(Where:=xlLocationAsObject, Name:="Sheet1")

' NOTE: This is making the width and height of the MyChart equal to whatever image you copied to the clipboard
MyChart.ChartArea.Width = Sheet1.Shapes(2).Width
MyChart.ChartArea.Height = Sheet1.Shapes(2).Height

' NOTE: This is removing the shape container boarder
MyChart.Parent.Border.LineStyle = 0

' NOTE: This is copying the image that was pasted to the sheet "Sheet1"
Sheet1.Shapes(2).Copy

' NOTE: This is selecting or making active the ChartArea of MyChart
MyChart.ChartArea.Select

' NOTE: This is pasting the image that was just copied via VBA of EXCEL into the MyChart ChartArea
MyChart.Paste

' NOTE: This is exporting the MyChart to a jpg file.
'       Filename:="location of folder\name of the file.XXX"
'       FilterName:="jpg" read about this at - > [URL='https://learn.microsoft.com/en-us/office/vba/api/Excel.Chart.Export']Chart.Export method (Excel)[/URL]
MyChart.Export Filename:="c:\hold\Me.jpg", FilterName:="jpg"

' NOTE: Make cell (Row 1, Column A) the active cell
Sheet1.Cells(1, 1).Activate

' NOTE: Delete the Chart you created earlier
Sheet1.ChartObjects(Sheet1.ChartObjects.Count).Delete

' NOTE: Delete the (hopefully) only 1 shape (image pasted ealier) on the sheet "Sheet1"
Sheet1.Shapes(1).Delete

Me.Image1.Picture = LoadPicture("c:\hold\Me.jpg")


End Sub
 
Last edited by a moderator:
Upvote 0
Solution
Glad you solved it but in the future when posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block at the bottom of this post has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,226,530
Messages
6,191,591
Members
453,666
Latest member
madelineharris

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