PDA

View Full Version : Insertion of paragraphs issue / unsolvable



Zack
07-30-2012, 08:20 AM
I have difficulties with the following code

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[^13]{1;}(why)[^13]{1;}"
.Replacement.Text = "^p^p^p\1^p^p"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub



I have attached two documents. One which explains how it looks like before the VBA, and the other one which shows how it Should look like after using the macro, but it doesn't. I can't resolve the issue.

Thank you.

Zack
07-30-2012, 08:21 AM
And the document which shows how it's supposed to look like.

fumei
07-30-2012, 01:47 PM
I can not actually open the documents to see, but is it possible that the spaces between the text lines are not true paragraphs?

You say it does not work, but does that mean:

It does nothing at all?
It adds 17 new paragraphs betwen the lines?
It adds 1 paragraph between the lines?
It adds 2 paragraphs between the first lines, but 153 new paragraphs between the next?

I am being silly, but we are not mind readers, nor can we actually look over your shoulder to see, so "does't work" tells us nothng.

Paul_Hossler
07-30-2012, 02:26 PM
Not attempting to read anybody's mind, but

1. The {1,} syntax needs a comma, not a semicolon



From n to m occurrences of the previous character or expression {n,m} 10{1,3} finds 10, 100, and 1000.



2. The (way) needs to be (Way)



When the Use wildcards check box is selected, Word finds only the exact text that you specify. Notice that the Match case and Find whole words only check boxes are unavailable (dimmed) to indicate that these options are automatically turned on. You can't turn off these options



3. I put (...) around all 3 .Find pieces, and use \2 in the .Replace. Don't know if it's required, but this seems to work for me


Sub test()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "([^13]{1,})(Why)([^13]{1,})"
.Replacement.Text = "^p^p^p\2^p^p"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

Selection.Find.Execute Replace:=wdReplaceAll
End Sub



Paul

Zack
07-30-2012, 09:49 PM
I can not actually open the documents to see, but is it possible that the spaces between the text lines are not true paragraphs?

You say it does not work, but does that mean:

It does nothing at all?
It adds 17 new paragraphs betwen the lines?
It adds 1 paragraph between the lines?
It adds 2 paragraphs between the first lines, but 153 new paragraphs between the next?

I am being silly, but we are not mind readers, nor can we actually look over your shoulder to see, so "does't work" tells us nothng.

It does nothing at all.

Files are uploaded now as .doc.

Zack
07-30-2012, 09:51 PM
And the second file.

Zack
07-30-2012, 10:01 PM
Not attempting to read anybody's mind, but

1. The {1,} syntax needs a comma, not a semicolon

Paul

not an English system




2. The (way) needs to be (Way)

Paul

yes

This is what I get when I use my code.
Should add two paragraphs from above and one from below, but doesn't always work. It is not ought to just simply add two new paragraphs of course, but to decrease the number if it's higher than two. So if there are twenty paragraphs, it should always be two at the end.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[^13]{1;}(Why)[^13]{1;}"
.Replacement.Text = "^p^p^p\1^p^p"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

This sometimes works, but sometimes doesn't.

Zack
08-01-2012, 07:20 AM
Basically, when I use this

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[^13]{1;}(why)[^13]{1;}"
.Replacement.Text = "^p^p^p\1^p^p"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Sometimes it adds paragraphs when needed and decreases when needed as well, but sometimes it does nothing. I was wondering what is wrong with the code?

macropod
08-01-2012, 03:55 PM
Hi Zack,

I suspect the cases where it does nothing is when you have two consecutive qualifying paragraphs. If so, that's because one F/R range overlaps the next. You probably need two F/R operations - one to handle paragraphs before and one to handle paragraphs after.

Zack
08-01-2012, 09:45 PM
Hi Zack,

I suspect the cases where it does nothing is when you have two consecutive qualifying paragraphs. If so, that's because one F/R range overlaps the next. You probably need two F/R operations - one to handle paragraphs before and one to handle paragraphs after.

Thx macropod.

Is there any alternative way of achieving this? Other than the code I've posted..

macropod
08-01-2012, 09:53 PM
As I said, two separate processes:
Sub Test()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Text = "([^13]why)[^13]{1;}"
.Replacement.Text = "\1^p^p"
.Execute Replace:=wdReplaceAll
.Text = "[^13]{1;}(why[^13])"
.Replacement.Text = "^p^p^p\1"
.Execute Replace:=wdReplaceAll
End With
End Sub

