Dear all,
I am writing a vba that synchronizes a word file, which is a template, with the excel file. at some point I need to paste a chart to the word file.
Dim wdApp As Word.Application
Dim doc As Word.Document
Dim CashFlow1 As Object
Set wdApp = New Word.Application
wdApp.Visible = True
wdApp.Activate
wdApp.Documents.Add (TEMPLATE_REPORT_PATH)
Worksheets("Graphs").Activate
Set CashFlow1 = ActiveSheet.ChartObjects("CashFlow1")
CashFlow1.Chart.ChartArea.Copy
With wdApp.Selection
.Goto what:=-1, Name:="G1"
.PasteSpecial link:=True
End With
this Code works perfectly. But assuming that I don't have the CashFlow1 chart I came out with the solution bellow but it shows me an error: "Run time error 9: Subscript out of range"
Worksheets("Graphs").Activate
If Charts("CashFlow1") <> "" Then
Set CashFlow1 = ActiveSheet.ChartObjects("CashFlow1")
CashFlow1.Chart.ChartArea.Copy
With wdApp.Selection
.Goto what:=-1, Name:="G1"
.PasteSpecial link:=True
End With
End If
So, how can I wrtie in VBA if a chart Exists then .....
thank you.
I am writing a vba that synchronizes a word file, which is a template, with the excel file. at some point I need to paste a chart to the word file.
Dim wdApp As Word.Application
Dim doc As Word.Document
Dim CashFlow1 As Object
Set wdApp = New Word.Application
wdApp.Visible = True
wdApp.Activate
wdApp.Documents.Add (TEMPLATE_REPORT_PATH)
Worksheets("Graphs").Activate
Set CashFlow1 = ActiveSheet.ChartObjects("CashFlow1")
CashFlow1.Chart.ChartArea.Copy
With wdApp.Selection
.Goto what:=-1, Name:="G1"
.PasteSpecial link:=True
End With
this Code works perfectly. But assuming that I don't have the CashFlow1 chart I came out with the solution bellow but it shows me an error: "Run time error 9: Subscript out of range"
Worksheets("Graphs").Activate
If Charts("CashFlow1") <> "" Then
Set CashFlow1 = ActiveSheet.ChartObjects("CashFlow1")
CashFlow1.Chart.ChartArea.Copy
With wdApp.Selection
.Goto what:=-1, Name:="G1"
.PasteSpecial link:=True
End With
End If
So, how can I wrtie in VBA if a chart Exists then .....
thank you.