Copy Microsoft Word table cell content including line breaks into Excel cell via VBA

abdulhaque

Board Regular
Joined
Dec 2, 2015
Messages
63
Hi all,

I have the below VBA code that will copy the contents of a Microsoft Word table cell and paste it into an Excel cell.

Code:
Cells(x) = WorksheetFunction.Clean(.Cell(x).Range.Text)

It works apart from picking up line breaks. So if the Word table cell reads

Username: CH_CLERICAL1
Password: CH_CLERICAL1

This will be copied and pasted into Excel as

Username: CH_CLERICAL1Password: CH_CLERICAL1

Can the above code be improved so it copies/pastes line breaks too?

Thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Are you sure there are line breaks in the Word cell and that what you're seeing is not just text wrapping, perhaps accompanied by tabs (i.e. →)? If there are breaks, are they paragraph breaks (i.e. ¶) or line breaks (i.e. ↵)? Do the Word cells contain formfields or content controls?
 
Upvote 0
Sorry they are paragraph breaks. I have other issues too, the code doesn't pick up table numbering or cross referencing or document property fields. Ideally the data that can't be copied and pasted as the source data in Excel should be copied and pasted in a basic text format..
 
Upvote 0
Perhaps then you could start off by telling us - in detail - what these cells do contain and how you want that content processed.
 
Upvote 0
Each cell in the Word table can contain any of the following.

1. Text sentences, some words in bold, italic, bold and italic.
2. The header row cells are filled with a light grey shade.
3. Every cell has wordwrap enabled.
4. Inline small image, size of small icon, in text sentence.
5. Table numbering in first column of each row apart from header row.
6. Crossreferencing of table numbering in text sentence.
7. Crossreferencing of another table numbering in text sentence. The script pulls data from each table with each execution of script.
8. Document property fields.

I would like Excel to process data from each Word table cell as is, so when pasted in Excel cell, it holds the formats etc. If there are limitations, then at the very least the copied data should be pasted as raw text format in Excel. Currently the document property fields are pasted as text in Excel. But it ignores paragraph breaks, numbering, text formats, cell formats, inline images.
 
Upvote 0
So, if you want the entire table, with formatting, why are you trying to work with individual cells instead of copying & pasting the entire table?
 
Upvote 0
I'm not very versed with VBA yet, so have been picking examples from here and there and gluing them together. Does a simple copy and paste function exist for tables across Word and Excel?
 
Upvote 0
Sure you can do that. Simply turning on Excel's macro recorder before pasting a table copied from Word will provide the basic code.

Do bear in mind though that, whatever method you use, Word tables with:
1. Columns with varying cell widths
2. Split cells
cannot be replicated in Excel. Similarly, Word fields, such as those used for document properties and cross-references, as well as auto paragraph numbering cannot be replicated in an Excel worksheet - the most you'll get for these is the corresponding textual content and formatting. Where a Word table cell has multiple paragraphs, or paragraphs with line-break characters, the paste process will create extra rows in Excel for these. While there are workarounds, they result in most other text formatting being lost.

If none of those outcomes is acceptable, you have two other options - paste the Word table as a:
1. Word object; or
2. picture (enhanced metafile)
both of which retain the original table's look, but don't otherwise interact with anything in Excel.
 
Last edited:
Upvote 0
The only code I get is
Code:
Sub Macro8()
ActiveSheet.Paste
End Sub

I understand the limitations, I'll probably try and improve the display via VBA post pasting the table.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,040
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top