excel - 使用 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
解决方案
也许是这样的:
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
推荐阅读
- python - 如何记录动态方法以实现自动完成
- ios - 如何修复从 github 分叉的库的“无法找到规范”
- vb.net - 从数据表中删除多行
- arrays - 在 Python 中实现集成和矢量化操作
- typescript - 打字稿减去文字数字类型
- java - 在 Java 中处理 CompletableFuture 中的错误的方法
- javascript - 如何制作无限图像滑块。像 Facebook 故事滑块
- string - bash 处理多引号
- python - 如何将格式 yyyy-mm-ddThh:mm:ss 转换为 unix 时间戳?
- qt - 在没有glOrtho的QOpenGLWidget中放大光标位置