一个看似简单的问题.但设定的宽度和高度ChartObject或它的Shape犯规做到这一点,他们都似乎是,白色矩形这是嵌入式图表的只是一些内部部分.也ChartArea似乎不是我感兴趣的对象.
我想要的是以下示例中的导出文件的尺寸为800x600(我并不意味着重新缩放导出的图像或试验和错误,直到大小意外地适合).图表周围一定有一些我忽略的对象.
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
更新:
事实证明ChartObject,Shape属于同一个人Chart已经拥有了Worksheet父母.但宽度和高度没有像我假设的那样以像素为单位指定,而是以点为单位,其中1点= 1/72英寸.
在大多数情况下,Windows似乎假设每英寸96像素.
感谢史蒂夫的评论,我现在正在使用以下内容,这是非常可靠的.一个ChartObject未在其出口的瞬间激活似乎产生一个文件,其中的宽度和高度为1大于它应该是.
它仍然是找出如何确定的因素px2ptH和px2ptV自动.
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
Ste*_*erg 10
尺寸以点为单位,而不是像素.72点到英寸.
800像素/ 72 = 11.11111 ....导出图像的大小将以英寸/计算机显示器的dpi为单位,通常为96(与您的情况一样,但您不能总是指望它. ..使用WIN API调用来查找当前值)
Randy Birch发布了可用于访问WinAPI调用以获取屏幕分辨率等的代码.
转到http://vbnet.mvps.org/index.html并使用左侧的搜索功能查找GETDEVICECAPS