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 / March 2006

Tip: Looking for answers? Try searching our database.

Copying cells containing line feeds from Word to Excel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Guillermo - 27 Mar 2006 18:55 GMT
[Context: automating Excel from within Word]

Hello,

I want to copy a single column table from Word to a spreadsheet. I know that due to an
Excel feature (bug?), copied cells from a Word table are pasted in separate cells in
Excel if they contain line feeds. Doing it by hand, you can get around this by
doubleclicking on the spreadsheet before actually pasting the Word cell's content -
one by one.

Using the following code...

...

Sub DOCTable2XLS()

Dim appXLS As Excel.Application
Dim a As Integer
Dim b As Long
Dim c As Cell

Set appXLS = GetObject(, "Excel.Application")
a = appXLS.ActiveCell.Column
b = appXLS.ActiveCell.Row

'appXLS.Visible = True
    For Each c In ActiveDocument.Tables(1).Columns(1).Cells

            c.Select
            Selection.Copy
            appXLS.ActiveCell.PasteSpecial xlPasteValues
            b = b + 1
            appXLS.Cells(b, a).Activate

    Next

End Sub

...

... I do get the Word table copied to Excel, but only the first line of each Word cell.

I'd like to know if there's a method to tell Excel to "enter" (like you do when
pressing F2 or doubleclicking) each cell for editing before copying the Word cell
contents. Alternatively, I could replace each line feed in Word with a placeholder
like "~r~" and then replace it again in Excel with a manual line feed, but I don't
know how to do that in Excel either.
Shauna Kelly - 28 Mar 2006 11:31 GMT
Hi Guillermo

You can use code something like the following. This only works for text in
the Word table; it won't copy things like pictures. In any case, you'll need
to add appropriate error checking.

Sub DOCTable2XLS()

Dim appXLS As Excel.Application
Dim wdCell As Word.Cell
Dim xlCell As Excel.Range
Dim sText As String

Set appXLS = GetObject(, "Excel.Application")

'Get a reference to the active cell in Excel
Set xlCell = appXLS.ActiveCell

For Each wdCell In ActiveDocument.Tables(1).Columns(1).Cells

   'Get the text of the Word cell
   sText = wdCell.Range.Text

   'Strip out the end-of-cell markers
   sText = Left(sText, Len(sText) - 2)

   'Replace end-of-paragraph markers with vbLfs so that
   'cells with more than one paragrah work in Excel
   sText = Replace(sText, Chr(13), vbLf)

   'Set the value of the Excel cell to the text
   'in the Word cell
   xlCell.Value = sText

   'Get a reference to the next Excel cell
   Set xlCell = xlCell.Offset(1, 0)

Next wdCell

End Sub

Hope this helps.

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word

> [Context: automating Excel from within Word]
>
[quoted text clipped - 44 lines]
> Word with a placeholder like "~r~" and then replace it again in Excel with
> a manual line feed, but I don't know how to do that in Excel either.
Guillermo - 28 Mar 2006 23:35 GMT
Hi Shauna,

Brilliant! Exactly what I wanted to know.

Thanks,

Guillermo

> Hi Guillermo
>
[quoted text clipped - 90 lines]
>> Word with a placeholder like "~r~" and then replace it again in Excel with
>> a manual line feed, but I don't know how to do that in Excel either.
Shauna Kelly - 29 Mar 2006 10:26 GMT
Hi Guillermo

I'm glad to hear it worked.

Shauna

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word

> Hi Shauna,
>
[quoted text clipped - 99 lines]
>>> Excel with a manual line feed, but I don't know how to do that in Excel
>>> either.
 
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.