VBA to delete chart image in Word

edwardj3

New Member
Joined
Jan 16, 2018
Messages
41
Office Version
  1. 365
Platform
  1. Windows
Hi community. Hope you can help a VBA novice.

I've found some VBA code which enables charts in excel to be pasted in to a Word doc. See code below. I want to enhance this further so could you advise on how I do the following:
  • delete the charts in the word doc (40+ each with its own bookmark location e.g. Bookmark1?
  • When pasting, how I can paste to a specific size?

Current code

Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdRng As Word.Range


' get Word application if it's running

Set wdApp = GetObject(, "Word.Application")
On Error Resume Next

If wdApp Is Nothing Then
' word not running so start it and create document
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Add
Else
If wdApp.Documents.Count > 0 Then
' get active document
Set wdDoc = wdApp.ActiveDocument
Else
' no active document so create one
Set wdDoc = wdApp.Documents.Add
End If
End If

' get cursor location
Set wdRng = wdDoc.Bookmarks("Bookmark1").Range

' copy chart

Sheets("sheet1").ChartObjects("Chart 2").Chart.ChartArea.Copy


' paste chart
wdRng.PasteSpecial _
Link:=False, _
DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, _
DisplayAsIcon:=False

Thanks in advance.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Seems like this would delete all of your charts (and maybe other inlineshapes if they exist?). Untested...
Code:
Dim TempObj as Object
For each TempObj in  wdApp.wdDoc.InlineShapes
TempObj.delete
Next TempObj
Not sure how to change the chart size in Word. Maybe trial changing the size before the copy. HTH. Dave
 
Upvote 0
Thank you very much Dave. Slight amendment - Removed wdApp. and it worked.

I've changed the word doc to a macro version and added some code to change the chart size there. See below for info. Seems to do the job for me.

Dim Inline as InlineShape
For each Inline in ActiveDocument.InlineShapes
Inline.LockAspectRatio = msoFalse
Inline.Height = 250
Inline.Width = 525
Next Inline
 
Upvote 0
You are welcome. Thanks for posting your outcome and the additional code to size the chart in Word. Have a nice day. Dave
 
Upvote 0

Forum statistics

Threads
1,224,804
Messages
6,181,061
Members
453,017
Latest member
rlundbulls23

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