Solved

A macro for word 2000 to search for imbedded white space

Posted on 2004-09-16
17
364 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:ESQuicksall
  • 11
  • 5
17 Comments
 

Author Comment

by:ESQuicksall
ID: 12075293
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
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 12075980
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
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 12076234
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
 

Author Comment

by:ESQuicksall
ID: 12076295
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
 

Author Comment

by:ESQuicksall
ID: 12076322
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
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 12076492
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
 

Author Comment

by:ESQuicksall
ID: 12076612
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
 
LVL 24

Accepted Solution

by:
R_Rajesh earned 300 total points
ID: 12076723
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ESQuicksall
ID: 12078175
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
 

Author Comment

by:ESQuicksall
ID: 12078349
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
 

Author Comment

by:ESQuicksall
ID: 12078359
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
 

Author Comment

by:ESQuicksall
ID: 12078433
Ignore my statements after you gave your answer regarding case sensitivity ... I did not see that response.
0
 

Author Comment

by:ESQuicksall
ID: 12078647
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
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 12079185
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
 

Author Comment

by:ESQuicksall
ID: 12079227
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
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 12082013
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
 

Author Comment

by:ESQuicksall
ID: 12088625
That works ... thanks.

Eddy
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now