首页 > 解决方案 > 使用 Word VBA 在表格单元格中查找粗体文本并提取其后面的文本

问题描述

我在 Word 中有一个表格。Word 表格单元格我需要从单元格中提取(使用 Excel VBA)粗体文本到 excel 列,然后将以下文本(不是粗体)提取到下一个粗体文本到下一个 Excel 列像这样等等..我尝试了以下,但它选择了整个单元格,而不是我需要的部分文本。任何帮助深表感谢。

  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 

标签: excelvbams-wordword-table

解决方案


也许是这样的:

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

推荐阅读