Hi guys...
I have a little problem... I have the weekly menu of a restaurant that I try to save in a .png file as I need to publish it on our website... But, when I run the macro, it stops on:
ActiveSheet.CopyPicture Format:=xlPicture
Here is the full macro:
Sub SaveAsPNG()
Dim FileLocation As String
Dim FileName As String
Dim SheetName As String
Dim FilePath As String
Dim ParamSheet As Worksheet
' Set a reference to the "param" worksheet
Set ParamSheet = ThisWorkbook.Worksheets("param")
' Get the directory location from cell E14 of the "param" worksheet
FileLocation = ParamSheet.Range("E14").Value
' Get the filename from cell E15 of the "param" worksheet
FileName = ParamSheet.Range("E15").Value
' Get the active sheet's name
SheetName = ActiveSheet.Name
' Define the file path and name based on the location and filename
FilePath = FileLocation & "\" & FileName & ".png"
' Export the active sheet as a .png file
ActiveSheet.CopyPicture Format:=xlPicture
With ActiveSheet.Shapes.AddPicture(FilePath, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=0, Top:=0, Width:=1, Height:=1)
.LockAspectRatio = msoTrue
.Width = ActiveSheet.UsedRange.Width
.Height = ActiveSheet.UsedRange.Height
End With
ActiveSheet.Shapes(SheetName).Delete
End Sub
Knowing that:
I use this way, as the filename is composed by several params located in another sheet...
If anybody has a better idea on how to write or correct this macro, you're welcome!
Thank you for your always valuable help...
I have a little problem... I have the weekly menu of a restaurant that I try to save in a .png file as I need to publish it on our website... But, when I run the macro, it stops on:
ActiveSheet.CopyPicture Format:=xlPicture
Here is the full macro:
Sub SaveAsPNG()
Dim FileLocation As String
Dim FileName As String
Dim SheetName As String
Dim FilePath As String
Dim ParamSheet As Worksheet
' Set a reference to the "param" worksheet
Set ParamSheet = ThisWorkbook.Worksheets("param")
' Get the directory location from cell E14 of the "param" worksheet
FileLocation = ParamSheet.Range("E14").Value
' Get the filename from cell E15 of the "param" worksheet
FileName = ParamSheet.Range("E15").Value
' Get the active sheet's name
SheetName = ActiveSheet.Name
' Define the file path and name based on the location and filename
FilePath = FileLocation & "\" & FileName & ".png"
' Export the active sheet as a .png file
ActiveSheet.CopyPicture Format:=xlPicture
With ActiveSheet.Shapes.AddPicture(FilePath, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=0, Top:=0, Width:=1, Height:=1)
.LockAspectRatio = msoTrue
.Width = ActiveSheet.UsedRange.Width
.Height = ActiveSheet.UsedRange.Height
End With
ActiveSheet.Shapes(SheetName).Delete
End Sub
Knowing that:
- I have a sheet called PARAM
- In sheet PARAM, the destination folder is located in the cell E14
- In sheet PARAM the filename is located in the cell E15
I use this way, as the filename is composed by several params located in another sheet...
If anybody has a better idea on how to write or correct this macro, you're welcome!
Thank you for your always valuable help...