tabbytomo
New Member
- Joined
- Jun 23, 2016
- Messages
- 18
Hiya guys! Looking for a bit of support, I'm looking to change a SaveAs dialogue to simply save with a predetermined file name and file path, but i'm struggling to understand how to change this code to do that.
This simply takes a range(currently only A1:D4), paste it as an image to a chart, then exports the chart object as a JPG. I don't want it to ask me for a file name and file path each time though, I'd love to be able to set that in code instead. Any ideas?
This simply takes a range(currently only A1:D4), paste it as an image to a chart, then exports the chart object as a JPG. I don't want it to ask me for a file name and file path each time though, I'd love to be able to set that in code instead. Any ideas?
VBA Code:
Sub SelectedRangeToImage()
Dim tmpChart As Chart, n As Long, shCount As Long, sht As Worksheet, sh As Shape
Dim fileSaveName As Variant, pic As Variant
'Create temporary chart as canvas
Set sht = Selection.Worksheet
Selection.Copy
sht.Pictures.Paste.Select
Set sh = sht.Shapes(sht.Shapes.Count)
Set tmpChart = Charts.Add
tmpChart.ChartArea.Clear
tmpChart.Name = "PicChart" & (Rnd() * 10000)
Set tmpChart = tmpChart.Location(Where:=xlLocationAsObject, Name:=sht.Name)
tmpChart.ChartArea.Width = sh.Width
tmpChart.ChartArea.Height = sh.Height
tmpChart.Parent.Border.LineStyle = 0
'Paste range as image to chart
sh.Copy
tmpChart.ChartArea.Select
tmpChart.Paste
'Save chart image to file
fileSaveName = Application.FileSave(fileFilter:="Image (*.jpg), *.jpg")
If fileSaveName <> False Then
tmpChart.Export Filename:="fileSaveName", FilterName:="jpg"
End If
'Clean up
sht.Cells(1, 1).Activate
sht.ChartObjects(sht.ChartObjects.Count).Delete
sh.Delete
End Sub