[英]MS Word VBA Find text around a word
I'd like find text in Microsoft Word and get adjacent words.我想在 Microsoft Word 中查找文本并获取相邻的单词。
I would like to start with a word and find all the words before and after they surround it.我想从一个词开始,找出它前后的所有词。
The function should be recursive.该函数应该是递归的。
For Example:例如:
abc def ghi jkl mno def pqr stu wxy def abc def ghi jkl mno def pqr stu wxy def
if i search the string "def", the function should return me:如果我搜索字符串“def”,该函数应该返回我:
abc def ghi mno def pqr wxy def abc def ghi mno def pqr wxy def
it's possible?这是可能的?
thank you!谢谢你!
Sub Cerca(Parola)
Dim rng1 As Range
Dim rng2 As Range
Dim rngFound As Range
Dim Prima As Integer
Dim Dopo As Integer
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = Parola
' .Replacement.Text = "Provo"
.Forward = True
.Wrap = wdFindStop
Do While .Execute() = True
Selection.MoveRight Unit:=wdWord, Count:=4
Set rng2 = Selection.Range
Selection.MoveLeft Unit:=wdWord, Count:=9
Set rng1 = Selection.Range
Prima = rng1.Start
Dopo = rng2.Start
Set rngFound = ActiveDocument.Range(Prima, Dopo)
strTheText = rngFound.Text
ScriviFile Parola & Chr(9) & strTheText
'Selection.Find.Replacement.Font.Italic = True
'Selection.Font.Bold = True
'Selection.MoveRight Unit:=wdCharacter, Count:=Dopo
' Selection.MoveRight Unit:=wdWord, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=9
Loop
End With
End Sub
The procedure I published does not work well because it also considers punctuation as words.我发布的程序效果不佳,因为它也将标点符号视为单词。
I try to explain myself better... i'd like a function that search in a Microsoft Word document a string and get me a number "x" of words before and next the string i have passed.我试图更好地解释自己...我想要一个函数,它在 Microsoft Word 文档中搜索一个字符串,并在我传递的字符串之前和之后给我一个数字“x”的单词。 For Example....例如....
function myGetMyListOfSearch(SearchString as string, PreviusWord as integer, NextWord as integer)
This function return me a list of "strings" with my "SearchString" surrounded by the terms on the left and right of it...此函数返回一个“字符串”列表,其中包含我的“SearchString”,其左右两侧的术语...
it's possible?这是可能的?
A wildcard Find with:通配符查找:
Find = <[!查找 = <[! ]@>[,. ]@>[,. ^t^l^13]@Parola[,. ^t^l^13]@Parola[,. ^t^l^13]@<[! ^t^l^13]@<[! ]@> ]@>
should suffice, even where the preceding/next word is in a different paragraph.应该就足够了,即使前一个/下一个单词在不同的段落中。
I am not proud of this solution ....我对这个解决方案并不感到自豪......
I look for a string in a word document and publish the result in a table of another word document ... The table is divided into 3 parts: in the center the string I searched for, in the first column "x number of words" to the left of the string and in the third column "y number of words" to the right of the searched string.我在一个word文档中查找一个字符串并将结果发布到另一个word文档的表格中......该表格分为3部分:在中心我搜索的字符串,在第一列“x字数”在字符串的左侧和在第三列“y 字数”中搜索的字符串右侧。 But it's very slow ... better solutions?但它很慢......更好的解决方案? Thank you谢谢
Sub Cerca(Parola, Destinazione)
Dim rng1 As Range
Dim rng2 As Range
Dim rngFound As Range
Dim Prima As Long
Dim Dopo As Long
Dim PosizioneAttuale As Long
Dim strSinistra As String
Dim strCentro As String
Dim strDestra As String
Dim UltimaRiga As Long
Dim Ciclo As Long
Dim Sicurezza As Long
Selection.HomeKey Unit:=wdStory
'Selection.Find.ClearFormatting
With Selection.Find
.Text = Parola
' .Replacement.Text = "Provo"
.Forward = True
.Wrap = wdFindStop
.IgnorePunct = True
.MatchWholeWord = ParoleIntere
.ClearFormatting
.Format = False
Do While .Execute() = True
DoEvents
PosizioneAttuale = Selection.Start
'SI CONTROLLA A DESTRA
Ciclo = 0
Sicurezza = 0
Do
'DoEvents
Sicurezza = Sicurezza + 1
Selection.MoveRight Unit:=wdWord, Count:=1
If InStr(1, ".,;:-_/!\'()" & Chr(34) & vbCrLf, Trim(Selection.Range.Words.Item(1)), vbTextCompare) = 0 Then
Ciclo = Ciclo + 1
End If
If Sicurezza > 100 Then
'Debug.Print "esco con exit do"
'Selection.MoveLeft Unit:=wdWord, Count:=501
Exit Do 'nel caso entri in loop per qualche motivo
End If
Loop Until Ciclo = ParoleDopo Or Selection.Range.Start = ActiveDocument.Range.End
Selection.MoveRight Unit:=wdWord, Count:=1
Set rng2 = Selection.Range
Selection.Start = PosizioneAttuale
'SI CONTROLLA A SINISTRA
Ciclo = 0
Sicurezza = 0
Selection.MoveLeft Unit:=wdWord, Count:=1
Do
'DoEvents
Sicurezza = Sicurezza + 1
Selection.MoveLeft Unit:=wdWord, Count:=1
If InStr(1, ".,;:-_/!\'()", Trim(Selection.Range.Words.Item(1)), vbTextCompare) = 0 Then
Ciclo = Ciclo + 1
End If
If Sicurezza > 100 Then
Debug.Print "esco con exit do"
'Selection.MoveRight Unit:=wdWord, Count:=501
Exit Do 'nel caso entri in loop per qualche motivo
End If
Loop Until Ciclo = ParolePrima Or Selection.Range.Start = ActiveDocument.Range.End
'Selection.MoveLeft Unit:=wdWord, Count:=ParolePrima + 1
Set rng1 = Selection.Range
Prima = rng1.Start
Dopo = rng2.Start
If Dopo > Prima Then
Set rngFound = ActiveDocument.Range(Prima, Dopo)
strTheText = rngFound.Text
'ScriviFile Left(strTheText, Prima) & Chr(9) & Parola & Chr(9) & Mid(strTheText, Dopo)
strSinistra = Left(strTheText, PosizioneAttuale - Prima)
strCentro = Parola
Prima = PosizioneAttuale + Len(Parola)
If Prima = -1 Then Prima = 0
strDestra = Right(strTheText, Dopo - Prima)
Selection.Start = PosizioneAttuale
Selection.MoveRight Unit:=wdWord, Count:=1
'scrivo nella tabella del foglio destinazione
Documents(Destinazione).Tables(1).Rows.Add
UltimaRiga = Documents(Destinazione).Tables(1).Rows.Count
Documents(Destinazione).Tables(1).Cell(UltimaRiga, 1).Range.InsertAfter strSinistra
Documents(Destinazione).Tables(1).Cell(UltimaRiga, 2).Range.InsertAfter strCentro
Documents(Destinazione).Tables(1).Cell(UltimaRiga, 3).Range.InsertAfter strDestra
End If
Loop
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.