if a specific chart exists in excel file then Paste it to the word template file

Chahine

New Member
Joined
Oct 8, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
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.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi Chahine. I think U can just do something like this...
Code:
Dim objCht as Chartobject
Worksheets("Graphs").Activate
On Error Resume Next
Set objCht = ActiveSheet.ChartObjects("CashFlow1")
If Not objCht Is Nothing Then
On Error Goto 0
'your code
Else
'no chart
On Error Goto 0
End if
HTH. Dave
ps. please use code tags
 
Upvote 0
Solution
Hi Chahine. I think U can just do something like this...
Code:
Dim objCht as Chartobject
Worksheets("Graphs").Activate
On Error Resume Next
Set objCht = ActiveSheet.ChartObjects("CashFlow1")
If Not objCht Is Nothing Then
On Error Goto 0
'your code
Else
'no chart
On Error Goto 0
End if
HTH. Dave
ps. please use code tags
Thank you so much really helpful way. it works
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,680
Members
449,463
Latest member
Jojomen56

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top