A macro for word 2000 to search for imbedded white space

I want to create a macro to search for imbedded white space. The macro would take two strings (1) the 1st string and (2) the 2nd string. The macro would then use the following expression between the strings and do the search: [!a-zA-Z0-9]@.

So, if I want to search for in<white space>with as in:

     laskdj laskjf in
     with alskdj asldkj

I would type "in" and "with" to the macro.
ESQuicksallAsked:
Who is Participating?
 
R_RajeshConnect With a Mentor Commented:
replace this line
str2 = str2 & str1(i) & "[!A-Za-z0-9]@"

with this to remove the trailing [!A-Za-z0-9]@
str2 = str2 & str1(i) & IIf(i <> UBound(str1), "[!A-Za-z0-9]@", "")

just create your module in the normal template (you probably already have, because thats the default location) and all documents will have access to it

unfortunately wildcards are case sensitive, so its got to be an exact match
0
 
ESQuicksallAuthor Commented:
Better yet, the macro would convert all space to the white space string given above. Then do the search. So if I type:

"this is a test"

It would find:
   this
   is a test
Or:
  this is
  a test
Or:
  this is a
  test
0
 
R_RajeshCommented:
in your document hit alt+f11 this will open the vbe window, here select module form the insert menu and paste the code below. close the vbe window, back in word, hit alt+f8 select myfind and click on run.

Sub myfind()
Dim str1() As String, str2 As String
Dim i As Integer
str1 = Split(InputBox("Enter the string to be found:"), " ")
For i = LBound(str1) To UBound(str1)
str2 = str2 & str1(i) & "[!A-Za-z0-9]@"
Next i
Selection.Find.ClearFormatting
With Selection.Find
.Text = str2: .Wrap = 1
.MatchWildcards = True
End With
Selection.Find.Execute
End Sub

Rajesh
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Joanne M. OrzechManager, Document Services CenterCommented:
Why not just do one search and replace:

Sub ReplaceWhite()
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "in^pwith"
        .Replacement.Text = "whatever you want here"
        .Forward = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Maybe I'm not understanding...somewhat common lately :)
0
 
ESQuicksallAuthor Commented:
I tried "SCSI architecture model" and sometimes it finds it but sometimes it doesn't. The expression formed is: The[!A-Za-z0-9]@iSCSI[!A-Za-z0-9]@protocol[!A-Za-z0-9]@

   This document describes a transport protocol for Internet Small
   Computer Systems Interface (iSCSI) that works on top of TCP.  The
   iSCSI protocol aims to be fully compliant with the standardized SCSI
   architecture model.
0
 
ESQuicksallAuthor Commented:
Actually, I want to do "finds", not replace. But I tried the above and got an error on this line:

    Selection.Find.Execute Replace:=wdReplaceAll

It says "Run-time error 5692
0
 
R_RajeshCommented:
if you type in this into the input box
SCSI architecture model

this is the expression that will be formed
SCSI[!A-Za-z0-9]@architecture[!A-Za-z0-9]@model[!A-Za-z0-9]@

i tried it 3 times and the code found the string every time
0
 
ESQuicksallAuthor Commented:
Now it seems to work.

1) Is it case sensitive? I tryed "The iscsi protocol" and it doesn't find it but if I try "The iSCSI protocol", it does. Maybe I had a case problem before.

The expression formed is: The[!A-Za-z0-9]@iscsi[!A-Za-z0-9]@protocol[!A-Za-z0-9]@

2) After I get it working, is there a way I can put it into normal.dot so I have it for every file?
3) Is there a way to remove the trailing [!A-Za-z0-9]@ so I can also search for "The iscsi prot"? (i.e., partial string)
0
 
ESQuicksallAuthor Commented:
I changed it to look like this and it still seems to be case sensitive:

Sub myfind()
Dim str1() As String, str2 As String
Dim i As Integer
str1 = Split(InputBox("Enter the string to be found:"), " ")
For i = LBound(str1) To UBound(str1)
str2 = str2 & str1(i) & "[!A-Za-z0-9]@"
Next i
Selection.Find.ClearFormatting
With Selection.Find
.Text = str2: .Wrap = 1
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = True
End With
Selection.Find.Execute
End Sub
0
 
