W
-W
Hello all;
First time poster here. What I'm trying to do is search through a
document targeting on a keyword (entered by the user). When the
keyword is found, the code then uses the Extend and wdMove objects to
Select the sentence that the key word is in copy it and paste it at
the bottom of the document.
The code below works, but is definitely newbie hack code. I'm sure
that there is a much more elegant way to do this. I would appreciate
any suggestions. I'm much better at VBA for Excel.
Also looking for;
Once I find the sentence I want, I would like to then scan up in the
document to where a style change is and copy that also. (Heading 3
vs. Normal)
Example: search word = "shall"
Desired result
2.3.1 Byte Control Parameter <tab> When the Byte Control Parameter is
False the system shall send BCPerror to Master <vbCr>
'''''''''''''''''''
' Code Begins
''''''''''''''''''
Public Sub Gobbeldygook()
Dim strSearch As String
strSearch = InputBox$("Type in the text you want to search for.")
Dim iCount As Long
Dim jCount As Long
iCount = 0
Selection.HomeKey Unit:=wdStory
Selection.HomeKey Unit:=wdStory
With ActiveDocument.Content.Find
.Text = strSearch
.Format = False
.Wrap = wdFindStop
'Uses Do While here
Do While .Execute
iCount = iCount + 1
Loop '
End With
crap = MsgBox(iCount, vbOKOnly)
If iCount = 0 Then
End
End If
jCount = 0
With ActiveDocument.Content.Find
.ClearFormatting
Do While .Execute(FindText:=strSearch, Forward:=True, _
Format:=True) = True
With .Parent
.StartOf Unit:=wdSentence, Extend:=wdMove
'.InsertAfter vbCr
.EndOf Unit:=wdSentence, Extend:=wdExtend
.Select
.Copy
.Move Unit:=wdSentence, Count:=1
.Bold = True
End With
strRetTextName = Selection.Text & vbCr
Selection.EndKey Unit:=wdStory
Selection.Text = strRetTextName
Selection.Paste
'
jCount = jCount + 1
If jCount = iCount Then
crap = MsgBox(jCount, vbOKOnly)
End
End If
Loop
End With
crap = MsgBox(iCount, vbOKOnly)
End Sub
First time poster here. What I'm trying to do is search through a
document targeting on a keyword (entered by the user). When the
keyword is found, the code then uses the Extend and wdMove objects to
Select the sentence that the key word is in copy it and paste it at
the bottom of the document.
The code below works, but is definitely newbie hack code. I'm sure
that there is a much more elegant way to do this. I would appreciate
any suggestions. I'm much better at VBA for Excel.
Also looking for;
Once I find the sentence I want, I would like to then scan up in the
document to where a style change is and copy that also. (Heading 3
vs. Normal)
Example: search word = "shall"
Desired result
2.3.1 Byte Control Parameter <tab> When the Byte Control Parameter is
False the system shall send BCPerror to Master <vbCr>
'''''''''''''''''''
' Code Begins
''''''''''''''''''
Public Sub Gobbeldygook()
Dim strSearch As String
strSearch = InputBox$("Type in the text you want to search for.")
Dim iCount As Long
Dim jCount As Long
iCount = 0
Selection.HomeKey Unit:=wdStory
Selection.HomeKey Unit:=wdStory
With ActiveDocument.Content.Find
.Text = strSearch
.Format = False
.Wrap = wdFindStop
'Uses Do While here
Do While .Execute
iCount = iCount + 1
Loop '
End With
crap = MsgBox(iCount, vbOKOnly)
If iCount = 0 Then
End
End If
jCount = 0
With ActiveDocument.Content.Find
.ClearFormatting
Do While .Execute(FindText:=strSearch, Forward:=True, _
Format:=True) = True
With .Parent
.StartOf Unit:=wdSentence, Extend:=wdMove
'.InsertAfter vbCr
.EndOf Unit:=wdSentence, Extend:=wdExtend
.Select
.Copy
.Move Unit:=wdSentence, Count:=1
.Bold = True
End With
strRetTextName = Selection.Text & vbCr
Selection.EndKey Unit:=wdStory
Selection.Text = strRetTextName
Selection.Paste
'
jCount = jCount + 1
If jCount = iCount Then
crap = MsgBox(jCount, vbOKOnly)
End
End If
Loop
End With
crap = MsgBox(iCount, vbOKOnly)
End Sub