Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Word Range Operations Seem Different

Tags:

ms-word

vba

In using or setting Ranges, some Ranges behave differently that other Ranges. For example,

ActiveDocument.Range(10, 20).Select
ActiveDocument.Tables(2).Cell(1, 1).Range(10, 20).Select

The first line if OK and works as expected. The second line produces an error on the Range statement, although it seems that two lines should be identical for this.

What is the difference?

like image 563
ForEachLoop Avatar asked Feb 03 '26 14:02

ForEachLoop


1 Answers

Range on a Cell is a property, not a method -- it returns the range of the document represented by the cell.

This means you can't add arguments like (10,20).

The following is equivalent:

 Dim rangeStart As Integer
 rangeStart = ActiveDocument.Tables(2).Cell(1, 1).Range.Start
 ActiveDocument.Range(rangeStart + 10, rangeStart + 20).Select
like image 176
Jay Avatar answered Feb 05 '26 09:02

Jay