Guys, I am trying to convert all charts of workbooks in a selected folder into picture. It is working properly during few samples trial. But errors keep coming out when there are a large amount of charts there. Anyway, to solve that? My vba codes are listed below:
Private Sub CommandButton1_Click()
'PURPOSE: Loop through every graph in the active workbook
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & ""
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Convert all Chart into Graph
Dim sht As Worksheet
Dim CurrentSheet As Worksheet
Dim cht0 As ChartObject
Dim dTop As Double, dLeft As Double
Set CurrentSheet = ActiveSheet
For Each sht In ActiveWorkbook.Worksheets
With sht
'loop through the sheet of the active worksheet
For Each chtO In .ChartObjects
chtO.Copy
.PasteSpecial Format:="Bitmap", Link:=False, DisplayAsIcon:=False
With .Shapes(.Shapes.Count)
.Top = chtO.Top
.Left = chtO.Left
.Name = chtO.Name & "_pic"
End With
chtO.Delete
Next chtO
End With
Next sht
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Private Sub CommandButton1_Click()
'PURPOSE: Loop through every graph in the active workbook
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & ""
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Convert all Chart into Graph
Dim sht As Worksheet
Dim CurrentSheet As Worksheet
Dim cht0 As ChartObject
Dim dTop As Double, dLeft As Double
Set CurrentSheet = ActiveSheet
For Each sht In ActiveWorkbook.Worksheets
With sht
'loop through the sheet of the active worksheet
For Each chtO In .ChartObjects
chtO.Copy
.PasteSpecial Format:="Bitmap", Link:=False, DisplayAsIcon:=False
With .Shapes(.Shapes.Count)
.Top = chtO.Top
.Left = chtO.Left
.Name = chtO.Name & "_pic"
End With
chtO.Delete
Next chtO
End With
Next sht
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub