Find Text in MS Word

I need to find the text in the MS Word...

" Commencing on 23rd October 2004 "

I need to find the above complete sentence to get that specific date, how can I do that ? because "Commencing on" will remain in the other documents , but the date will be vary in other documents. I need to get the date . How can I do that ? Please help .Thanks

Please !
[377 byte] By [sathmaha] at [2007-11-20 11:01:23]
# 1 Re: Find Text in MS Word
Post the code you are trying. How are you opening Word?
dglienna at 2007-11-9 19:34:00 >
# 2 Re: Find Text in MS Word
WordObj.Documents.Open "C:\Documents and Settings\DER\Desktop\Letz Move\" + folder_name + "\new agreement.doc"

AND i need to fInd the specific word paragraph < without going to the for loop>>>(TO MINIMIZE THE PROCESS SPEED)

PLEASE HELP, STILL i'M HELPLESS...?????
sathmaha at 2007-11-9 19:35:06 >
# 3 Re: Find Text in MS Word
Pretty sure this works in Word as well as RTB's.

Option Explicit

' Add a reference to Riched20.dll. Browse for it!
' This will select TOM. You only need to do this once!
'
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_USER = &H400&
Private Const EM_GETOLEINTERFACE = (WM_USER + 60)

Dim tomDoc As ITextDocument

Private Enum Direction
Forward = 1
Backward = -1
End Enum

Private Sub Command1_Click()
RTFindText Text1.Text, Forward
End Sub

Private Sub Command2_Click()
RTFindText Text1.Text, Backward
End Sub

' All the hard work done here
Private Sub RTFindText(strText As String, Optional RequiredDirection As Direction = Forward)
Dim flags As Long
flags = 2 * Check1 + 4 * Check2 ' + 8 * Check3 sadly, RegExp functionality is not available on a RichTextBox
tomDoc.Selection.FindText Text1.Text, RequiredDirection * tomDoc.Selection.StoryLength, flags
End Sub

Private Sub Form_Load()
Dim myIUnknown As IUnknown ' Bet you didn't know that VB actually knows about IUnknown ...

' Get alternative TOM interface to RTB
SendMessage RichTextBox1.hwnd, EM_GETOLEINTERFACE, 0&, myIUnknown
Set tomDoc = myIUnknown ' We're doing a QueryInterface!

' Ok, set up our RTB with some text
RichTextBox1.Text = "hello and Hello and hEllo or hello with another hello just for luck saying hello there"
RichTextBox1.HideSelection = False 'Always show highlight, whether RTB has focus or not

Text1.Text = "Hello" ' Default word to search for
Command1.Caption = "Find Next"
Command2.Caption = "Find Previous"
Check1.Caption = "Whole word"
Check2.Caption = "Case"
' Check3.Caption = "RegExp" 'Sadly RegExp functionality is not available on a RichTextBox
End Sub
dglienna at 2007-11-9 19:36:05 >
# 4 Re: Find Text in MS Word
Hi Thanks for the reply.

You have sent the code for the Rich text control, but I DONT HAVE IT < I JUST PROCESSING THIS CODE IN THE Excelsheet .

Please let me know how can I do that ?
sathmaha at 2007-11-9 19:37:02 >
# 5 Re: Find Text in MS Word
Do you mean you aren't using VB6? Should have said so in the first post.
If you mean Excel, and you're opening a Word doc, then, I'd do this:

Record a macro to use FIND and then code that.
dglienna at 2007-11-9 19:38:12 >
# 6 Re: Find Text in MS Word
Oh..Ok ! Thanks very much, I can able to find the TEXT IN THE WORD USING FIND command , and how can I able to return the particular paragraph line through the FIND command...because actually I need to find the whole paragraph...

Thanks for the help.

Sorry for troubles caused to you.
sathmaha at 2007-11-9 19:39:06 >
# 7 Re: Find Text in MS Word
You can use VbCrLf to detect newlines. You can probably detect paragraph marks, or the "/" in the date, or something.

You know what the data looks like.
dglienna at 2007-11-9 19:40:05 >
# 8 Re: Find Text in MS Word
This is I'm doing ...

Actual Word = COMMENCING ON 27TH JULY 2005

i'M USING TO FIND THE FIRST @COMMENCING ON@ AND GOING TO FIND THE REST OF THEM...

Set myRange = WordObj.ActiveDocument.Content
If myRange.Find.Execute(FindText:="COMMENCING ON" ) = True Then

msgbox("found")

Else

MsgBox ("NOT Found COMMENCING ON")
End If

// Its found the particluar word, not the whole paragraph , I need to get the whole particular paragraph (which including 24th july2004) also its not hard coded...the date can vary everyday...
sathmaha at 2007-11-9 19:41:08 >
# 9 Re: Find Text in MS Word
Move the text to strbuff and then split it like this:

Option Explicit

Private Sub Form_Load()
Dim x As Integer, st As String
Dim ff As Integer
Dim strBuff As String
Dim str() As String
ff = FreeFile
Open App.Path & "\to do.txt" For Input As #ff
strBuff = Input(LOF(ff), ff)
Close #ff
' ------ two ways to skin a cat -----
MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
' -----------------
str() = Split(strBuff, vbCrLf)
MsgBox "There are " & UBound(str) + 1 & " lines in the file"
For x = 0 To UBound(str)
st = st & str(x) & vbCrLf & vbCrLf
Next x
MsgBox st
End Sub
dglienna at 2007-11-9 19:42:11 >
# 10 Re: Find Text in MS Word
Sorry ! I bit confused about your code, could you explain it to suitable to M word...thanks. dont get misunderstand me ...becoz I'm new to VB
sathmaha at 2007-11-9 19:43:10 >
# 11 Re: Find Text in MS Word
Is there are anyway that through the find command can able return the Paragraph line number ?? ( Then its easy to get the whole paragraph ....
sathmaha at 2007-11-9 19:44:14 >
# 12 Re: Find Text in MS Word
How would that help? You need to know how big the paragraph is. Post your zipped up project and all needed files.
dglienna at 2007-11-9 19:45:12 >