?
Solved

A macro for word 2000 to search for imbedded white space

Posted on 2004-09-16
17
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 

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 1200 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
 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses

770 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