Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Word 2003, macro to find txt, delete specific part of line, contin

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tailings - 04 Oct 2006 22:42 GMT
One more thing I am having problems with - I need to find some specific text
- space, period, space, space, PAC. This is the only common item to all the
lines that need to be changed, and always occupies the same position. Once
this is found, I need to go to the beginning of the line (all lines are
actually paragraphs) and delete up to and including the space before PAC.

I need the macro to continue thru the entire document.

I recorded the following:

Sub RemoveBeginningOfLine

   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = ".  PAC"
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute
   Selection.MoveLeft Unit:=wdWord, Count:=1
   Selection.MoveRight Unit:=wdCharacter, Count:=3
   Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
   Selection.Delete Unit:=wdCharacter, Count:=1
End Sub
How do I make it continue?
Tony Strazzeri - 05 Oct 2006 00:49 GMT
you create a loop from after the execute statement using the
Selection.Find.Found property to test if the find has succeded.

The following should work, but I advise stepping through to ensure it
works as I have not tested it.

Cheers
TonyS.

Sub RemoveBeginningOfLine()

   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = ".  PAC"
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute
   Do While Selection.Find.Found
       Selection.MoveLeft Unit:=wdWord, Count:=1
       Selection.MoveRight Unit:=wdCharacter, Count:=3
       Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
       Selection.Delete Unit:=wdCharacter, Count:=1

       Selection.Find.Execute
   Loop

End Sub

> One more thing I am having problems with - I need to find some specific text
> - space, period, space, space, PAC. This is the only common item to all the
[quoted text clipped - 28 lines]
> End Sub
>  How do I make it continue?
Tailings - 05 Oct 2006 01:03 GMT
Worked like a charm! I had found a workaround, but your solution is FAR more
elegant! Thanks SO much!

> you create a loop from after the execute statement using the
> Selection.Find.Found property to test if the find has succeded.
[quoted text clipped - 64 lines]
> > End Sub
> >  How do I make it continue?
Helmut Weber - 05 Oct 2006 01:34 GMT
Hi,

so, if a paragraph starts with " .  PAC"
you want to remove " .  " from that paragraphs start?

Note, that you talk about
>- space, period, space, space, PAC
while searching for period, space, space, PAC!

The information, that the search string is always
at the same position isn't very helpful,
as one would have to find out the position first.
It could help to speed up the macro, in theory,
as then no searching would be necessary at all.

Also:
>This is the only common item to all the
>lines that need to be changed,

tells, that all lines to be processed, contain " .  PAC",
not that all lines containing " .  PAC"
have to be processed. ;-)

However, I don't think you need more than this,
which does not check whether " .  PAC" is at the
paragraph's start.

With ActiveDocument.Range.Find
  .Text = " .  PAC" ' leading space!
  .Replacement.Text = "PAC"
  .Execute Replace:=wdReplaceAll
End With

Still a bit a stab in the dark.

You may use the selection object, too,
search for "^p .  PAC", and replace it with "^pPAC".

But as this would't work for the first paragraph,
I don't like it.

HTH

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Tailings - 05 Oct 2006 02:31 GMT
Sorry I wasn't very clear - or precise! I have been battling with this
project for several days and even dreamed I had the solution. How annoying to
wake up and find it still didn't work.

I am cutting and pasting text from a very primitive database and putting it
in what is supposed to be a relatively sophisticated-looking report. Right
now we go through and delete all the unneeded info manually, which take a
very long time. The macro work is speeding it up.

Thanks to all for the help!

> Hi,
>
[quoted text clipped - 38 lines]
>
> HTH
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.