Error in Export Range as image

Shinod

New Member
Joined
Jun 29, 2022
Messages
38
Office Version
  1. 2019
Platform
  1. Windows
VBA Code:
Sub SaveAsJPG()

    Dim ChO As ChartObject, ExportName As String
    Dim CopyRange As Range
    Dim Pic As Picture
    Dim i As Long

    Dim Fold As String
    Dim Name As String
    Dim Path As String

    Fold = ActiveSheet.Range("R1")
    Name = ActiveSheet.Range("H3").Value & " " & ActiveSheet.Range("D3").Value
    Path = Fold & Application.PathSeparator & Name & ".jpg"

    With ActiveSheet
        Set CopyRange = Range("B1:I43")
        If Not CopyRange Is Nothing Then
            Application.ScreenUpdating = False
            ExportName = Path
            If Not ExportName = "False" Then
                CopyRange.Copy
                .Pictures.Paste
                Set Pic = .Pictures(.Pictures.Count)
                Set ChO = .ChartObjects.Add(Left:=10, Top:=10, Width:=Pic.Width, Height:=Pic.Height)
                Application.CutCopyMode = False
                Do
                    DoEvents
                    Pic.Copy
                    DoEvents
                    ChO.Chart.Paste
                    DoEvents
                    i = i + 1
                Loop Until (ChO.Chart.Shapes.Count > 0 Or i > 50)

                ChO.Chart.Export Filename:=ExportName, Filtername:="JPG"
                ChO.Delete
                Pic.Delete
            End If
            Application.ScreenUpdating = True
        End If
    End With
    MsgBox "Payslip created for " & ActiveSheet.Range("D3") & " in " & Fold
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi Shinod. Here's some more flexible code. A couple of things. Using "Name" and/or "Path" as variable names is not good as these have specific XL meaning(s). Not certain what is in your cells that name your file but sometimes XL doesn't like the name you have come up with. If you repeat copying and pasting pictures, your clipboard will eventually crash hence the clipboard code below. HTH. Dave
Regular code....
Code:
Sub SaveAsJPG()
Dim Fold As String, FlName As String, ExportName As String
Fold = ActiveSheet.Range("R1")
FlName = ActiveSheet.Range("H3").Value & " " & ActiveSheet.Range("D3").Value
ExportName = Fold & Application.PathSeparator & ValidFilePath(FlName) & ".jpg"
Call CreateJpg(ActiveSheet.Name, ActiveSheet.Range("B1:I43"), ExportName)
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
MsgBox "Payslip created for " & ActiveSheet.Range("D3") & " in " & Fold
End Sub

Sub CreateJpg(SheetName As String, xRgAddrss As Range, NameFile As String)
xRgAddrss.CopyPicture
With Sheets(SheetName).ChartObjects.Add(xRgAddrss.Left, xRgAddrss.Top, xRgAddrss.Width, xRgAddrss.Height)
.Activate
.Chart.Paste
.Chart.Export NameFile, "JPG"
End With
Sheets(SheetName).ChartObjects(Sheets(SheetName).ChartObjects.Count).Delete
End Sub

Public Function ValidFilePath(Arg As String) As String
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
    .Pattern = "[\\/:\*\?""<>\|]"
    .Global = True
    ValidFilePath = .Replace(Arg, "_")
End With
Set RegEx = Nothing
End Function
Module code...
Code:
#If VBA7 Then
Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Public Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
#Else
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
#End If
To operate...
Code:
Call SaveAsJPG
 
Upvote 0
Hi Shinod. Here's some more flexible code. A couple of things. Using "Name" and/or "Path" as variable names is not good as these have specific XL meaning(s). Not certain what is in your cells that name your file but sometimes XL doesn't like the name you have come up with. If you repeat copying and pasting pictures, your clipboard will eventually crash hence the clipboard code below. HTH. Dave
Regular code....
Code:
Sub SaveAsJPG()
Dim Fold As String, FlName As String, ExportName As String
Fold = ActiveSheet.Range("R1")
FlName = ActiveSheet.Range("H3").Value & " " & ActiveSheet.Range("D3").Value
ExportName = Fold & Application.PathSeparator & ValidFilePath(FlName) & ".jpg"
Call CreateJpg(ActiveSheet.Name, ActiveSheet.Range("B1:I43"), ExportName)
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
MsgBox "Payslip created for " & ActiveSheet.Range("D3") & " in " & Fold
End Sub

Sub CreateJpg(SheetName As String, xRgAddrss As Range, NameFile As String)
xRgAddrss.CopyPicture
With Sheets(SheetName).ChartObjects.Add(xRgAddrss.Left, xRgAddrss.Top, xRgAddrss.Width, xRgAddrss.Height)
.Activate
.Chart.Paste
.Chart.Export NameFile, "JPG"
End With
Sheets(SheetName).ChartObjects(Sheets(SheetName).ChartObjects.Count).Delete
End Sub

Public Function ValidFilePath(Arg As String) As String
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
    .Pattern = "[\\/:\*\?""<>\|]"
    .Global = True
    ValidFilePath = .Replace(Arg, "_")
End With
Set RegEx = Nothing
End Function
Module code...
Code:
#If VBA7 Then
Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Public Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
#Else
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
#End If
To operate...
Code:
Call SaveAsJPG
Dear @NdNoviceHlp Thank you for your reply. I was looking for a solution for this for a long time.

The purpose is to create payslips in "Image".

So will you be kind enough to go through my sheet, please?

As you said when creating image files excel is getting errors sometimes. Someone suggested me to put some delay to overcome this error. but that one also didnt help me much.

Here is the file, could you please fix for me.

The purpose is to create payslip (s) based on the selection. if there is some other way to achieve this goal, then suggest me.

Thanks in advance.

 
Upvote 0
Shinod did you trial the code posted? In my limited testing, it created a .jpg file containing the specified range which is what you seemed to be requesting. Not sure that looking at your file will help if I don't know what is actually going wrong? If there is some error, please indicate on what line of code the error occurs. Dave
 
Upvote 0

Forum statistics

Threads
1,215,228
Messages
6,123,747
Members
449,118
Latest member
kingjet

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