2017-10-11 71 views
0

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

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

回答

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