Setting width and height of the ChartObject
or its Shape
doesnt do it. They both seem to be just some inner part of that white rectangle which is the embedded chart. Also the ChartArea
doesn't seem to be the object I'm interested in.
I want the exported file in the following example to have dimensions 800x600 (and I don't mean rescaling the exported image or trial and error until the size accidentally fits). There must be some object around the chart which I have overlooked.
Sub mwe()
Dim filepath As String
Dim sheet As Worksheet
Dim cObj As ChartObject
Dim c As Chart
Dim cShape As Shape
Dim cArea As chartArea
filepath = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\"))
Set sheet = ActiveSheet
Set cObj = sheet.ChartObjects(1)
Set c = cObj.chart
Set cShape = sheet.Shapes(cObj.Name)
Set cArea = c.chartArea
cObj.Width = 800
cObj.Height = 400
MsgBox cArea.Width & " x " & cArea.Height '793x393
c.Export filepath & "test1.png" '1067x534, this is also the size on screen
cShape.Width = 800
cShape.Height = 400
MsgBox cArea.Width & " x " & cArea.Height '794x393
c.Export filepath & "test2.png" '1068x534, this is also the size on screen
End Sub
update:
It turns out the ChartObject
and the Shape
belonging to the same Chart
already have the Worksheet
as parent but width and height are not specified in pixels but in points, where 1 point = 1/72 inches, and most of the time, Windows seems to assume 96 pixels per inch.
Thanks to Steve's comment I am now using the following, which is quite reliable.
A ChartObject
that is not activated at the moment of export seems to produce a file where width and height is 1 higher than it should be.
It remains to find out how to determine the factors px2ptH
and px2ptV
automatically.
Sub mwe()
Dim filepath As String
Dim sheet As Worksheet
Dim cObj As ChartObject
Dim c As Chart
Dim px2ptH As Double: px2ptH = 72 / 96
Dim px2ptV As Double: px2ptV = 72 / 96
Dim w As Double: w = 800 * px2ptH
Dim h As Double: h = 400 * px2ptV
filepath = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\"))
Set sheet = ActiveSheet
Set cObj = sheet.ChartObjects(1)
Set c = cObj.Chart
'otherwise image size may deviate by 1x1
cObj.Activate
cObj.Width = w
cObj.Height = h
c.Export filepath & "test.png"
End Sub
Click the chart, and then drag the sizing handles to the size that you want. Click the chart, and then on the Format tab, in the Size group, enter the size in the Shape Height and Shape Width boxes.
By default, Excel charts will automatically move and resize when cells underneath them change. We can see this behavior in action if we increase or decrease the width of any columns underneath the chart. The same is true of rows. If we increase or decrease row heights, the chart automatically expands and contracts.
The dimensions are in points, not pixels. 72 points to the inch.
800 pixels / 72 = 11.11111.... The size of the exported image will be the size in inches/the dpi of your computer's display, usually 96 (as it is in your case, but you can't always count on it ... use WIN API calls to find the current value)
Randy Birch has published code you can use to access the WinAPI calls to get screen resolution and more.
Go to http://vbnet.mvps.org/index.html and use the search feature on the left to look up GETDEVICECAPS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With