在单个单元格中粘贴多段格式

时间:2017-10-11 05:05:42

标签: vba excel-vba word-vba excel

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

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

1 个答案:

答案 0 :(得分:0)

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

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
相关问题