COM access to Excel: Avoid directly subscripting a collection

  • Follow


Another example of when it is dangerous to assume a VBA syntax in
using the COM interface:

>> xlApp.ActiveSheet.Range('A2:B3').Rows(1).Address
ans =
$A$2:$B$3
>> xlApp.ActiveSheet.Range('A2:B3').Rows.Item(1).Address
ans =
$A$2:$B$2

Anyway, in case anyone is googling this, it seems like an important
"best practice".
0
Reply paul.domaskis (115) 6/3/2012 12:45:26 AM

On Jun 2, 8:45 pm, Paul <paul.domas...@gmail.com> wrote:
> Another example of when it is dangerous to assume a VBA syntax in
> using the COM interface:
>
> >> xlApp.ActiveSheet.Range('A2:B3').Rows(1).Address
>
> ans =
> $A$2:$B$3>> xlApp.ActiveSheet.Range('A2:B3').Rows.Item(1).Address
>
> ans =
> $A$2:$B$2
>
> Anyway, in case anyone is googling this, it seems like an important
> "best practice".

Actually, this works too.  xlApp.Selection.get('Rows',1).Address

It's unfortunate, this ambiguity in the syntax that can lead to a bit
of guesswork in coding.  I find myself constantly having to lookup my
own code to ensure that I'm not taking a mis-step.  It probably can't
be avoided, because we're talking two different languages (matlab and
VBA or COM), so it boils down to capturing best practices regarding
what works and what doesn't.
0
Reply paul.domaskis (115) 6/3/2012 1:05:20 AM


1 Replies
36 Views

(page loaded in 0.038 seconds)

Similiar Articles:












7/2/2012 10:20:16 AM


Reply: