×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

conditionally manipulate text in each paragraph

conditionally manipulate text in each paragraph

conditionally manipulate text in each paragraph

(OP)
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

RE: conditionally manipulate text in each paragraph

Start with a clear, complete, logical description of what you need.

RE: conditionally manipulate text in each paragraph

Quote:

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
   ‘par.Range.Text Is the text for this paragraph
   Debug.Print par.Range.Text
Next 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

RE: conditionally manipulate text in each paragraph

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]

RE: conditionally manipulate text in each paragraph

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.

RE: conditionally manipulate text in each paragraph

>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

RE: conditionally manipulate text in each paragraph

(OP)
@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

RE: conditionally manipulate text in each paragraph

Quote (Andrzejek)

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]

RE: conditionally manipulate text in each paragraph

It is a VBA forum. 😉

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

RE: conditionally manipulate text in each paragraph

(OP)
Just in case it is interesting to anyone, this is the solution I went with. The code is a mess big smile 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 --> VBA

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 smile 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

RE: conditionally manipulate text in each paragraph

By this line of code:
If Mid(xString, (Len(xString)) - 1, 1) = ")" Then

did you mean this:
If Right(xString, 1) = ")" Then
ponder

smile


---- Andy

There is a great need for a sarcasm font.

RE: conditionally manipulate text in each paragraph

(OP)
@Andrzejek
/By this line of code:
/If Mid(xString, (Len(xString)) - 1, 1) = ")" Then
/did you mean this:
/If Right(xString, 1) = ")" Then ponder

big smile and you haven't seen the rest of the code big smile

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

RE: conditionally manipulate text in each paragraph

>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

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
 

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

    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 

You could do similar for Excerpt 2

RE: conditionally manipulate text in each paragraph

(OP)
@strongm (MIS)
That was very pretty smile
Thank you!
It took me a while to figure out what you are doing there. If anybody needs it, here is a very nice document about using wildcards in search: https://wordmvp.com/FAQs/General/UsingWildcards.ht...

Best regards :)
P

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close