ESQuicksallAuthor Commented:
I took care of cases 2 and 3 above but still can't get it to be case insensitive. My current macro looks like this:

Sub myfind()
Dim str1() As String, str2 As String
Dim i As Integer
str1 = Split(InputBox("Enter the string to be found:"), " ")
For i = LBound(str1) To UBound(str1) - 1
str2 = str2 & str1(i) & "[!A-Za-z0-9]@"
Next i
str2 = str2 & str1(UBound(str1))
Selection.Find.ClearFormatting
With Selection.Find
.Text = str2: .Wrap = 1
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = True
End With
Selection.Find.Execute
End Sub
0
 
ESQuicksallAuthor Commented:
Sorry, I was experimenting and did a past error... it looks like this:

Sub myfind()
Dim str1() As String, str2 As String
Dim i As Integer
str1 = Split(InputBox("Enter the string to be found:"), " ")
For i = LBound(str1) To UBound(str1) - 1
str2 = str2 & str1(i) & "[!A-Za-z0-9]@"
Next i
str2 = str2 & str1(UBound(str1))
Selection.Find.ClearFormatting
With Selection.Find
.Text = str2: .Wrap = 1
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
End With
Selection.Find.Execute
End Sub
0
 
ESQuicksallAuthor Commented:
Ignore my statements after you gave your answer regarding case sensitivity ... I did not see that response.
0
 
ESQuicksallAuthor Commented:
Thanks, now I would like to be able to search for something like "3.2.8.    Message Sy" but since, for ease of operation, I prefer doing a cut and past of the original text I get the following expression:
   3.2.8.[!A-Za-z0-9]@[!A-Za-z0-9]@[!A-Za-z0-9]@[!A-Za-z0-9]@Message[!A-Za-z0-9]@Sy
which will not match on:
   3.2.8.  Message Sy
I would like the expression to be like this:
   3.2.8.[!A-Za-z0-9]@Message[!A-Za-z0-9]@Sy

Can you help with that?
Is there a way to award more points after I already did an accept?
0
 
R_RajeshCommented:
Dear ESQuicksall,

Thanks for the grade. You can post on this thread until you get the code to work just the way want it to. and dont worry about the points. Only problem is that its 2:30am here and i may not be able to answer you immediately...
 
just replace
str2 = str2 & str1(i) & "[!A-Za-z0-9]@"

with this
str2 = str2 & str1(i) & IIf(i <> UBound(str1) And str1(i) <> "", "[!A-Za-z0-9]@", "")

Sub myfind()
Dim str1() As String, str2 As String
Dim i As Integer
str1 = Split(InputBox("Enter the string to be found:"), " ")
For i = LBound(str1) To UBound(str1)
str2 = str2 & str1(i) & IIf(i <> UBound(str1) And str1(i) <> "", "[!A-Za-z0-9]@", "")
Next i
MsgBox str2
Selection.Find.ClearFormatting
With Selection.Find
.Text = str2: .Wrap = 1
.MatchWildcards = True
End With
Selection.Find.Execute
End Sub
0
 
ESQuicksallAuthor Commented:
That works perfect. Thanks for all your help. Too bad Microsoft didn't allow a case insensitive compare with that.

You can go to bed now :) I'm in EST.
0
 
R_RajeshCommented:
try this for a case insensitive compare

Sub myfind()
Dim oRegExp As Object, oFound As Object
Dim str1() As String, str2 As String
Dim i As Integer
Set oRegExp = CreateObject("VBScript.Regexp")
str1 = Split(InputBox("Enter the string to be found:"), " ")
For i = LBound(str1) To UBound(str1)
str2 = str2 & str1(i) & _
IIf(i <> UBound(str1) And str1(i) <> "", "\s*", "")
Next i
With oRegExp
.Pattern = str2: .IgnoreCase = True: .Global = True
End With
Selection.WholeStory
Set oFound = oRegExp.Execute(Selection)
If oFound.Count > 0 Then
Selection.SetRange Start:=oFound(0).firstindex, _
End:=oFound(0).firstindex + oFound(0).Length
Else
MsgBox "Search string not found"
End If
End Sub
0
 
ESQuicksallAuthor Commented:
That works ... thanks.

Eddy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.