Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Word vba Table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ram - 11 Jan 2006 17:26 GMT
Hi,
 I am creating multiple page word document. Here is what I would like to do
on each page.
Goto first page and select non empty cell from last in the first column.
Goto next page check second cell in first column is empty or not. If it is
empty then paste the copied text from first page.

I was able to goto the page I want using GoTo. But then I was not able to
copy particular cell and then paste it in the second cell of first column,
second page.

Cindy M  -WordMVP- - 12 Jan 2006 10:15 GMT
Hi =?Utf-8?B?UmFt?=,

> I am creating multiple page word document. Here is what I would like to do
> on each page.
[quoted text clipped - 5 lines]
> copy particular cell and then paste it in the second cell of first column,
> second page.

Here's some sample code

Sub CopyTableCell()
   Dim rngPg1 As Word.Range
   Dim rngPg2 As Word.Range
   Dim rngCell1 As Word.Range
   Dim rngCell2 As Word.Range
   
'Move to the top of the document
   Selection.HomeKey wdStory
'Pick up the entire page of the active selection
   Set rngPg1 = ActiveDocument.Bookmarks("\Page").Range
'Pick up the last row, first cell
   Set rngCell1 = rngPg1.Rows(rngPg1.Rows.Count).Cells(1).Range
'Shorten the range to drop the end-of-cell marker
   rngCell1.MoveEnd wdCharacter, -1
'Move to the second page
   Selection.GoTo What:=wdGoToPage, Which:=2
'Pick up the entire page
   Set rngPg2 = ActiveDocument.Bookmarks("\Page").Range
'Pick up the first cell of the second row
   Set rngCell2 = rngPg2.Rows(2).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
   Debug.Print rngPg2.Rows.Count, rngCell1.Text
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)
Ram - 13 Jan 2006 00:13 GMT
Hi,
   Thanks for the code. I will try. Thanks a lot.

> Hi =?Utf-8?B?UmFt?=,
>
[quoted text clipped - 44 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)
Ram - 13 Jan 2006 14:21 GMT
Hi,
 I tried. Its working. I am more of learning VBA. Instead of copying last
cell content of first column on first page and copying into second cell of
first column on second page, Is it possible to copy last non empty cell
content of first column and paste it into the second page second cell of
first column. If it is possible that would be great. And I want to do this
repetetively for every page of the table. Here is the code that I you
provided me last time. Thank you.

Sub CopyTableCell()
Dim rngPg1 As Word.Range
Dim rngPg2 As Word.Range
Dim rngCell1 As Word.Range
Dim rngCell2 As Word.Range

'Move to the top of the document
Selection.HomeKey wdStory
'Pick up the entire page of the active selection
Set rngPg1 = ActiveDocument.Bookmarks("\Page").Range
'Pick up the last row, first cell
Set rngCell1 = rngPg1.Rows(rngPg1.Rows.Count).Cells(1).Range
'Shorten the range to drop the end-of-cell marker
rngCell1.MoveEnd wdCharacter, -1
'Move to the second page
Selection.GoTo What:=wdGoToPage, Which:=2
'Pick up the entire page
Set rngPg2 = ActiveDocument.Bookmarks("\Page").Range
'Pick up the first cell of the second row
Set rngCell2 = rngPg2.Rows(2).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
Debug.Print rngPg2.Rows.Count, rngCell1.Text
End Sub

> Hi =?Utf-8?B?UmFt?=,
>
[quoted text clipped - 44 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)
Ram - 14 Jan 2006 19:08 GMT
Hi,
I tried. Its working. I am more of learning VBA. Instead of copying last
cell content of first column on first page and copying into second cell of
first column on second page,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. Here is the code that I modified. And I want to do this
repetetively for every page of the table. Could you please tell me how to do
this on every page. Thank you.

Sub CopyTableCell()
Dim rngPg1 As Word.Range
Dim rngPg2 As Word.Range
Dim rngCell1 As Word.Range
Dim rngCell2 As Word.Range

'Move to the top of the document
Selection.HomeKey wdStory
'Pick up the entire page of the active selection
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:=2
'Pick up the entire page
Set rngPg2 = ActiveDocument.Bookmarks("\Page").Range
'Pick up the first cell of the second row
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

Debug.Print rngPg2.Rows.Count, rngCell1.Text

End Sub

> Hi =?Utf-8?B?UmFt?=,
>
[quoted text clipped - 44 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.