Link to home
Start Free TrialLog in
Avatar of Venturer58
Venturer58

asked on

Copy word table, paste into excel, simple?

Hey guys and gals,

Here's my problem, one of my partners created this 75 page MS Word table. Each cell has a blob of text with hard returns in it. I tried to copy the word table and paste it into excel, but wherever there is a hard return, the text goes to the next cell in excel and the formatting gets screwed up. Here's a list of stuff I already tried with no success:

1. Tried converting it to tab delimited and then importing to excel.
2. Copied whole table and pasted into excel
3. Downloaded a program called Word2Excel Converter
4.  Did the paste special thing in excel and chose "Keep original formatting"
None of these things worked...

Is there a VBA program or something out there that I can use? Any other ideas/suggestions are welcome, I am pulling my hair out trying to figure this out...

Thanks everyone!

-Steve
Avatar of sebastienm
sebastienm

Hi Steve,
1. Should these hard Returns be considered in Excel as:
   - word wrap within a single cell
   -or real enter within a single cell
   ?
2. Do you have a visible border around each cell in the Word table? or not always? When pasting top excel, hard returns becomes separate cells, but these cells dont have a top-horizontal border, they could therefore be tracked that way in a macro.

Regards,
Sebastien
 
Avatar of Venturer58

ASKER

I would like the hard returns to transfer to a hard return within a single cell. For example:

_____
BLAH |
        |
BLAH |
_____|

in one cell in the MS Word table to:

_____
BLAH |
        |
BLAH |
_____|

in the excel cell.

Does this answer your question?
ASKER CERTIFIED SOLUTION
Avatar of R_Rajesh
R_Rajesh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if the carriage return?linefeed combination creates strange chars in xl you could replace it with only a linefeed

xlSht.UsedRange.Replace "myCreturn", vbLf, 2
Search and replace hard return ^p with a space?  That's what I've done in the past.
Okay guys, we're almost there, I used your code above and got everything into excel almost correctly, but it gave the error msg "Formula too long" and instead of carraige returns, it inserted "myCreturn" where the hard returns should be... any way to replace "myCreturn" with a carraige return? If so, we're good to go...

Thanks!!

-Steve
Steve,

how long is the biggest string in the word table???

anyway try this modified code:

Sub wrdTOxl()
Dim xlApp As Excel.Object, xlSht As Excel.Object, myTbl As Table
Set myTbl = ActiveDocument.Tables(1): myTbl.Select
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "^p": .Replacement.text = "myCreturn"
.Forward = True: .Wrap = 0
End With
Selection.Find.Execute Replace:=wdReplaceAll
Set xlApp = CreateObject("Excel.Application"): xlApp.Visible = True
Set xlSht = xlApp.Workbooks.Add.Sheets(1)
myTbl.Range.Copy
xlSht.Cells.NumberFormat = "@"
xlSht.Paste
xlSht.Cells.NumberFormat = "@"
'On Error Resume Next
xlSht.UsedRange.Replace "myCreturn", vbLf, 2
'On Error GoTo 0
Set myTbl = Nothing: Set xlSht = Nothing: Set xlApp = Nothing
End Sub


if you still get the error, run this sub form xl once the table has been transfered there.

Sub test1()
Cells.Replace What:="myCreturn", Replacement:=vbLf, LookAt:=xlPart
End Sub


Rajesh
oops... the second line in the above code should be this:
Dim xlApp As Object, xlSht As Object, myTbl As Table

R
almost worked... alright, I ran your very first block of code and that was great, everything transferred formatted as I needed it, except "myCReturn" was where the carraige returns should have been, so what I did was do a Find/Replace all "myCreturn" with a carraige return, and that worked for every cell, except the cells (about 50 cells) that had really long text strings in them. I got the error "Formula too long" message when trying to do a find/replace into these cell. I tried reformatting the cells to "Text", but this just changed my text fields to "####" and when i went to make the field wider all I saw was more # signs.

I ran your second block of code and I got # signs for all of my fields except a few... So I am now manually finding and replacing every "myCreturn" in the cells that have a ton of text in them... I tried to run this:

Sub test1()
Cells.Replace What:="myCreturn", Replacement:=vbLf, LookAt:=xlPart
End Sub

but it didn't seem to do anything...

Is there a way in excel to find all of the myCreturn's and replace them with carraige returns? Thanks so much!

-Steve
Hi steve,
sorry, I am going out for couple of hours...
Any possibility of me getting a chance to look at you word doc. Email id is in my profile

R
I apologize, I can't e-mail it to you, it's corporate confidential information... I work for a consulting firm and the document is an audit program for one of our clients.

I will continue to manually find and replace the myCreturn's and if you have any other ideas, please post them. If I finish over the next couple of hours, i'll accept your (R. Rajesh) comment as the answer, you saved me hours worth of work already. Thanks again!

-Steve
Got it, whohooo!
hi steve,
sorry we couldn't nail the replacing bit.
thanks for the grade :)
R