• 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.

VBA help on saving selected range as picture

DJ

Member
Dear All,

I need help on saving a specific range as a picture. I found a code at https://stackoverflow.com/questions/38367064/operating-ms-paint-using-excel-vba but a line of code not working and no solution related to this found.

Sub Saving_Pic_of_a_Range()
Dim path As String
Dim MyAppID As Variant
Range("A1:B20").Select
Selection.Copy

path = "C:\Windows\system32\mspaint.exe"

MyAppID = Shell(path, 1)
AppActivate (MyAppID) 'Getting error at this line
Application.SendKeys "^v", True
Application.SendKeys "^s", True
End Sub

Thanks,
DJ
 
If at all possible avoid use of SendKeys. It's unreliable at best.

Use something like below instead to export range to image.
Change "C:\test\" to folder of your choice.
Code:
Sub Demo()
Dim expRng As Range
    With ActiveSheet
        Set expRng = .Range("A1:B20")
        expRng.CopyPicture xlScreen, xlBitmap
        With .ChartObjects.Add(expRng.Left, expRng.Top, expRng.Width, expRng.Height)
            .Name = ActiveSheet.Name & "exp"
            .Activate
        End With
    End With
    ActiveChart.Paste
    ActiveSheet.ChartObjects(ActiveSheet.Name & "exp").Chart.Export "C:\test\" & ActiveSheet.Name & ".jpg", "JPG"
    ActiveSheet.ChartObjects(ActiveSheet.Name & "exp").Delete
   
End Sub
 
Back
Top