Consulting

Results 1 to 5 of 5

Thread: Selecting a specific cell in a footer table

  1. #1

    Selecting a specific cell in a footer table

    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?

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    With ActiveDocument.Sections.Last.Footers(wdHeaderFooterPrimary).Range.Tables(1).Range
      .Cells(.Cells.Count).Range.Text = vbNullString
    End With
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    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

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Thanks, Greg.

    It all makes sense now.

    Roderick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •