gravanoc
Active Member
- Joined
- Oct 20, 2015
- Messages
- 351
- Office Version
- 365
- Platform
- Windows
- Mobile
I've got a userform that is populated with some checkboxes and image controls displaying a section of a worksheet. It is successful probably about 85% of the time. The other times it just doesn't export the picture to the folder properly, and when it populates it looks blank or like an empty chart (since a chart is used in the process). If I run the code step by step instead of like normal it will always work, so something like the paste command is not consistently working. Here is the code involved:
This is how it should look:
This is how it looks sometimes:
VBA Code:
Sub ExportAsPicture(Optional choice As String, Optional bAddImages As Boolean, Optional y As Long)
Dim fso As Object, aFold As Object
Dim aFile As Object
Dim thePic As Picture
Dim rngView(0 To 3) As Range
Dim MyChart As String, MyPicture As String
Dim PicWidth As Long, PicHeight As Long
Dim fileName As String
Dim filePath As String
Dim i As Long, X As Long
' choice = wksDefinitions.Range("CF20")
filePath = Environ("UserProfile") & "\OneDrive\Pictures\Budget Tool\"
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set aFold = fso.GetFolder(filePath)
If Err.Number <> 0 Then
Set aFold = fso.CreateFolder(filePath)
End If
On Error GoTo 0
If bAddImages = False Then
wksDefinitions.Range("BY18").Offset(0, 0).Value = ""
wksDefinitions.Range("BY18").Offset(0, 1).Value = ""
wksDefinitions.Range("BY18").Offset(0, 2).Value = ""
wksDefinitions.Range("BY18").Offset(0, 3).Value = ""
X = 3
Else
X = 0
End If
Application.ScreenUpdating = False
wksDashboard.Activate
For i = 0 To X
Set rngView(i) = wksDashboard.Range("View" & i + 1)
If InStr(1, LCase(choice), "view " & (i + y) + 1) Then
DoEvents
rngView(i).CopyPicture Appearance:=xlScreen, format:=1
ActiveSheet.Paste
Set thePic = Selection
fileName = Replace(Replace(Replace(Replace(Now, ":", ""), "/", ""), " ", ""), "PM", "") & (i + y)
MyPicture = Selection.Name
With Selection
PicHeight = .ShapeRange.Height
PicWidth = .ShapeRange.Width
End With
wksDashboard.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.Location Where:=xlLocationAsObject, Name:="Dashboard"
Selection.Border.LineStyle = 0
MyChart = Selection.Name
With wksDashboard
With .Shapes(MyChart)
.Width = PicWidth
.Height = PicHeight
End With
.Shapes(MyPicture).Copy
With ActiveChart
.ChartArea.Select
.Paste
End With
.ChartObjects(1).Chart.Export fileName:=filePath & fileName & ".jpg", FilterName:="jpg"
.Shapes(MyChart).Delete
End With
thePic.Delete
wksDefinitions.Range("Attachments").Offset(0, i).Value = "\Pictures\Budget Tool\" & fileName & ".jpg"
End If
Next i
wksTracker.Activate
Application.ScreenUpdating = True
Exit Sub
Finish:
End Sub
This is how it should look:
This is how it looks sometimes: