I like Office VBA automation. Sometimes Microsoft makes it hard for me to remember that fact though.
The usual problem is that the easy stuff is easily automated with VBA. Which is good. But if you want to do something slightly irregular, dare I say even advanced, then you're in for a world of hurt.
When copying and pasting charts between Excel and Word, the traditional wisdom is to copy and paste as a picture because it's the most reliable reproduction of the original chart once pasted.
Fine if all you want is a static chart pasted into a Word document, but if you want to give the Word user the ability to modify the chart or data, then you need to copy and paste a chart object.
Although it's fully supported by the VBA interop runtime, the results are less than fantastic.
Here's some code that pastes an existing Excel Chart Object
Sub PasteChartAsInteractive(chart As Excel.chart1) Dim PageWidth As Long Dim WidthRatio As Double chart.ChartArea.Copy Dim myShape As Shape Selection.Style = ActiveDocument.Styles("Normal") Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter 'Commented out line is pasting as image. 'Selection.PasteSpecial Placement:=wdInLine, DataType:=wdPasteMetafilePicture Selection.PasteAndFormat (wdChart) Selection.EndOf Unit:=wdParagraph, Extend:=wdMove Selection.TypeParagraph Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft Selection.range.ListFormat.ApplyBulletDefault Selection.TypeText "..." + vbCr End Sub
But the problem with the inserted chart is that the kerning is all messed up. Observe the results.
The kerning fault is especially noticeable in the title above. There is virtually no spacing between characters in any letters. Some even appear to overlap.
Google searches mostly turn up the workaround of 'paste as image', which was not an option for me, the chart had to be editable and live.
I had given up and was going to move away from Excel Charts and into using Word Graph Objects This was going to be an ordeal because I had a bunch of existing charts in this macro, pasted as images, and the formatting all needed to be consistent. My impression is that the graph and chart API's are not really that similar.
Then I stumbled on a solution with Clouseau-esque serendipity. Changing the object layout (Format -> Object -> Layout) to use 'Square' rather than 'In-line' made the kerning problem disappear. What's more, I could change the layout back to 'In-line' and kerning was still proper.
There was a minor issue with finding the appropriate commands to do this with VBA. Normally I record a macro of the action I want to reproduce if I'm not totally sure how. With macros, the Layout tab is disabled, so I had to dig around online to find the VBA.
Sub PasteChartAsInteractive(chart As Excel.chart) Dim PageWidth As Long Dim WidthRatio As Double chart.ChartArea.Copy Dim myShape As Shape Selection.Style = ActiveDocument.Styles("Normal") Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter Selection.PasteAndFormat (wdChart) Set myShape = Selection.Paragraphs(1).range.InlineShapes(1).ConvertToShape myShape.ConvertToInlineShape Selection.EndOf Unit:=wdParagraph, Extend:=wdMove Selection.TypeParagraph Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft Selection.range.ListFormat.ApplyBulletDefault Selection.TypeText "..." + vbCr End SubAnd the result
It's too bad that we have to resort to these cheap tricks when trying to get members of the Office suite to behave together. You would think by now, the integration would be much tighter.
By the way, did you notice that the Y-axis title is truncated in both images? The fix for that is to add superfluous characters to the Y-axis title until all the important characters show.