【问题标题】:Paste multi-paragraph formatted in single cell粘贴在单个单元格中格式化的多段
【发布时间】:2018-03-22 15:45:40
【问题描述】:

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

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

【问题讨论】:

    标签: vba excel ms-word


    【解决方案1】:

    我找到了一个解决方法,我想分享一下:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2017-01-09
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      相关资源
      最近更新 更多