VBA导出为jpg

问题描述 投票:0回答:1

我设法实现了使用图表导出为JPG的代码。然而,当我试图改变文件导出路径时,我得到的错误是 运行时错误1004。方法 "范围对象"_全球失败 我不明白代码不读什么,因为路径都差不多。

如果有人能帮忙,我将感激不尽。

jpg导出为*Path = ThisWorkbook.Path*Path = ThisWorkbook.Path & "\" & Range("PDF_Folder").Value。

不输出到 *Path = ThisWorkbook.Path & "\" & Range("New_Folder").Value。

'//Creat a jpg image using charts
 Dim rng As Range
 Dim shtTemp As Worksheet
 Dim chtTemp As Chart
 Application.ScreenUpdating = False
 '// Range
 Set rng = Worksheets("Receipt").Range("B2:E27")
 shReceipt.rows("2:27").EntireRow.AutoFit
 '// Add a temp worksheet. Chart will be placed on this. It will be deleted after
 Set shtTemp = Worksheets.Add
 '// Add chart
 Charts.Add
 '// Move the chart to the new sheet and get a reference to it
 ActiveChart.Location Where:=xlLocationAsObject, Name:=shtTemp.Name
 Set chtTemp = ActiveChart
 '// Copy and paste the range
 rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
 chtTemp.Paste

 With ActiveChart.Parent
.Height = 520 ' resize
.Width = 360 ' resize
 End With


 '// Export

ChDir ActiveWorkbook.Path
Dim Name As String, Path, filename
Name = InvTable.ListColumns("Customer").DataBodyRange(r).Value
Dim Inv As Double
Inv = InvTable.ListColumns("Invoice Number").DataBodyRange(r).Value

filename = "\" & Inv & "_" & Name & ".jpg"
''//This PDF folder is recognised-no error
Path = ThisWorkbook.Path & "\" & Range("PDF_Folder").Value

''//This path is regonised-no error
'Path = ThisWorkbook.Path

''//This New folder creates an error (it does exist in the workbook path
''Path = ThisWorkbook.Path & "\" & Range("New_Folder").Value
chtTemp.Export filename:=Path & filename
excel vba filepath chdir
1个回答
0
投票

所以我得到了答案。"Excel_Folder "是一个命名的单元格中的范围(1,1) "PDF_Folder "是一个命名的单元格中的范围(1,2)这些范围的范围都是工作簿。

我把 "New_Folder "定义为命名范围,结果找到了路径。

© www.soinside.com 2019 - 2024. All rights reserved.