粘贴单个单元格中格式化的多段落



我的word文档中有一个表格,其中包含格式化的多段落/行文本(包括编号列表和项目符号列表)。我想使用 VBA 宏将此文本复制到单个单元格中。当我将单词单元格粘贴到 Excel 单元格中时,源的一段被粘贴到另一行中。当我将其直接粘贴到单元格中(单击公式字段并粘贴剪贴板的内容)时,我丢失了格式。由于Excel单元格不支持HTML标记,列表等,因此如果将格式化文本转换为纯文本,将编号列表替换为实数即可。

所以问题:如何将格式化文本作为普通结构化文本粘贴到单个单元格中?

我找到了一种解决方案,我想分享一下:

resultRow=4  ' row number in Excel sheet
' read the number of paragraphs from word table cell (vRow)
i = vRow.Cells(3).Range.Paragraphs.Count
' copy and paste the cell content into Excel
vRow.Cells(3).Range.Copy
ActiveSheet.Cells(resultRow, 3).Select
ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
' excel has copied the formatted the paragraphs into consecutive rows
' of the selected column
' concatenate the text of the cells
v = ""
For j = 0 To i - 1
   v = v + ActiveSheet.Cells(resultRow + j, 3).Value
   If j < i - 1 Then v = v + Chr(10)
Next j
ActiveSheet.Cells(resultRow, 3).Value = v
' clear all formatting
Selection.ClearFormats
Selection.WrapText = True
Selection.VerticalAlignment = xlTop

最新更新