Solved

Macro that will Select the contents of a cell in a Word Table and Paste it at the end of the Document

Posted on 2009-04-11
4
191 Views
Last Modified: 2013-11-25
I need help writing a macro that will select and copy the contents of the 2nd cell in Row 2 of the 3rd Table in my active document and paste them at the end of the document.
Thanks for your help in anticipation.
0
Comment
Question by:FaheemAhmadGul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24121516
This will copy the text from the cell to the end of the document.
Sub CopyCellText()
    Dim tbl As Word.Table
    Dim strText As String
    
    Set tbl = ActiveDocument.Tables(3)
    strText = GetCellText(tbl.Cell(2, 2))
    ActiveDocument.Bookmarks("\EndOfDoc").Range.Text = strText
End Sub
 
Function GetCellText(cel As Word.Cell) As String
    Dim rng As Range
    
    Set rng = cel.Range
    rng.MoveEnd wdCharacter, -1 'drop cell format character
    GetCellText = Trim$(rng.Text)
End Function

Open in new window

0
 
LVL 1

Author Comment

by:FaheemAhmadGul
ID: 24121598
Many thanks for your help with my question. The above code works but the only thing is that when the contents of the relevant cell are pasted at the end of the document, they loose formatting they had in the source cell.
I would be grateful if you could modify the code so that the formatting is not lost in the process.
As this  involves some extra work, I am increasing the points for this problem  to 500.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 24121810
My apologies. You did say copy and paste.
Sub CopyCellText()
    Dim tbl As Table
    Dim strText As String
    Dim rng As Range
 
    Set tbl = ActiveDocument.Tables(3)
    Set rng = tbl.Cell(2, 2).Range
    rng.MoveEnd wdCharacter, -1  'drop cell format character
    rng.Copy
    ActiveDocument.Bookmarks("\EndOfDoc").Range.Paste
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:FaheemAhmadGul
ID: 31569152
This worked perfectly. Many thanks. Regards,  Faheem
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question