PDA

View Full Version : [SOLVED:] Formatted Word text to Excel



gmaxey
01-23-2021, 08:30 AM
I have procedure that finds text in Word and the puts the Word paragraph text (where the term was fount) into and Excel cell:

Sheets("Test").Cells(xlRow, 3).Value = oRng.Paragraphs(1).RangeText 'Where oRng is the found range in the word document.

This results in unformatted text in the Excel cell. If my word text has formatting, e.g., bold, italic, underline etc. How can I adapt this line of code so that the result in the Excel cell matches the formatted text in the Word document.

snb
01-23-2021, 08:54 AM
Sub M_snb()
GetObject(, "word.application").activedocument.Paragraphs(1).Range.Copy
ActiveSheet.Cells(20, 1).PasteSpecial "HTML"
End Sub

gmaxey
01-23-2021, 09:41 AM
snb, thanks! One other question if you don't mind. Using either method the resulting text is strung out as one line. Is there a way to make it wrap to a certain cell width?

snb
01-23-2021, 09:48 AM
Is this what you mean ?

Sub M_snb()
GetObject(, "word.application").activedocument.Paragraphs(1).Range.Copy
ActiveSheet.Cells(20, 1).PasteSpecial "HTML"
ActiveSheet.Cells(20, 1).wraptext=true
End Sub

I also was somewhat disappointed to find out that Paragraphs(1).range.formattedtext won't work either from Word to Excel (and I suspect to any other Office application).

gmaxey
01-23-2021, 09:59 AM
Thanks!!