szita2000
Board Regular
- Joined
- Apr 25, 2012
- Messages
- 101
- Office Version
- 365
- Platform
- Windows
Hi all.
I am trying to run the following code one after another for three different ranges.
The macro extracts the ranges and save them as pictures.
I am calling the same macro three times, each time extracting a different range.
On the line that is red I receive random 1004 - CopyPicture method of Range class failed errors.
I can run the three macros a number of time then the error pops up. - But not at the same subroutine ?!?
I clear the error and it runs again.
The line it craps out, is always the .copypicture one but in different subs.
Any idea of what am I doing wrong?
I am trying to run the following code one after another for three different ranges.
The macro extracts the ranges and save them as pictures.
Code:
Sub ExportBRRangeToPNG() '--exports selected range to png file
' jpg is not appropriate format for this output
' default filename is address of selected range
' based on code example posted at:
' http://www.emoticode.net/visual-basic/vba-export-excel-range-as-image-and-save-as-file.html
' modified at
' http://www.mrexcel.com/forum/excel-questions/751500-export-range-cells-jpg.html#post3691601
' further refined by Jon Peltier, Peltier Technical Services, Inc.
Dim vBRFilePath As String
Dim rBRSelection As Range
Dim sBRDefaultName As String
Dim t As Variant
If TypeName(Selection) <> "Range" Then
MsgBox "Selection is not a range of cells."
Exit Sub
End If
Set rBRSelection = Worksheets("DirMail").Range("A60:K65")
' t = Now + TimeValue("00:00:01")
' sBRDefaultName = Replace(rBRSelection.Address(0, 0, xlA1, 1), ":", "to") & ".png"
sBRDefaultName = "Oralcare.png"
vBRFilePath = "G:\ASTER\11 Production\DDS\Aster DDS output files\BR.png"
'--exit if cancelled by user
' If vBRFilePath = False Then Exit Sub
'-- copy selected range as picture (not as bitmap)
Application.CutCopyMode = False
[B][COLOR=#b22222] rBRSelection.CopyPicture Appearance:=xlScreen, Format:=xlPicture[/COLOR][/B]
'--Create an empty chart, slightly larger than exact size of range copied
With ActiveSheet.ChartObjects.Add( _
Left:=rBRSelection.Left, Top:=rBRSelection.Top, _
Width:=rBRSelection.Width + 0.1, Height:=rBRSelection.Height + 0.1)
With .Chart
' clean up chart
.ChartArea.Format.Line.Visible = msoFalse
.ChartArea.Select
' paste and position picture
.Paste
' Do While t > Now
' DoEvents
' Loop
' With .Pictures(1)
' .Left = .Left
' .Top = .Top
' End With
' export
.Export CStr(vBRFilePath)
End With
' remove no-longer-needed chart
.Delete
End With
End Sub
I am calling the same macro three times, each time extracting a different range.
Code:
Sub ExporttoJpg()
Call ExportOCRangeToPNG
Call ExportBRRangeToPNG
Call ExportSummaryRangeToPNG
End Sub
I can run the three macros a number of time then the error pops up. - But not at the same subroutine ?!?
I clear the error and it runs again.
The line it craps out, is always the .copypicture one but in different subs.
Any idea of what am I doing wrong?