Hi
I have a small issue.
I'd like to find occurances of square brackets [], or this brackets (), in a word document and check if number or alphabet is immediately preceeding or suffixing it without space and if so insert one space.
Eg. my house[VBF]is in china
This I want to change it to my house [VBF] is in china
Eg. my house(VBF)is in china
This I want to change it to my house (VBF) is in china
yes I used the wild card option in more..
I believe it goes something like this in Find section: [A-Za-z0-9]\(
I get struck up in the Replace section:???????
Thanks in advance...
patience is not the ability to wait but the ability to keep a good attitude while waiting
Last edited by VBFnewcomer; May 4th, 2011 at 11:51 PM.
Reason: insufficient information
THANK YOU FOR RECOGNIZING MY CONTRIBUTION AND RATING ME!
i remember trying to do this some time ago, but i think my internet crashed or some such
i believe you would have to use a VBA procedure to do this, only way i figured was to use replace to put space next to each bracket (before or after), then replace double spaces with single space, either in all instance, or when next to each bracket
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
if you really need to only add the space where character is number or letter (so not punctuation or other brackets, or like!@#$$%^&* etc), the you would need to use VBA to find each bracket, check the character before (or after), then add space as appropriate, then find next bracket and repeat process, you would need to use instr to do this, otherwise loop through every character, to determine if it is a bracket
you can not do what you originally specified using words find /replace method
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
The current issue is slightly away from the main topic...but related..
you can test this, it should do for both
vb Code:
Dim char As Range, chars As String, alpha As String, notnums as string
alpha = "ABCDEFGHIJKLNOPQRSTUVWXYZ0123456789"
notnums "ABCDEFGHIJKLNOPQRSTUVWXYZ"
For Each char In ThisDocument.Content.Characters
Select Case char
Case "(", "["
If InStr(1, alpha, ThisDocument.Characters(char.Start), vbTextCompare) > 0 Then char.InsertBefore " "
Case ")", "]"
If InStr(1, alpha, ThisDocument.Characters(char.End + 1), vbTextCompare) > 0 Then char.InsertAfter " "
Case 0 To 9
If InStr(1, notnums, ThisDocument.Characters(char.Start), vbTextCompare) > 0 Then char.InsertBefore " "
If InStr(1, notnums, ThisDocument.Characters(char.End + 1), vbTextCompare) > 0 Then char.InsertAfter " "
End Select
Next
minimal testing done, as checks every character in the document, it may be too slow, but it does test for all brackets and numbers, you can add any other required characters to strings for them to be included for spaces to be inserted
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
I added your code in a sub just for the 0-9 case (at present) and ran it..nothing happened. Then I put the break point at "Case 0-9" and found that the loop does not enter the line next to it....
Code:
Dim char As Range, chars As String, alpha As String, notNums As String
alpha = "ABCDEFGHIJKLNOPQRSTUVWXYZ0123456789"
notNums = "ABCDEFGHIJKLNOPQRSTUVWXYZ"
For Each char In ThisDocument.Content.Characters
Select Case char
Case 0 To 9
If InStr(1, notNums, ThisDocument.Characters(char.Start), vbTextCompare > 0) Then char.InsertBefore " "
If InStr(1, notNums, ThisDocument.Characters(char.End + 1), vbTextCompare > 0) Then char.InsertAfter " "
End Select
Next
BTW
Code:
notNums = "ABCDEFGHIJKLNOPQRSTUVWXYZ"
did not have "=" so I added it...
THANK YOU FOR RECOGNIZING MY CONTRIBUTION AND RATING ME!
Then I put the break point at "Case 0-9" and found that the loop does not enter the line next to it....
works for me
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
1. Always add a space before "[" and "(" regardless they are preceeded by a space or not.
2. Always add a space after "]" and ")" regardless they are followed by a space or not.
3. In both cases, if a space (before or after) already existed, it will become double spaces; so just replace all double spaces with single space.
Easy!
Don't forget to use [CODE]your code here[/CODE] when posting code
If your question was answered please use Thread Tools to mark your thread [RESOLVED]
basically what i put in post #3, which did not completely fill the original request and did not cover the additional requirement in posts #8 /10
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Dear Anhn & west,
I did understand what you were saying and already implemented it in the same order as suggested by you....Thanks for your expert guidance.
Ive included a sample file for your quick reference.
In each word block (I believe its called simply word in Office VBA) one must check for series of alpahbets or numbers. If there exist no space after the end of series then we must add a space.
eg. This document has200pages.
Here has200pages is a block. First we identify the start series, in the present case 'has'
this is followed by numbers ie. '200',
there is no intervening space so we add one space
this is followed by numbers ie. 'pages',
here again there is no intervening space so we add one space
Imp: Existing data must not be deleted
THANK YOU FOR RECOGNIZING MY CONTRIBUTION AND RATING ME!
as my testing for the code in post #11 appeared to work correctly i do not understand why it did not work for you
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete