Re: replace all ^p 's unless followed by a date



I figured it out. I created a macro with the code "msgbox asc(selection)" to
find the actual ascii character being used. I found that the ^p's were
actually ascii char(10)'s. So I did a find/replace ^10 with ^13. Then ran the
find/replace that Jay supplied. It worked great.

Thanks again!

"Brent Whaling" wrote:

This worked as long as I took out the ^13. With the ^13 it never found an
instance. I was using the the find/replace without the ^13 but now (as would
be expected) I have examples where it is now inserting ^P's where there
shouldn't be.

Any ideas?

I really appreciate your post, it got me on the right track.

"Jay Freedman" wrote:

Word's Find/Replace isn't really set up to do exceptions. You could program
a macro that does a Find for each ^p and then tries to figure out whether
the text that follows it matches the rest of your pattern, but that would be
horribly inefficient and slow. A better method is to do three "Replace All"
steps:

1. Use a wildcard search (that is, include .MatchWildcards = True in
the settings of the Find object; see
http://www.gmayor.com/replace_using_wildcards.htm) for
^13([0-9][0-9]-[A-Z]{3}-20[0-9][0-9])
and replace with
~~\1
so each date that was preceded by a paragraph mark is now
preceded by two tildes (the assumption being that two tildes
won't appear anywhere else in the document).

2. Search (without wildcards) for
^p
and replace with nothing (that is, .Replacement.Text = "").

3. Search for
~~
and replace with
^p

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Brent Whaling wrote:
I am using word to prepare a delimited file to be imported into Excel
and am having trouble getting rid of extra ^p characters.

Basically, starting with the second line of the file I need to remove
all ^p unless they are immediately followed by the date (ex.
04-NOV-2008). I've figured out the pattern mask
(^p^#^#-^$^$^$-20^#^#).

I was hoping to use "Find and Replace" code but I'm wondering how to
create the above exception. Maybe a properly place "if" statement or
perhaps there is a property of "Find" that I'm not aware of.

Any help would be much appreciated.

Until now I've been doing it this way but I keep finding more and
more ^p characters and I would like to implement a more elegant
solution.


Dim wdApp As Word.Application, wdDoc As Word.Document

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

Set wdDoc = wdApp.Documents.Open("C:\temp\esi_monitor.dat")

wdApp.Visible = True

With wdDoc.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ".^p.^p"
.Replacement.Text = ".."
.Execute Replace:=wdReplaceAll
End With
With wdDoc.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p)"
.Replacement.Text = ")"
.Execute Replace:=wdReplaceAll
End With
With wdDoc.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "D^p@"
.Replacement.Text = "D@"
.Execute Replace:=wdReplaceAll
End With
With wdDoc.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\@|"
.Replacement.Text = "\&|"
.Execute Replace:=wdReplaceAll
End With

wdDoc.SaveAs "C:\temp\esi_monitor.dat"
wdDoc.Close
wdApp.Quit



.



Relevant Pages

  • Re: Address can not be more than 30 characters
    ... Its a macro. ... Put in a module and execute from there. ... I am sending this report to a thrid party who can not have the ... > address field be more than 30 characters. ...
    (microsoft.public.excel.worksheet.functions)
  • replace all ^p s unless followed by a date
    ... I am using word to prepare a delimited file to be imported into Excel and am ... characters and I would like to implement a more elegant solution. ... Set wdApp = CreateObject ... .Execute Replace:=wdReplaceAll ...
    (microsoft.public.word.vba.general)
  • Re: replace all ^p s unless followed by a date
    ... and am having trouble getting rid of extra ^p characters. ... Set wdApp = CreateObject ... On Error GoTo 0 ... .Execute Replace:=wdReplaceAll ...
    (microsoft.public.word.vba.general)
  • macro with font size
    ... I'm trying to creat a macro which changes the font size and the font type ... and the color of the characters, however when I execute the macro only the ...
    (microsoft.public.word.general)
  • Re: Word Document Shows Unreadable Text
    ... Graham Mayor - Word MVP ... flash drives and say it is not a problem, then all of a sudden they ... As the macro did not work, we can assume that the problem was not ... these Asian-type characters at the top. ...
    (microsoft.public.word.docmanagement)