Edit/Replace to end of line

A

AccessNewbie

I have a document with over 700 pages, I need to remove all lines that start
with 43110. All of the paragraphs have different lengths of text and numbers
in them. I would like to use edit/replace to find all paragraphs that start
with 43110 and get all information that goes from there to the paragraph
return ^p. I used to use word a lot and know that we had some key
combination that would look for the next tab or paragraph return or even the
next instance of a word. I would appreciate it if someone out there knows
this or where I can find this information. Thanks in advance!
 
H

Helmut Weber

Hi AccessNewbie,

have a look at this one:

Sub Test56()
Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Paragraphs
If InStr(oPrg.Range.Text, "43110") = 1 Then
oPrg.Range.Delete
End If
Next
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

David Sisson

Here's my version.

Sub DeleteToEndOfParagraph()
'Most of this is from the help file on Find.
Dim Rng1 As Range
Dim SearchString As String

SearchString = "43110"

Set Rng1 = ActiveDocument.Range
With Rng1.Find
Do While .Execute(findtext:=SearchString, Forward:=True, Wrap:=False)
With .Parent
.MoveEnd unit:=wdParagraph, Count:=1
'If you don't want to delete the paragraph marker, uncomment this.
'.MoveEnd unit:=wdCharacter, Count:=-1
.Delete
End With
Loop
End With
End Sub


(Helmut, I couldn't get your version to work. It didn't do anything. It's
like InStr wasn't recongnizing the 43110.
I used =rand(5,3) for some random text, then put 43110 at the beginning of
some of the sentences.)
 
H

Helmut Weber

Hi David,
If InStr(oPrg.Range.Text, "43110") = 1 Then

You were first talking about lines
and later about paragraphs,
so I thought, at a long shot,
it is paragraphs, what is in question.
Sentences is again something different
from lines and paragraphs.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

David Sisson

(I'm not the original author BTW.)

Sorry, I figured out my mistake.

I was reading
If InStr(oPrg.Range.Text, "43110") = 1 Then

But I was thinking
If InStr(oPrg.Range.Text, "43110") = True.

As in the Find logic, instead of, If search string is in position one.

(Slaps forehead.)

Thanks!
 
H

Helmut Weber

Hi David,
(I'm not the original author BTW.)

I'm seeing now, but with 36°C (100 Fahrenheit) here,
an excuse is ready at hand.
(Slaps forehead.)

Which I do quite often. :)
Me stupido, me know nothing, from "Name of the rose"

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

Access101

I think these are all good too:

If instr(val, "x") = True then
If instr(val, "x") = -1 then
If instr(val, "x") then
 
Y

yves

Hi there,

Would a simple and single Find-Replace be useful here?

We need to delete all paragraphs that begin with 43110. Search for

(^13)(43110)*(^13)

and replace that with

^13

Use Range/Selection.Find.Execute as usual; Reset the Find object
settings, have MatchWildCards.=True.

But perhaps I missed something.
Nevertheless, HTH!
Yves Champollion
 
H

Helmut Weber

Hi Yves,
(^13)(43110)*(^13)
and replace that with
^13
But perhaps I missed something.
Nevertheless, HTH!
Yves Champollion

you missed a lot,
1st: does not work on the first paragraph in the doc,
means, if the doc starts with "43110"
2nd: does not work on consecutive paragraphs,
which all start with "43110"
3rd: replaces paragraphs marks with chr(13),
which is not a paragraph mark!

Try:

43110¶
43110¶
43110¶
43110¶
43110¶
43110¶
43110¶

and

msgbox ActiveDocument.Range.Paragraphs.Count

afterwards.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
Y

yves

Good you jumped in to find the flaw. Yes indeed you are right. I was
offering guidelines in the form of a prompt - a question - to try and
take AccessNewbie to the right direction.

What surprised me at first was using such a process-intensive loop
while a global approach seemed more appropriate and programmatically
elegant.

A way to keep that same global approach is quite simply derived from
my guidelines. I'll let AccessNewbie work out the code:

1. Do the search-replace as indicated, but instead of replacing
with... ^13, replace with a token, of course.
2. Do a second pass, turning off MatchWildCard, replacing tokens with
a canonic ^p (paragraph mark).

.... and separately cater for a first line beginning with 43110, if
needs be (does it, AccessNewbie?). All of the above, once again, is a
guideline, not definite source code and the final code has to be
written and tested, which really is AccessNewbie's job.

A tip to Accessnewbie: after a large Find-Replace, do an
ActiveDocument.UndoClear to flush the Undo pile. Well, do that after
any large quantity of document changes to keep Word flying. VBAing
Word is quite different from Access.

I'm curious to know which approach you (Helmut) would privilege, when
faced with this sort of problem. I'm programming for industrial-
strength processes that push Word very far; I always go for global
actions that use low-level Word commands, rather than intensive VBA
loops...

Cheers,
Yves Champollion
 
H

Helmut Weber

Hi Yves,

sorry for the late reply.

If you want to or must avoid VBA,
1. Do the search-replace as indicated, but instead of replacing
with... ^13, replace with a token, of course.
2. Do a second pass, turning off MatchWildCard, replacing tokens with
a canonic ^p (paragraph mark).

IMHO, there is hardly a way around consecutive
searches and replaces from the user interface.
Nothing wrong with that, unless you got to process
thousands of documents, which I do as a daily fully automated routine,
and which works for about 6 weeks without failure.
But not longer, not even with a machine dedicated
to nothing but that job.

The documents, before they are processed,
are edited by humans, though, and I bet,
on a large scale, then every automation will fail in the end.
Not to speak of thunderstorms, a cleaning woman or man
tearing out cables, floods, short circuits by life bugs,
strikes and much more.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
Y

yves

Hi Yves,

sorry for the late reply.

If you want to or must avoid VBA,

Funny, I never said that. As I said, it's AccessNewbie's job is to
figure out the *code*, and I even offered advice on using
Selection.Find.
IMHO, there is hardly a way around consecutive
searches and replaces from the user interface.
Nothing wrong with that, unless you got to process
thousands of documents, which I do as a daily fully automated routine,

no need to play it big guy. This is not about Helmut, but about
getting AccessNewbie moving, which we're achieving together.
and which works for about 6 weeks without failure.
But not longer, not even with a machine dedicated
to nothing but that job.

The documents, before they are processed,
are edited by humans, though, and I bet,
on a large scale, then every automation will fail in the end.
Not to speak of thunderstorms, a cleaning woman or man
tearing out cables, floods, short circuits by life bugs,
strikes and much more.

That does not answer the real question I brought to you. I can do
without an answer because I have an opinion, but the answer could be
helping AccessNewbie.

One can loop each and every paragraph in a document to replace things,
or send Word two Range/Selection.Find.Executes and be done. Those are
very different programming approaches. The processor load is immensely
less in the latter method, because 1. we're not keeping the VBA engine
running and interpreting all the time and 2. we're not chopping the
document in paragraphs to be examined, most of which perhaps don't
need to be processed. In this case, IMHO, a global process is better
than a looped one but you are perfectly entitled to thinking
otherwise!

Yves Champollion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top