Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conditionally manipulate text in each paragraph

Status
Not open for further replies.

565u

Technical User
Apr 25, 2005
46
0
0
CZ
Hi and thanks for reading my post!
I have a plain text file. I need to go thru each paragraph and check it for specific character or several characters and then manipulate them or text near them. I just can't figure out the methodology to do that :( Everything I try fails me at some point. My problem is not how to manipulate the text, but how to be able to do so limited to a particular paragraph. (for example search every paragraph for "abc", then find "xyz" after the "abc" and if it exists, add a current date after "x") If I were to do this limited to the whole document, there would be no problem. I just don't know how to limit myself to one paragraph.
Any help, pls?
Many thanks in advance!!
:)
P
 
Start with a clear, complete, logical description of what you need.
 
My problem is not how to manipulate the text, but how to be able to do so limited to a particular paragraph.

It appears that you need to “manipulate the text” for each paragraph in the Word document.
Code:
Dim par As Paragraph

For Each par in ActiveDocument.Paragraphs
   ‘[b]par.Range.Text[/b] Is the text for this paragraph
   Debug.Print [b]par.Range.Text[/b]
Next

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Assuming the "abc" & "xyz" string exist together in the same paragraph and you want to replace just the first instance, that's as simple as:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "(abc[!^13]@x)(yz)"
  .Replacement.Text = "\1 " & Format(Now, "DD-MMM-YYYY") & " \2"
  .Forward = True
  .Wrap = wdFindStop
  .Format = False
  .MatchWildcards = True
  .Execute Replace:=wdReplaceOne
End With
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
I read the OP a little different. Just because this post is in VBA forum, doesn’t mean 565u is dealing with Word.

“I have a plain text file.” In my book that’s just a simple text file that can be opened in Notepad, like SomeFile.txt

“I need to go thru each paragraph” which means this text needs to be split into separate paragraphs, which is split by either Chr(10) & Chr(13) or vbNewLine

“My problem is not how to manipulate the text, but how to be able to do so limited to a particular paragraph” since the text is split into paragraphs, you can deal with each individual paragraph and accomplish what you need.

“Everything I try fails me at some point.” It would be nice to see what have tried. This way we could get an idea of what you are trying to accomplish.



---- Andy

There is a great need for a sarcasm font.
 
>If I were to do this limited to the whole document, there would be no problem

So … all you need to do is be able to treat each paragraph as if it was a whole document …

Skip gives one method, stepping through the paragraphs collection of a Word document one paragraph as a time.

And Andy hints at another, splitting a plain text file up at a paragraph break, in VBA that typically being vbCRLF (so you could read the entire file into a string then Split it at vbCRLF into an array, where each item in the array is a single paragraph which you can treat as if it were an entire document
 
@SkipVought
@macropod
Thank you very much for your kind replies! They got me started and now I am sucessfully progressing in solving this. I will post my working solution once it's done.


@Andrzejek
Thank you for your kind reply!
re: I read the OP a little different. Just because this post is in VBA forum, doesn’t mean 565u is dealing with Word.

My blatant mistake. Appologies. Should have worded the Q better. It is Word (XP). I have not done anything in VBA for the past three years and forgot how solutions differ between word and excel.

re: “I have a plain text file.” In my book that’s just a simple text file that can be opened in Notepad, like SomeFile.txt
re: “I need to go thru each paragraph” which means this text needs to be split into separate paragraphs, which is split by either Chr(10) & Chr(13) or vbNewLine
re: “My problem is not how to manipulate the text, but how to be able to do so limited to a particular paragraph” since the text is split into paragraphs, you can deal with each individual paragraph and accomplish what you need.
re: “Everything I try fails me at some point.” It would be nice to see what have tried. This way we could get an idea of what you are trying to accomplish.

You understand my original issue very well. I will post my solution (which will make the source of the issue more transparent) soon.


@strongm (MIS)
Thank you for your kind reply!


:)
P
 
Andrzejek said:
Just because this post is in VBA forum, doesn’t mean 565u is dealing with Word.
True, but any text file can be opened in Word and processed there.

Cheers
Paul Edstein
[MS MVP - Word]
 
It is a VBA forum. 😉

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Just in case it is interesting to anyone, this is the solution I went with. The code is a mess :-D but it works and that's all I need right now.
I needed to turn a txt into a tab separated text that can be imported into excel to create a spreadsheet.
A lot of weird text manipulation was needed.

Basically...

variable xP is a counter. it holds the number of the paragraph i work on. the number of paragraphs changes as i progress thru the text. i use
ActiveDocument.Paragraphs.Count
to check the current number when needed

Code:
EXCERPT 1:

If xP >= ActiveDocument.Paragraphs.Count Then GoTo ENDOFSUB

