试图在Mac Excel 2016中将范围另存为jpeg图像。我运行VB代码,首先出现此错误:“运行时错误'-2147287035(80030005)':指定的尺寸对于当前图表无效类型。”如果我依次单击“调试”和“F5”,则会出现以下错误:“运行时错误'70':权限被拒绝”。
根据我的研究,前者要么是沙盒问题,要么是图表问题。我什至添加了grantAccessToMultipleFiles进行修复,但没有帮助。
有问题的部分:
.Chart.Export Filename:=ThisWorkbook.Path & "/william.jpg", Filtername:="JPG"
这是完整的代码:
Sub SaveImage()
'from stackoverflow originally
Dim sSheetName As String
Dim oRangeToCopy As Range
Dim Lastrow As Integer
Dim manager As String
manager = Worksheets("by Mgr").Range("C6").Value
Set b = Worksheets("by Mgr").Range("T:T").Find(what:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues)
Lastrow = b.Row
sSheetName = "by Mgr" ' worksheet to work on
With Worksheets(sSheetName).Range("A1:T" & Lastrow)
.CopyPicture xlScreen, xlPicture
'Getting the Range height
PicHeight = .Height
'Getting the Range Width
PicWidth = .Width
End With
filePermissionCandidates = Array(ThisWorkbook.Path)
fileAccessGranted = GrantAccessToMultipleFiles(filePermissionCandidates)
With Worksheets(sSheetName)
'Creating the Chart
.ChartObjects.Add(30, 44, PicWidth, PicHeight).name = manager
With .ChartObjects(manager)
'Pasting the Image
.Chart.Paste
'Exporting the Chart
.Chart.Export Filename:=ThisWorkbook.Path & "/william.jpg", Filtername:="JPG"
End With
.ChartObjects(manager).Delete
End With
End Sub
我快要结束了,任何想法都值得赞赏。
最佳答案
您的路径分隔符似乎不正确。请尝试以下操作:
.Chart.Export Filename:=ThisWorkbook.Path & Application.PathSeparator & "william.jpg", Filtername:="JPG"
请注意,我删除了对
GrantAccessToMultipleFiles
的调用,它似乎可以在我的计算机上使用。