簡體   English   中英

使用 Word VBA 在表格單元格中查找粗體文本並提取其后的文本

[英]Using Word VBA find Bold Text in a table cell and extract the text following it

我在 Word 中有一張表格。 Word 表格單元格我需要從單元格中提取(使用 Excel VBA)BOLD 文本到 excel 列,然后將以下文本(不是 BOLD)提取到下一個 BOLD 文本到下一個 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 

也許是這樣的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM