Word2003: Obtaining COLUMNS in a table

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Word2003: Obtaining COLUMNS in a table

Post by ChrisGreaves »

Code: Select all

strRowText = tbl.Rows(lngRow).Range.Text
strRowText = UW.strNot(strRowText, Chr(13))
strRowText = UW.strNot(strRowText, Chr(7))
If 0 = Len(strRowText) Then
    tbl.Rows(lngRow).Delete
Else
End If

Code: Select all

strColumnText = Selection.Text
strColumnText = UW.strNot(strColumnText, Chr(13))
strColumnText = UW.strNot(strColumnText, Chr(7))
If 0 = Len(strColumnText) Then
    tbl.Columns(lngColumn).Delete
Else
End If
In Word2003 I am hunting for and deleting empty rows and columns in a table.
The first chunk of code works fine; I grab “tbl.Rows(lngRow).Range.Text” and set to work.
The second chunk of code is not so easy, there is no Word2003/VBA construct for “tbl.Columns(lngColumn).Range.Text”.
Nor does the use of Selection.Text work – that selects only one cell, not all the cells in the chosen column.
The solution for me is to write an inner loop, looping along all the cells in that column to aggregate the text.

In the meantime I ponder this. I suppose that a table’s data is stored in memory row-wise, so that obtaining the data for .Rows(i) is straightforward, but to deliver the data from .Columns(i) would involve the MSoft programmer in an inner loop, looping along all the cells in that column to aggregate the text. (Better that I do their dirty work for them!)

I get that.

At a higher level though, I can’t come up with a reason why the VBA programmer (me!) shouldn’t be able to refer to, and gain access to, .Columns(i).
The inner workings of the interpreter ought to be transparent to me. At my level I am dealing with tables, and tables consist of rows and columns. The internal implementation of the Rows and Columns ought to be irrelevant to me.
Excepting, maybe, a warning in the Advanced section that “accessing data by .Columns(i) can be measurable slower than accessing data by .Rows(i)”. This is like the old 11.5μs warnings we used to see in the Good Old Days of assembly-language programming.

Put your hand up if you have ever used “Table, Select, Column”

OK. You can all put your hands down now … Why can an end-user access columns but not the poor VBA programmer?

Code: Select all

Function strGetColumnText(tbl As Table, lngColumn As Long) As String
    Dim strResult As String
    Dim lngRow As Long
    For lngRow = 1 To tbl.Rows.Count
        strResult = strResult & tbl.Rows(lngRow).Cells(lngColumn).Range.Text
    Next lngRow
    strGetColumnText = strResult
'Sub TESTstrGetColumnText()
'    Debug.Print strGetColumnText(Selection.Tables(1), 4)
'End Sub
End Function
Cheers
Chris
An expensive day out: Wallet and Grimace

User avatar
HansV
Administrator
Posts: 78229
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word2003: Obtaining COLUMNS in a table

Post by HansV »

There is no support for non-contiguous ranges in Word VBA whatsoever.

It has been possible for quite a while now to select multiple ranges by holding down Ctrl, and to select rectangular areas (technically multiple ranges) by holding down Alt. These selections can be manipulated in the interface.

But there is no equivalent for this in VBA.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Word2003: Obtaining COLUMNS in a table

Post by ChrisGreaves »

HansV wrote:
22 May 2020, 11:55
...But there is no equivalent for this in VBA.
Quite so, and thank you, Hans.
But can you (or anyone else) come up with a good reason why it ought not be implemented?
I think of the VBA programmer as someone who should be able to mimic (and of course exceed) the end-user's actions.
Cheers
Chris
An expensive day out: Wallet and Grimace

User avatar
HansV
Administrator
Posts: 78229
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word2003: Obtaining COLUMNS in a table

Post by HansV »

Only Microsoft can answer that...

(Off-topic: in Word and Excel it is to a large extent possible to mimic end-user actions in VBA, even though there are exceptions, as discussed above. But Outlook VBA focuses almost exclusively on manipulating email messages, appointments etc. There is very little support for mimicking end-user actions.)
Best wishes,
Hans

snb
4StarLounger
Posts: 547
Joined: 14 Nov 2012, 16:06

Re: Word2003: Obtaining COLUMNS in a table

Post by snb »

In Word 2010:

Code: Select all

Sub M_snb()
   Tables(2).Columns(2).Select
   If Replace(Selection.Text, vbCr & Chr(7), "") = "" Then Tables(2).Columns(2).Delete
End Sub
I think it's just the other way around. MS tries to translate the Objects in Word into an understandable UI.
More often than not the UI is clumsier, less efficient, more redundant than the VBA approach.
'Mimicking the UI in VBA' is just the opposite of its development: VBA is first, the UI is derived.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Word2003: Obtaining COLUMNS in a table

Post by ChrisGreaves »

ChrisGreaves wrote:
22 May 2020, 11:43
The second chunk of code is not so easy, there is no Word2003/VBA construct for “tbl.Columns(lngColumn).Range.Text”.
snb wrote:
22 May 2020, 21:00
In Word 2010:

Code: Select all

Sub M_snb()
   Tables(2).Columns(2).Select
   If Replace(Selection.Text, vbCr & Chr(7), "") = "" Then Tables(2).Columns(2).Delete
End Sub
ChrisI think it's just the other way around. MS tries to translate the Objects in Word into an understandable UI.
More often than not the UI is clumsier, less efficient, more redundant than the VBA approach.
'Mimicking the UI in VBA' is just the opposite of its development: VBA is first, the UI is derived.
In Word2003, too.
Thanks snb for this concise solution.
I had to go back and dig out my old code, and now must correct myself.
Word2003/VBA does support Table.Columns.
Of course it does!
How else could I have looped on Table.Columns.Count :bash: ???

Untitled.png
I should have said something along the lines of "Columns(index) does not support the Range property"

And I think that overnight I shall internalize your comments.
I now think along the lines of "There is MSWord which has objects; these objects are well-described in terms of VBA. The user interface is an MSInternal platform for giving the end-user access to the objects in Word".

It is as if Msoft (writing in C++ or whatever) and ChrisGreaves (writing in VBA) are both striving to make a comprehensive UI for the poor end-user.

Thanks again.
Chris
You do not have the required permissions to view the files attached to this post.
An expensive day out: Wallet and Grimace