I see a few things now that I look at this more closely. The first snippet is Excel code, this doesn't run and can't run in Word, and in fact it's raising a compile error.
When I try running this in Word VBA, I get the error
ActiveChart.Axes(xlCategory).Select
With Selection.Border
.ColorIndex = 57
.Weight = xlMedium
.LineStyle = xlContinuous
End With
The compiler highlights .Border
because there is no Border
property of a Selection
object in Word.
The Selection
object is a bit ambiguous, because it can be different things. Think of it like a variant which refers to whatever is selected or the cursor point if nothing is selected.
So that explains the method or data member not found
which isn't a run-time error, but a compile error suggesting that your code cannot possibly execute as-is.
If you replace Select
and Selection
with the object itself, like so:
With ActiveChart.Axes(xlCategory).Border
.ColorIndex = 57
.Weight = xlMedium
.LineStyle = xlContinuous
End With
You'll get another error, because there is no such thing as ActiveChart
in Word, that is an Excel object only. (Using Option Explicit
would have alerted you to this problem). In word, you need to refer to the shape or chart, and you can do this with a handle on the shape as you have done, with a variable. Try:
With salesChart.Axes(xlCategory).Border
.ColorIndex = 57
.Weight = xlMedium
.LineStyle = xlContinuous
End With
This might be useful:
How to avoid using Select in Excel VBA macros
While written for Excel specifically, it is a better programming concept: Select
is a pretty crude way of doing most things, and although there are some exceptions, nearly everything you can do with VBA can be done more efficiently by directly working with the object, rather than first selecting and then acting on the selection -- it's redundant. It's also messy because you need to constantly keep selecting things and your code is necessarily ambiguous, referring to vague Selection
object (variant) rather than strongly typed object, etc.