PDA

View Full Version : FIND-problem



JPDO
08-07-2006, 05:32 AM
Hi,

I have to manipulate some data in a Word-document using VBA.
First i have to search for the word ?Director? in a document.
If i found it i should select from there till the rest of the document and set this selection with fonttype = ?Courier New? / 9pts

If i do not find it i should search for the word ?SIGNED?.
This word is always present in the document.
When i find it i should delete it and from there on till the end of the document i should set the font type = ?Courier new? / 12 pts.

Code To find the word ?Director?

Selection.Find.ClearFormatting
With Selection.Find
.Text = "DIRECTOR"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

Can someone tell me how i could know if the word was found or not ?
When it is found it is automatically selected, so i can continue with Selection.find.execute.

Thanks for any reply.

EricFletcher
08-07-2006, 07:27 AM
Try something based on an approach like this:
With Selection.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Execute FindText:="Director"
End With
If Selection.Find.Found = True Then
...

When the word is found, it will process the code after the If. The wdFindStop will stop looking if you reach the end of the document; otherwise it will just keep looping and finding ones you've already dealt with.

mdmackillop
08-07-2006, 09:04 AM
Try

Sub DirectorSigned()

Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = "Director"
.MatchCase = False
.Execute
End With
If Len(Selection) = 8 Then
MsgBox "'Director' found!"
With Selection
.EndKey Unit:=wdStory, Extend:=wdExtend
.Font.Name = "Courier New"
.Font.Size = 9
End With
Else
MsgBox "'Director' not found!"
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = "Signed"
.MatchCase = False
.Execute
End With
With Selection
.Delete Unit:=wdCharacter, Count:=1
.EndKey Unit:=wdStory, Extend:=wdExtend
.Font.Name = "Courier New"
.Font.Size = 12
End With
End If
Selection.HomeKey Unit:=wdStory
End Sub

mdmackillop
08-23-2006, 10:57 AM
Is this resolved?