Chart export saves a watermark instead of image when picture inserted into Worksheet using VBA

morsagmon

New Member
Joined
May 6, 2015
Messages
28
Hi all.

I want to save a picture (shape) from my Worksheet as a file, using VBA.
When I manually insert the picture into the Worksheet and name it - all works perfect.
When I use VBA to insert the picture and name it - instead of saving the image, the resulting file is a picture of a grey watermark I have no idea where it was invented.
See my comments in code that wraps the VBA section that causes this.


Code:
Sub TestChartExport()


    'IF THIS PICTURE INSERT IS DONE MANUALLY - ALL WORKS FINE
    Dim strPicturePath As String
    strPicturePath = "C:\Users\morsa\Documents\Personal\Career & Personal Development\Sigmatek-IS\Marketing\Branding\Media\LinkedIn_Icon_dark_bg.JPG"
    ThisWorkbook.Worksheets("Sheet1").Pictures.Insert(strPicturePath).Select
    Selection.Name = "LogoImage"
    'END OF AUTOMATIC SECTION THAT "HIDES" THE EXPORTED PICTURE WITH A WATERMARK PICTURE


    Dim strResult As String
    strResult = SaveImageAsFile("Sheet1", "LogoImage")
    Debug.Print (strResult)
End Sub




Function SaveImageAsFile(strSheetName As String, strShapeName As String) As String
'Saves strShapeName that is on strSheetName on current application folder and returns the full path and filename


     Dim MyChart As String
     Dim PicWidth As Long, PicHeight As Long
     Dim strFullFileName As String


     Application.ScreenUpdating = False
     On Error GoTo FailedSave
     
     strFullFileName = Application.ActiveWorkbook.Path & "\LogoImage.jpg"
     
     Worksheets(strSheetName).Shapes.Range(Array(strShapeName)).Select
     With Selection
           PicHeight = .ShapeRange.Height
           PicWidth = .ShapeRange.Width
     End With


     Charts.Add
     ActiveChart.Location Where:=xlLocationAsObject, Name:=strSheetName
     Selection.Border.LineStyle = 0
     MyChart = Selection.Name & " " & Split(ActiveChart.Name, " ")(2)


     With Worksheets(strSheetName)
           With .Shapes(MyChart)
                 .Width = PicWidth
                 .Height = PicHeight
           End With


           .Shapes(strShapeName).Copy


           With ActiveChart
                 .ChartArea.Select
                 .Paste
           End With
    
           .ChartObjects(1).Chart.Export Filename:=strFullFileName, Filtername:="jpg"
           .Shapes(MyChart).Delete
     End With
     SaveImageAsFile = strFullFileName
CloseSub:
     Application.ScreenUpdating = True
     Exit Function
FailedSave:
    SaveImageAsFile = ""
    GoTo CloseSub
End Function
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try using Shapes.AddPicture instead...

Code:
ThisWorkbook.Worksheets("Sheet1").Shapes.AddPicture . . .
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top