Logit
Well-known Member
- Joined
- Aug 31, 2016
- Messages
- 5,103
- Office Version
- 2019
- 2007
- Platform
- Windows
Cross Posted : msoFileDialogSaveAs - How To Implement In This Macro
The following macro will successfully save an embedded image to the DESKTOP. The path is hard coded.
I am needing to implement the msoFileDialogSaveAs so the USER can select where the embedded image is to be saved.
Please assist me in editing this code so it uses the msoFileDialogSaveAs object.
Thank you.
The following macro will successfully save an embedded image to the DESKTOP. The path is hard coded.
I am needing to implement the msoFileDialogSaveAs so the USER can select where the embedded image is to be saved.
Please assist me in editing this code so it uses the msoFileDialogSaveAs object.
Thank you.
VBA Code:
Sub SaveShapeAsPicture()
Dim cht As ChartObject
Dim ActiveShape As Shape
Dim UserSelection As Variant
'Ensure a Shape is selected
On Error GoTo NoShapeSelected
Set UserSelection = ActiveWindow.Selection
Set ActiveShape = ActiveSheet.Shapes(UserSelection.Name)
On Error GoTo 0
'Create a temporary chart object (same size as shape)
Set cht = ActiveSheet.ChartObjects.Add( _
left:=ActiveCell.left, _
Width:=ActiveShape.Width, _
top:=ActiveCell.top, _
Height:=ActiveShape.Height)
'Format temporary chart to have a transparent background
cht.ShapeRange.Fill.Visible = msoFalse
cht.ShapeRange.Line.Visible = msoFalse
'Copy/Paste Shape inside temporary chart
ActiveShape.Copy
cht.Activate
ActiveChart.Paste
'Save chart to User's Desktop as PNG File
cht.Chart.Export Environ("USERPROFILE") & "\Desktop\" & ActiveShape.Name & ".jpeg"
'Delete temporary Chart
cht.Delete
'Re-Select Shape (appears like nothing happened!)
ActiveShape.Select
Exit Sub
'ERROR HANDLERS
NoShapeSelected:
MsgBox "You do not have a single shape selected!"
Exit Sub
End Sub