Word VBA >> With.Selection.Find & strings

by Lucas » Sat, 24 Jul 2004 03:19:52 GMT

I need to search for the second occurrence in my document
of whatever the value of the first line happens to be.

I'm unsure how to get With.Selection.Find.Text to use a
value from a string that I've set earlier in the macro.
Is this even possible?

Thanks in advance.

Sub Whatever()
Dim Company As String
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Company = Selection
MsgBox Company, vbOKOnly, "blah"
Selection.EndKey Unit:=wdLine
Selection.Find.ClearFormatting
With Selection.Find
.Text = Company
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub


Word VBA >> With.Selection.Find & strings

by Shauna Kelly » Sat, 24 Jul 2004 11:39:59 GMT


Hi Lucas

Try something like the following:

Sub FindSecondOccurreceOfTheTextOfTheFirstLine()

Dim strCompany As String
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
strCompany = Selection.Range.Text

Selection.Collapse Direction:=wdCollapseEnd

Selection.Find.ClearFormatting
With Selection.Find
.Text = strCompany 'Use the variable name here
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop 'Don't use wdFindContinue or
'it will wrap and find the first line
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute 'Perform the search

If .Found Then
MsgBox "We found the text"
Else
MsgBox "We didn't find the text"
End If
End With
End Sub


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word













Similar Threads

1. Find.Selection does not change selection when the find criteria is found

I have been creating a nice template which will control styles when
they are pasted into a document and any unrecognised style will be set
to normal.

I then wanted to offer the user a macro button which will jump down to
any text of style normal.

I expected this to work with just a normal Find.Selection statement -
however the cursor in the document does not move to the found
selection.

Sub Find_Unformatted()
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Normal")
    Selection.Find.ParagraphFormat.Borders.Shadow = False
    With Selection.Find
        .Text = "^?"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
End Sub

Can anyone help me please !!!!

2. Selection.Find.Found returns true after all items have been found - Word VBA

3. Finding One String in Another String

I have the following in a macro.

InStr(1, cbLocate, "NIC/", vbTextCompare)

The problem is that sometimes the NIC/ tag appears twice in a record, and
the first occurance never follows a paragraph mark, and is always followed
by unusable data.  However, the second tag is always preceded by a paragraph
mark, and is followed by good data.  When the NIC/ tag appears only once in
the data, it is also always preceded by a paragraph mark and followed by
good data.

So what I need to do is make the above command find ONLY the NIC/ tag that
is preceded by a paragraph mark.  Alternatively, I could make it pick up the
first occurance of the NIC/ tag when there is only one, and ignore it when
there are two in the record.  Can anyone help me out with either of these
two possible solutions?

--Tom


4. Compare two strings and find longest common sub-string - Word VBA

5. Find can find a string, but not Replace.

Version: 2008
Operating System: Mac OS X 10.6 (Snow Leopard)
Processor: Intel

I have Office 2008. No updates are currently available. <br><br>I performed a case-specific Find and Replace option, replacing every instance of a generic word that is capitalized with a lowercase version. <br><br>That generic word is also part of a proper business name, so I attempted to perform a Find and Replace for the full, three-word business name. When I do this, Find and Replace reports no instances found. <br><br>Later, proofreading, I found several instances. I copied the text into the Find field of Find and Replace, and no instances were found. <br><br>If I click the Find button to switch the dialog to Find mode (from Find and Replace mode), my query remains in the search box, and 32 instances are found. <br><br>All searches, in Find or Find and Replace were performed with case sensitivity enabled. <br><br>I'm going to try to find a Windows machine with Word 2007 this morning, but for future reference, what's going on here, and how do I get around it?

6. Exit Find if string not found - MS Word&VBA

7. Find and replace using values calculated from the found string

I am trying to write a program which can find and translate Japanese
dates, in the form

Heisei 19 nen 12 gatsu 14 nichi

into Western dates.

So far I have made the routine at the end of this message.

My problem is that I want to do some work on the strings in the find
and then make a replace string using the calculated values. Firstly
the months in wide format need to be replaced with words. I overcame
that problem by using a loop from one to twelve and repeatedly formed
the "find" string and its replacement using an array.  Secondly the
years need to be replaced with western years. For example, Heisei 19
is 2007. It is just a matter of subtracting 1988 from the year, so I
guess I could use another loop to loop over each year. However, my
question is, what is the "smart" way to do this? That is, rather than
using these loops, how could I find the string, calculate the month
and the year, and then replace it, rather than using two loops?

I have tried using a loop of the form
Do While .Execute()
Selection.insertBefore

or

Selection.insertAfter

only to get into an infinite loop resulting in a crash.

Thanks for any advice.

=========================== Function follows (contains Japanese
characters)

Function IntToWide(i As Integer)
Dim widenumbers(0 To 9) As String
Dim j As Integer
widenumbers(0) = "$B#0(B"
widenumbers(1) = "$B#1(B"
widenumbers(2) = "$B#2(B"
widenumbers(3) = "$B#3(B"
widenumbers(4) = "$B#4(B"
widenumbers(5) = "$B#5(B"
widenumbers(6) = "$B#6(B"
widenumbers(7) = "$B#7(B"
widenumbers(8) = "$B#8(B"
widenumbers(9) = "$B#9(B"
j = i
Do While j > 0
    IntToWide = widenumbers(j Mod 10) & IntToWide
    j = j \ 10
Loop
End Function


Sub TranslateDate()
 Dim myMonth(1 To 12)   As String
Dim i As Integer
myMonth(1) = "January"
myMonth(2) = "February"
myMonth(3) = "March"
myMonth(4) = "April"
myMonth(5) = "May"
myMonth(6) = "June"
myMonth(7) = "July"
myMonth(8) = "August"
myMonth(9) = "September"
myMonth(10) = "October"
myMonth(11) = "November"
myMonth(12) = "December"
'
' Macro1 Macro
' $B5-O?F|(B 2007/12/14 $B5-O?<T(B Ben Bullock
'
For i = 1 To 12

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "$BJ?@.(B([$B#0(B-$B#9(B]{1,})$BG/(B" & IntToWide(i) & "$B7n(B([$B#0(B-$B#9(B]{1,})$BF|(B"
        .Replacement.Text = myMonth(i) & " \2 \1"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchFuzzy = False
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
Next i
End Sub

8. Find and Replace - found selection positioning - Word Document management