Solved

A macro for word 2000 to search for imbedded white space

Posted on 2004-09-16
17
361 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ESQuicksall
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ignore my statements after you gave your answer regarding case sensitivity ... I did not see that response.
0
 

Author Comment

by:ESQuicksall
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That works ... thanks.

Eddy
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

763 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

14 Experts available now in Live!

Get 1:1 Help Now