如何使用Word VBA提取包含多个词的句子。

huangapple go评论91阅读模式
英文:

How to extract sentences containing any of several words using Word VBA

问题

  1. 我有一个Microsoft Word文件,名为Tst.doc,其中包含以下句子:

沙发1。
沙发2。
椅子1。
椅子2。
座位1。
座位2。

  1. 我需要提取所有包含以下任意单词(沙发、椅子、座位)的句子到一个名为OutputBin.doc的第二个文件中,该文件存储在固定位置。
  2. 我根据一个现有模块编写了以下代码,该模块只能处理一个单词,但我的代码工作不稳定:三个单词的相对位置很重要,所以我认为我无法重置单词查找范围(每次搜索后它都在不断缩小?)
  3. ```vba
  4. Sub Extract_MANY_OR()
  5. Dim liste As String
  6. Dim file1 As String
  7. Dim file2 As String: file2 = "D:\OutputBin.doc"
  8. '每次都要更改这个!
  9. file1 = "D:\TST.doc"
  10. liste = "座位,沙发,椅子"
  11. Dim i As Integer
  12. '为文档分配变量
  13. Dim wrdDoc1 As Document: Set wrdDoc1 = Documents.Open(file1)
  14. Dim wrdDoc2 As Document: Set wrdDoc2 = Documents.Open(file2)
  15. Dim r1 As Range: Set r1 = wrdDoc1.Range
  16. Dim r2 As Range: Set r2 = wrdDoc2.Range
  17. For i = 0 To UBound(Split(liste, ","))
  18. With r1
  19. .Find.Text = Split(liste, ",")(i)
  20. .Find.MatchCase = False '
  21. ' .Find.MatchCase = True
  22. Do While .Find.Execute
  23. r1.Expand Unit:=wdSentence
  24. wrdDoc2.Characters.Last.FormattedText = r1.FormattedText
  25. r2.InsertParagraphAfter
  26. r1.Collapse wdCollapseEnd
  27. Loop
  28. End With
  29. Next i
  30. End Sub
英文:

I have a Microsoft Word file, Tst.doc that contains the following sentences:

  1. Sofa 1.
  2. Sofa 2.
  3. Chair 1.
  4. Chair 2.
  5. Seat 1.
  6. Seat 2.

I need to extract all the sentences containing any of the words (sofa,chair,seat) to a second file named OutputBin.doc that is stored in a fixed location.

I wrote the following code, based on an existing module that is able to work only on one word, and my code is only working erratically: the relative position of the three words matters, so I believe I am not able to reset the word find range (it is shrinking continuously after each search?)

  1. Sub Extract_MANY_OR()
  2. Dim liste As String
  3. Dim file1 As String
  4. Dim file2 As String: file2 = "D:\OutputBin.doc"
  5. ' Change this every time!
  6. file1 = "D:\TST.doc"
  7. liste = "seat,sofa,chair"
  8. Dim i As Integer
  9. ' assign variables for the documents
  10. Dim wrdDoc1 As Document: Set wrdDoc1 = Documents.Open(file1)
  11. Dim wrdDoc2 As Document: Set wrdDoc2 = Documents.Open(file2)
  12. Dim r1 As Range: Set r1 = wrdDoc1.Range
  13. Dim r2 As Range: Set r2 = wrdDoc2.Range
  14. For i = 0 To UBound(Split(liste, ","))
  15. With r1
  16. .Find.Text = Split(liste, ",")(i)
  17. .Find.MatchCase = False '
  18. ' .Find.MatchCase = True
  19. Do While .Find.Execute
  20. r1.Expand Unit:=wdSentence
  21. wrdDoc2.Characters.Last.FormattedText = r1.FormattedText
  22. r2.InsertParagraphAfter
  23. r1.Collapse wdCollapseEnd
  24. Loop
  25. End With
  26. Next i
  27. End Sub

答案1

得分: 1

你需要在每个单词之后重置搜索范围。

英文:

You need to reset the search range after each word.

  1. Sub Extract_MANY_OR()
  2. Dim liste As String
  3. Dim file1 As String
  4. Dim file2 As String: file2 = "D:\OutputBin.doc"
  5. ' Change this every time!
  6. file1 = "D:\TST.doc"
  7. liste = "seat,sofa,chair"
  8. Dim i As Integer
  9. ' assign variables for the documents
  10. Dim wrdDoc1 As Document: Set wrdDoc1 = Documents.Open(file1)
  11. Dim wrdDoc2 As Document: Set wrdDoc2 = Documents.Open(file2)
  12. For i = 0 To UBound(Split(liste, ","))
  13. ' Reset the range after each word
  14. Dim r1 As Range: Set r1 = wrdDoc1.Range
  15. Dim r2 As Range: Set r2 = wrdDoc2.Range
  16. With r1
  17. .Find.Text = Split(liste, ",")(i)
  18. .Find.MatchCase = False '
  19. ' .Find.MatchCase = True
  20. Do While .Find.Execute
  21. r1.Expand Unit:=wdSentence
  22. wrdDoc2.Characters.Last.FormattedText = r1.FormattedText
  23. r2.InsertParagraphAfter
  24. r1.Collapse wdCollapseEnd
  25. Loop
  26. End With
  27. Next i
  28. End Sub

huangapple
  • 本文由 发表于 2023年5月25日 23:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定