Set xRange = ActiveDocument.Paragraphs(xP).Range ' USING THIS WAS THE KEY TO MY UNDERSTANDING HOW TO MANIPULATE SPECIFIC PARAGRAPH ONLY
xString = xRange ' HERE I USED STRING TO DO SOME OF THE TASKS
    If Mid(xString, (Len(xString)) - 1, 1) = ")" Then
        For x = (Len(xString)) - 1 To 2 Step -1
            If x < 2 Then MsgBox "error"
            If Mid(xString, x - 1, 2) = " (" Then
                With xRange.Find
                    .ClearFormatting
                    .Text = ")"
                    .Replacement.ClearFormatting
                    .Replacement.Text = ""
                    .Forward = False
                    .Wrap = wdFindStop
                    .Format = False
                    .MatchCase = False
                    .MatchWholeWord = False
                    .MatchWildcards = False
                    .MatchSoundsLike = False
                    .MatchAllWordForms = False
                    .Execute Replace:=wdReplaceOne
                End With
                With xRange.Find
                    .ClearFormatting
                    .Text = " ("
                    .Replacement.ClearFormatting
                    .Replacement.Text = "^t"
                    .Forward = False
                    .Wrap = wdFindStop
                    .Format = False
                    .MatchCase = False
                    .MatchWholeWord = False
                    .MatchWildcards = False
                    .MatchSoundsLike = False
                    .MatchAllWordForms = False
                    .Execute Replace:=wdReplaceOne
                End With
                GoTo HIGHLIGHTAPOD
            End If
        Next x
    End If



EXCERPT 2: VERY EASY TO REPLACE SOME CHARACTERS...

xP = xP + 1
If xP >= ActiveDocument.Paragraphs.Count Then GoTo ENDOFSUB

Set xRange = ActiveDocument.Paragraphs(xP).Range
                
                With xRange.Find
                    .ClearFormatting
                    .Text = "  "
                    .Replacement.ClearFormatting
                    .Replacement.Text = " "
                    .Forward = True
                    .Wrap = wdFindStop
                    .Format = False
                    .MatchCase = False
                    .MatchWholeWord = False
                    .MatchWildcards = False
                    .MatchSoundsLike = False
                    .MatchAllWordForms = False
                    .Execute Replace:=wdReplaceAll
                End With
    
Set xRange = ActiveDocument.Paragraphs(xP).Range ' I HAD TO REFRESH THE RANGE AFTER SOME MANIPULATION
    
    If Mid(xString, 1, 12) = "- Highlight " Then
                With xRange.Find
                    .ClearFormatting
                    .Text = "- Highlight "
                    .Replacement.ClearFormatting
                    .Replacement.Text = "H^t"
                    .Forward = False
                    .Wrap = wdFindStop
                    .Format = False
                    .MatchCase = False
                    .MatchWholeWord = False
                    .MatchWildcards = False
                    .MatchSoundsLike = False
                    .MatchAllWordForms = False
                    .Execute Replace:=wdReplaceOne
                End With
    GoTo POKR1
    End If

EXCERPTS END.

So that's basically it :) I am very glad that despite being rusty at VBA, I have now sorted 153 pages of automated notes into a workable spreadsheet. Many thanks to everyone who was so kind to help!!

Best regards,
P
 
By this line of code:[tt]
If Mid(xString, (Len(xString)) - 1, 1) = ")" Then[/tt]
did you mean this: [tt]
If Right(xString, 1) = ")" Then[/tt] [ponder]

:)


---- Andy

There is a great need for a sarcasm font.
 
@Andrzejek
/By this line of code:
/If Mid(xString, (Len(xString)) - 1, 1) = ")" Then
/did you mean this:
/If Right(xString, 1) = ")" Then ponder

:-D and you haven't seen the rest of the code :-D

seriously, actually not. the rightmost character was the paragraph mark. also i was moving from the right to the left looking first for ) then for (

:)
P
 
>did you mean this:
>If Right(xString, 1) = ")"

Yeah, I initially thought that as well, then realised what was actually being done.

>also i was moving from the right to the left looking first for ) then for (

Just use Word's wild card search and replace (a cut-down regular expressions capability). Then you can get rid of all that silly xString stuff, and reduce your code, eg your entire Excerpt 1 can be replaced with (although you may have to think hard about the GoTo HIGHLIGHTAPOD):

Code:
[blue]If xP >= ActiveDocument.Paragraphs.Count Then GoTo ENDOFSUB
Set xRange = ActiveDocument.Paragraphs(xP).Range

    With xRange.Find
        .Text = "( [\(])(*)([\)])(^13)"
        .Replacement.Text = "^t\2\4"
        .Forward = True
        .MatchWildcards = True
        .Execute Replace:=wdReplaceOne
        If .Found Then GoTo HIGHLIGHTAPOD
    End With
[/blue]

But we can do better than this - since we are now including the paragraph break in the search function and are using regexp to generically replace your string search, we can apply this across the whole text file in one go. So the code becomes:

Code:
[blue]    With ActiveDocument.Range.Find
        .Text = "( [\(])(*)([\)])(^13)"
        .Replacement.Text = "^t\2\4"
        .Forward = True
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
        If .Found Then GoTo HIGHLIGHTAPOD
    End With[/blue]

You could do similar for Excerpt 2

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top