PDA

View Full Version : [SOLVED:] Selecting a specific cell in a footer table



Roderick
02-14-2018, 04:01 PM
I'm using Word 2013 in Windows 7.

I've got a document with a number of pages into which I've added a last page in a new separate section. The cursor is sitting in the body of this last page.

The footer is 'not the same as previous'.

In the footer of this section, I have a table with a carriage return just below it. The table consists of two rows: the top row is one complete cell stretching across the width of the table. The lower row consists of three cells. In the cell in column 3 of this row is a piece of text I want to delete.

At present, I'm using the code below to select the last cell and delete its contents. It works if I process the code row by row. However, when I let it do its own thing it somehow deletes the contents of the second column and leaves the third one intact!


'now works in the footer
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
With Selection
.PageSetup.DifferentFirstPageHeaderFooter = False
.HeaderFooter.LinkToPrevious = False
With ActiveWindow.ActivePane.View
.SeekView = wdSeekMainDocument
.SeekView = wdSeekCurrentPageFooter
End With
With Selection
.MoveUp Unit:=wdLine, Count:=1
.SelectCell
.MoveRight Unit:=wdWord, Count:=2
.SelectCell
.Delete Unit:=wdCharacter, Count:=1
End With
End With
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument



I've searched in Google and in this forum and found a number of references about using ranges. However, I can't seem to get the right combination. They fail miserably!

What I'm trying to achieve using pseudo-code is to say: "Go to the footer of this section and find the table within it. Then go to the third cell in the second row and delete its contents.

The code above is rather a convoluted way to achieve this. And anyway, it doesn't seem to work like I want it to.

Could anyone suggest a better way, please?

macropod
02-14-2018, 07:04 PM
With ActiveDocument.Sections.Last.Footers(wdHeaderFooterPrimary).Range.Tables(1) .Range
.Cells(.Cells.Count).Range.Text = vbNullString
End With

Roderick
02-15-2018, 01:50 AM
Thanks very much, Paul. It works brilliantly!

I can see you've used Ranges but could you help me to understand how it knows to go to the last cell in the bottom row and then delete its contents, please?

The clue might be in the second line of your code:

.Cells(.Cells.Count).Range.Text = vbNullString
...but I can't seem to find an explanation as to what ".Cells(.Cells.Count) actually does.

However, thanks very much!

Roderick

gmaxey
02-15-2018, 10:46 AM
Where is the ever present Paul? ;)


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 2/15/2018
With ActiveDocument.Sections.Last.Footers(wdHeaderFooterPrimary).Range.Tables(1) .Range
'Cells are collection indexed from 1 (the first cell in a table to .Cells.Count (the last cell in a table)
MsgBox "The table of interest has " & .Cells.Count & " cells."
'Work with a specific indexed cell, or in this case the last cell in the table.
.Cells(.Cells.Count).Range.Text = vbNullString
End With

lbl_Exit:
Exit Sub
End Sub

Roderick
02-15-2018, 01:31 PM
Thanks, Greg.

It all makes sense now.

Roderick