【问题标题】:Using Word VBA find Bold Text in a table cell and extract the text following it使用 Word VBA 在表格单元格中查找粗体文本并提取其后面的文本
【发布时间】:2021-06-17 15:04:44
【问题描述】:

我在 Word 中有一个表格。 Word table cell 我需要从单元格中提取(使用 Excel VBA)BOLD 文本到 excel 列,然后将以下文本(不是 BOLD)提取到下一个 BOLD 文本到下一个 Excel 列 Something like this 等等。 我尝试了以下操作,但它选择了整个单元格,而不是我需要的部分文本。 非常感谢任何帮助。

  For Each wdCell In oDoc.Tables(1).Range.Cells
      Set myRange = wdCell.Range
      If nCol = 12 Then
         With myRange.Find
          .Text = ""
          .MatchCase = False
          .Replacement.Text = ""
          .Forward = True
          .wrap = wdFindContinue
          .Format = True
          .MatchWildcards = False
          .Font.Bold = True

         Do While .Execute
            If myRange.Find.Found Then
               Debug.Print "Bold text is found ->" & myRange.Text
               myRange.Select
               myRange.Start = myRange.End + 1
               myRange.End = wdCell.Range.End
               myRange.Select
               ActiveSheet.Cells(nRow, nCol) = WorksheetFunction.Clean(wdCell.Range.Text)
         Loop
       End With
   Next 

【问题讨论】:

    标签: excel vba ms-word word-table


    【解决方案1】:

    大概是这样的:

    Dim c As Long
    With oDoc.Tables(1).Range
      With .Find
        .Text = ""
        .Replacement.Text = "^t^&^t"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchWildcards = False
        .Font.Bold = True
        .Execute Replace:=wdReplaceAll
      End With
      For Each wdCell In .Cells
        With wdCell.Range
          If .Characters.First = vbTab Then .Characters.First.Delete
          .Copy
          ActiveSheet.Paste Destination:=ActiveSheet.Cells(wdCell.RowIndex, wdCell.ColumnIndex + c)
          If wdCell.ColumnIndex = 1 Then
            c = UBound(Split(.Text, vbTab))
          Else
            c = c + UBound(Split(.Text, vbTab))
          End If
        End With
      Next
    End With
    

    【讨论】:

    • ActiveSheet.Paste Destination:=.Cells(.RowIndex, .ColumnIndex) 这给出“参数数量错误或属性分配无效”
    • 代码已修改。立即尝试。
    • 基于您用制表符替换文本的解决方案和分隔符。我用回车替换了该单元格中字段之间的特殊字符,然后将这些单元格拆分为行。但这留给另一个问题。谢谢
    • 插入回车将导致单元格内容在 Excel 中拆分为单独的行而不是单独的列。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    相关资源
    最近更新 更多