Zack
08-02-2012, 08:22 AM
Thx macropod, this works good, but what if there are three words in one paragraph that you want to move like that?

For example, it's not just "why", but "why is that". I've tried putting just "why" hoping that all three words (sentence) would be moved with that VBA, but they didn't.. Used capital "W" as well, nothing happens. It only works if there is one word, for example - "Why".

How can I set it up to move the sentence, for example - "why is that" ?

Thx in advance.

Like this

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Text = "([^13]why is that)[^13]{1;}"
.Replacement.Text = "\1^p^p"
.Execute Replace:=wdReplaceAll
.Text = "[^13]{1;}(why is that[^13])"
.Replacement.Text = "^p^p^p\1"
.Execute Replace:=wdReplaceAll
End With
End Sub

Except for that it doesn't work.

Even better solution would be to use just "why" as a word and anything that goes after (whether numbers or letters) would be moved together with "why".

So - "why is that" would be moved as well as "why 3243 DD# " as long as it's within the same line.

macropod
08-02-2012, 03:26 PM
I suspect the issue has to do with both the use of 'Selection' and the .Wrap setting. Try:
Sub Test()
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Text = "([^13]why is that)[^13]{1;}"
.Replacement.Text = "\1^p^p"
.Execute Replace:=wdReplaceAll
.Text = "[^13]{1;}(why is that[^13])"
.Replacement.Text = "^p^p^p\1"
.Execute Replace:=wdReplaceAll
End With
End Sub
Note also that wildcard searches are always case-sensitive. So, if you want to find 'Why is that', you need to specify 'Why is that' (or use '[Ww]hy is that' to get 'why is that' as well).

Zack
08-02-2012, 09:48 PM
Thx macropod it works, but it doesn't work when there is another set of words after "why"

it can be
/why is that
/why should I
/why do you think that is so
etc.

So it's important for me to use that "why" in the code and that all that follows it, no matter what, would react with the word "why".

So anything that's after "why" would react with the word why. It can be
"why SADA #$ #$@ #@$#" and the entire line would behave as the word "why" in this case.

I know it sounds complicated but I am doing my best to describe it.

macropod
08-02-2012, 11:03 PM
That's because you have a Find expression that specifically checks to ensure there is at least one paragraph break before and after the words being searched for.

Zack
08-05-2012, 03:09 PM
That's because you have a Find expression that specifically checks to ensure there is at least one paragraph break before and after the words being searched for.

Having that in mind, is there a way to accomplish what I referred to in my previous post?

To be exact, is there a way to include one word in find and all the rest in the same paragraph would behave as that first word?

macropod
08-05-2012, 03:26 PM
If you look at the Find/Replace code, you'll see that the first Find expression says to only find strings that begin with a paragraph break. If that's not what you want, delete the first [^13]. Similarly, you'll see that the second Find expression says to only find strings that end with a paragraph break. If that's not what you want, delete the last [^13].

Zack
08-06-2012, 12:40 AM
If you look at the Find/Replace code, you'll see that the first Find expression says to only find strings that begin with a paragraph break. If that's not what you want, delete the first [^13]. Similarly, you'll see that the second Find expression says to only find strings that end with a paragraph break. If that's not what you want, delete the last [^13].

When I delete the last [^13] then only the first word of the sentence moves, but the rest of the sentence stays where it was.

For example. "Why is it like that"

If I use this:

With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Text = "([^13]Why){1;}"
.Replacement.Text = "\1^p^p"
.Execute Replace:=wdReplaceAll
.Text = "[^13]{1;}(Why)"
.Replacement.Text = "^p^p^p\1"
.Execute Replace:=wdReplaceAll
End With
End Sub

Then only the word "Why" will move, but not the rest of the sentence.
If [^13] represents a paragraph end, what would represent any letter and/or number that would follow the word "Why" so the entire phrase "Why is it like that" would move by writing only "Why". So if the sentence is not "Why is it like that", but "Why ZXY 903" it would still move because we have a paragraph [^13] before "why" and [SOMETHING] after it.


