Word Table VBA Question
Hi,
I am creating a word table using some software. I am copying last cell
content of first column on first page and copying into second cell of first
column on second page. I got an answer from a MVP about how to do this.
Thanks to her. I extended it to copy last non empty cell content of first
column and paste it into the second page second cell of first column. I was
able to repeat this action for each page.
Its working except for the last page. Its giving error 'There is no table
at this location'. But there is a table on the last page. On the last page,
the table can be
just 2 rows, so it might occupy less than first half of the last page. Here
is
the code already that I am using to do this. Anyone please check the code
and tell me what 's going wrong and What should I do?
Sub copynext()
Dim rngPg1 As Word.Range
Dim rngPg2 As Word.Range
Dim rngCell1 As Word.Range
Dim rngCell2 As Word.Range
numPages = Selection.Information(wdNumberOfPagesInDocument)
'Move to the top of the document
Selection.HomeKey wdStory
'Pick up the entire page of the active selection
For i = 1 To numPages
If i = numPages Then Exit Sub
Selection.GoTo What:=wdGoToPage, Which:=wdGoToPageNumber, Count:=i
Set rngPg1 = ActiveDocument.Bookmarks("\Page").Range
'Pick up the last row, first cell
cellNumber = rngPg1.Rows.Count
Set rngCell1 = rngPg1.Rows(cellNumber).Cells(1).Range
'Shorten the range to drop the end-of-cell marker
rngCell1.MoveEnd wdCharacter, -1
Do Until cellNumber = 1
If rngCell1.Text = "" Then
cellNumber = cellNumber - 1
Set rngCell1 = rngPg1.Rows(cellNumber).Cells(1).Range
rngCell1.MoveEnd wdCharacter, -1
End If
If rngCell1.Text <> "" Then Exit Do
Loop
'Move to the second page
Selection.GoTo What:=wdGoToPage, Which:=wdGoToPageNumber, Count:=i + 1
'Pick up the entire page
Set rngPg2 = ActiveDocument.Bookmarks("\Page").Range
'Pick up the first cell of the second row
'If i < numPages Then Set rngCell2 = rngPg2.Rows(1).Cells(1).Range
Set rngCell2 = rngPg2.Rows(1).Cells(1).Range
'Make sure the range is IN the cell (not containing the cell)
rngCell2.Collapse
'"Copy" the text + formatting
rngCell2.FormattedText = rngCell1.FormattedText
rngCell2.Paragraphs.Alignment = wdAlignParagraphLeft
Next i
Debug.Print rngPg2.Rows.Count, rngCell1.Text
End Sub
date: Tue, 24 Jan 2006 07:47:04 -0800
author: Ram