PDA

View Full Version : Find Cut Move and Past - loop needed



Gabor
09-18-2007, 08:01 PM
Please help me with the following problem.
I am in need of a macro which cuts distinct parts of several-line lists (usually one unit in a line: a two-figure number together with the surrounding parentheses), places them to the end of the same line, preceeded by a ? character then goes on to the next occurrence to do the same until it finishes the whole document.
I recorded a macro:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "(^#^#)"
.Replacement.Text = "#"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Cut
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:="?"
Selection.Paste

...but unfortunately I have no idea how to instruct it to stop searching when it reached the end of the document, so it gives an error message then.
Also in the above code there seem to be superfluous lines but I am not quite sure which may be omitted. Perhaps the lines containing "False"?

Thank you very much for any help in advance.

TonyJollans
09-18-2007, 11:38 PM
To make the search stop, change

.Wrap = wdFindContinue

to:

.Wrap = wdFindStop


As for the other lines, leave them in - one day they might make a difference. I can give you a full explanation if you want one but they are just making sure all the right options are set.

fumei
09-19-2007, 07:50 AM
I do agree with Tony that you may have use for the other ( possibly extraneous) instructions. It is better to know what they are, and what they do, before you remove them. Although, for the most part, they are generally (IMO) extraneous.

Here is an alternative to your code, and a demo. You can run the code by clicking "Try This" on the top menu bar. It may not be exactly what you want.

For example, I am unsure about your Replacement text. Do you want "(23)" replaced with....ummmm, "#"?? In which case, what is the number?

Further, I am not sure if you really want "?" followed by "(##) with no spaces.

"?(42)"

rather than:

"? (42)"

or even

" ? (42)"

ie. with a space before the "?"

All that can be adjusted. As it is, the demo does put a space before, and after the "?".

Also, there is an assumption that your "line" is actually a paragraph. If it is not, there may be errors. But you can see the demo effect on the example text in the document.Sub TryThis()
Dim r As Range
Dim strStuff As String
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
.Text = "(^#^#)"
.Replacement.Text = "#"
Do While .Execute(Forward:=True) = True
strStuff = r.Text
With r
.Cut
.Expand unit:=wdParagraph
.Collapse Direction:=wdCollapseEnd
.Move unit:=wdCharacter, Count:=-1
.InsertAfter Text:=" ? " & strStuff
.Collapse Direction:=wdCollapseEnd
End With
Loop
End With
End Sub

Gabor
09-19-2007, 11:00 PM
Thank you very much to both of you.
Unfortunately the change to wdFindStop suggested by Tony did not work, but the Try this macro by Gerry does exactly what I imagined.
Obviously it was my mistake to say line instead of paragraph which actually it is.
For other peculiarities: by this macro and others I am trying to prepare a fairly complicated document for my colleague to redesign it in InDesign so that he could start off with an edited version. SO the ? will be a mark for him (actually for InDesign) to start a new nested style there and the number must be simply repeated.
"#" got in the way among the other extraneous code lines when I recorded the macro.
Thanks again, this was great help for me! :hi:

fumei
09-20-2007, 08:45 PM
Actually, it is probably the most common phrase misconstrued with Word. line vs paragraph.

There can be multiples lines that do not terminate with a paragraph mark. A single paragraph can have many many "lines".

Or just one.

This is some text that
is on multiple lines
in the document.

The above could be one, two, or three paragraphs, depending on how each "line" is terminated. The paragraph mark is probably THE most important "thing" in Word. Never mind using VBA, just selecting in a document can have profound differences. Selecting text that includes the paragraph mark, is VERY different from selecting the same text and NOT including the paragraph mark.

This is one reason most experienced Word users keep Show/Hide ON. So we can see those paragraph marks.