With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Text = "([^13]Why) [any letter or number]{1;}"
.Replacement.Text = "\1^p^p"
.Execute Replace:=wdReplaceAll
.Text = "[^13]{1;}(Why) [any letter or number]"
.Replacement.Text = "^p^p^p\1"
.Execute Replace:=wdReplaceAll
End With
End Sub

macropod
08-06-2012, 08:56 PM
Why on eareth do you have this:
.Text = "([^13]Why) [any letter or number]{1;}"
and this:
.Text = "[^13]{1;}(Why) [any letter or number]"
Have you stopped to consider what those Find constructions mean - or what the corresponding replace expressions mean? If you're going to muck around with the code like this, you're wasting my (our) time - what you're using is nothing like what has been suggested!

How about taking a little time to learn the basics of wildcard F/R syntax. Then you'll understand why your munged code can't possibly work.

fumei
08-06-2012, 09:33 PM
oi vay!

If I may - somewhat - paraphrase...

Zack, code works as logical expressions. It is not magical. It must be constructed in logical expressions, not vague variable (as in changing) decisions. Logical constructions result in logical results. I agree with macropod, in that it would be a real use of your time to work on getting a solid basis of both wildcards, and the Find operation itself.

macropod is one of the best (literally) people in the world (literally) when it comes to the use of wildcards. But he can not help when you can not understand why your code is, ummmm, munged. Or when you change the parameters.

Zack
08-06-2012, 09:53 PM
Why on eareth do you have this:
.Text = "([^13]Why) [any letter or number]{1;}"
and this:
.Text = "[^13]{1;}(Why) [any letter or number]"


I wrote this just as an example of what I had in mind. Of course I knew it wouldn't work. I assumed that in those brackets I need a value which would include any number and/or letter, thus I wrote "any letter or number".

If [^13] represents a paragraph end, what would represent any letter and/or number that would follow the word "Why" so the entire phrase "Why is it like that" would move by writing only "Why". So if the sentence is not "Why is it like that", but "Why ZXY 903" it would still move because we have a paragraph [^13] before "why" and [SOMETHING] after it.

macropod
08-06-2012, 11:12 PM
Of course I knew it wouldn't work.
So why don't you say what you want instead of wasting time with such code???

And how about providing all parts of the problem. We're not mind readers here. As for finding "any letter or number", a few seconds with the 'Special' prameters in Word's wildcard Find/Replace dialogue would tell you the answer to that. BUT, that isn't going to achieve what you want - which is apparently:

the entire phrase "Why is it like that"
or any other phrase beginning with 'why'. The simple answer is that there is no 'phrase' object in Word and no way to specify one as a Find expression.

Zack
08-07-2012, 01:00 AM
The general idea is to find a word "why" and replace it with

.Replacement.Text = "^p^p^p\1^p^p"

However, if there are other words after "why", but in the same line, they should be replaced with the same Replacement.Text.

I know it sounds hard and if it's impossible it doesn't matter.

macropod
08-07-2012, 01:32 AM
So, is your 'line' a complete paragraph, or at least a line terminated by a manual line break? If so, the task really has nothing to do with the 'phrases' you referred to at all.

You could do yourself a favour by attaching a document with some representative before & after examples of what should and should not be changed.

Zack
08-07-2012, 03:04 AM
So, is your 'line' a complete paragraph, or at least a line terminated by a manual line break? If so, the task really has nothing to do with the 'phrases' you referred to at all.

You could do yourself a favour by attaching a document with some representative before & after examples of what should and should not be changed.

I've posted two examples of before and after, but you are right, I didn't post example of this recent case. My fault.

So here it is.

Zack
08-07-2012, 03:07 AM
And the "After" one.


The entire sentence moves by just using "why" as a find value and it should automatically move the entire paragraph in which "why" is. In this case the sentence "Why is the Sun so bright"?

So if the sentence is "Why hola hola" it would work as well. In the cases I've uploaded.

macropod
08-07-2012, 03:57 AM
Sub Test()
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Text = "([^13][Ww]hy*)[^13]{1,}"
.Replacement.Text = "\1^p^p"
.Execute Replace:=wdReplaceAll
.Text = "[^13]{1,}([Ww]hy*[^13])"
.Replacement.Text = "^p^p^p\1"
.Execute Replace:=wdReplaceAll
End With
End Sub

Zack
08-07-2012, 06:10 AM
It works. Ty.