vba 指定形状宽度会产生错误:指定的维度对当前图表类型无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11447914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specifying shape width generates error: The specified dimension is not valid for the current chart type
提问by user1099480
I am trying to save Excel diagrams as images.
我正在尝试将 Excel 图表保存为图像。
With the whole code I call several workbooks, lookup all worksheets and save all diagrams.
使用整个代码,我调用了几个工作簿,查找所有工作表并保存所有图表。
The following code works with Excel 2007 and 2010 (but there is a horizontal and vertical line visible because of the + 4). If I change the Round(shp.Width + 4, 0)
to Round(shp.Width, 0)
I get the following error in 2010 (but not in 2007):
以下代码适用于 Excel 2007 和 2010(但由于 + 4,水平线和垂直线可见)。如果我改变了Round(shp.Width + 4, 0)
对Round(shp.Width, 0)
我得到了2010以下错误(但不是在2007年):
Run-Time error '-2147467259 (80004005)':
The specified dimension is not valid for the current chart type.
运行时错误“-2147467259 (80004005)”:
指定的维度对于当前图表类型无效。
Dim shp As Shape
Dim sht As Worksheet
Set sht = Application.ActiveWorkbook.Sheets(shtName)
Set shp = sht.Shapes(chrtName)
shp.CopyPicture xlScreen, xlBitmap
Dim objChart As ChartObject
Set objChart = sht.ChartObjects.Add(200, 200, Round(shp.Width + 4, 0), Round(shp.Height + 4, 0))
objChart.Activate
ActiveChart.Paste
ActiveChart.Export Filename:=fullPath, Filtername:=Right(fullPath, 3)
objChart.Delete
How I can avoid using the +4?
如何避免使用 +4?
I found the following:
我发现了以下内容:
"The problem occurs whenever the default chart type is set to other chart types than the ones you are trying to create. For an example, if you try to create line chart and if the default chart in Excel is OHLC (Candlestick Stock Chart) then Excel will be fast to complain "The specified dimension is not valid for the current chart type". The same occurs even if you try to create charts from VB.NET. Hence, first of all change the default chart type to some basic chart types like Line charts. The problem will be solved." http://www.excelbanter.com/showthread.php?t=204071
“只要将默认图表类型设置为您尝试创建的图表类型以外的其他图表类型,就会出现问题。例如,如果您尝试创建折线图,并且 Excel 中的默认图表是 OHLC(烛台股票图表),那么Excel 会很快抱怨“指定的维度对当前图表类型无效”。即使您尝试从 VB.NET 创建图表也会发生同样的情况。因此,首先将默认图表类型更改为一些基本图表类型就像折线图一样。问题将得到解决。” http://www.excelbinter.com/showthread.php?t=204071
How can I do this with VBA?
我怎样才能用 VBA 做到这一点?
回答by Jon Peltier
Long time no answer.
好久没有回复了。
You need to do two things. First, make sure no data is selected, or Excel may try to put it into the chart you're creating.
你需要做两件事。首先,确保未选择任何数据,否则 Excel 可能会尝试将其放入您正在创建的图表中。
Insert this block
插入这个块
Dim rngTmp as range
If Typename(Selection) = "Range" then
Set rngTmp = selection
End If
With ActiveSheet
.Range("A1").Offset(.Rows.Count - 1).Select
End With
before the line
行前
Set objChart = sht.ChartObjects.Add(...)
and insert the following after the above line
并在上述行之后插入以下内容
ObjChart.Chart.ChartType = xlLine ' or another "safe" chart type
If Not rngTmp is Nothing then
rngTmp.select
End If
Strictly speaking, if you are not adding data to the chart, you don't need to change the chart type, but for the sake of OCD, it's better to be complete.
严格来说,如果不是在图表中添加数据,则不需要更改图表类型,但是为了OCD,最好是完整的。
回答by OfficeAddinDev
Use
用
Set objChart = Worksheet.Shapes.AddChart2().Chart.Parent
Set objChart = Worksheet.Shapes.AddChart2().Chart.Parent
instead of
代替
Set objChart = Worksheet.ChartObjects.Add()
Set objChart = Worksheet.ChartObjects.Add()
The former function allows you to specify a basic chart type in the second argument.
前一个函数允许您在第二个参数中指定基本图表类型。
回答by Roger Fernandes
I got the same exception. I traced the problem to the save path.
我得到了同样的例外。我将问题追溯到保存路径。
If the save path is wrong, or the specified path does not exist, then this exception will be raised. Check to make sure that the path exists.
如果保存路径错误,或者指定的路径不存在,则会引发此异常。检查以确保路径存在。