Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-28-2012, 06:30 PM
marceepoo marceepoo is offline Get para no where txt is found, and move text to end of paragraph using Word 201 Windows 7 64bit Get para no where txt is found, and move text to end of paragraph using Word 201 Office 2010 64bit
Novice
Get para no where txt is found, and move text to end of paragraph using Word 201
 
Join Date: Sep 2012
Posts: 22
marceepoo is on a distinguished road
Default Get para no where txt is found, and move text to end of paragraph using Word 201

I am trying to use VBA to move a rich text clause ("strText"), which appears at the beginning of various paragraphs, to the end of each paragraph where the clause appears, and thereafter to underline strText.

I am a novice/hobbyist at vba programming, so please be gentle. I spent a few days on this before seeking help.



Problems with my attempted coding (which appears below):

I tried to assign to var "LparaNo" the number of the paragraph wherein the found text (strText) appears. But the number that "LparaNo" returns is totally off base. If someone has a suggestion about how to get the right paragraph number, I'd appreciate it.

My intention is to set a range variable objRange_ParaHoldingText= ActiveDocument.Paragraphs(LparaNo).Range, i.e., a range that would reflect the paragraph in which the sought text was found.

I can't figure out how to move objRange01 ("strText", which is formatted text) to the end of the paragraph in which it appears.

Any suggestions would be much appreciated.

Thanks, Marc


Code:
Sub subFindTextAndMoveItToEndOfTheSameParagraphAndUnderlineIt_03()

    ' Code canniablized from http://stackoverflow.com/questions/11733766/how-to-search-for-text-and-check-for-underline-in-vba-for-word
    Dim c As Range
    Dim fnd As String
    Dim strText As String
    Dim objRange01 As Range
    Dim objRange02 As Range
    Dim objRange03 As Range
    Dim LparaNo As Long
    Dim strParazText As String


    With ActiveDocument

        strText = "Falsification  45 C.F.R. �" & Chr(160) & "6891(a)(2):  "

        ' My objectives are: (1) to move strText from the beginning of various paragraphs, to the end of each paragraph where it appears,
        '    and thereafter, (2) to delete the ":" at the end of strText, and (3) to underline strText

        fnd = strText

        If fnd = "" Then Exit Sub

        Set c = ActiveDocument.Content

        c.Find.ClearFormatting
        c.Find.Replacement.ClearFormatting

        With c.Find
            .Text = fnd
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
        End With

        c.Find.Execute

        While c.Find.Found
            c.Select ' I am trying to select the text that was found

            Set objRange01 = c ' I am trying to set objRange01 = the text that was found, and selected
            Selection.EndOf Unit:=wdParagraph, Extend:=wdExtend ' I am extending the selection to include the entire paragraph
            Set objRange02 = Selection.Range 'The entire paragraph
            Set objRange03 = ActiveDocument.Range(Start:=0, End:=Selection.End) ' I am trying to set objRange02 = all text from
            '                                                                     '   beginning of doc thru objRange01.text
            LparaNo = objRange03.ComputeStatistics(wdStatisticParagraphs) + 1 ' I am trying to set LparaNo = the no. of paras in all
            '                                                                 '    text from beginning of doc thru the end of objRange02.
            '                  ' Alas, the number generated for "LparaNo" is incorrect. The paragraph number generated for "LparaNo"
            '                  '    is the number for a paragraph that appears 5 pages before objRange01.text

            MsgBox "Paragraph # " & LparaNo & "  [objRange01.Text = c = ]  " & Chr(34) & objRange01.Text & Chr(34) & vbCrLf & _
                    vbCrLf & objRange02.Text & vbCrLf & vbCrLf & _
                    ActiveDocument.Paragraphs(LparaNo - 2).Range.Text & vbCrLf & _
                    ActiveDocument.Paragraphs(LparaNo - 1).Range.Text & vbCrLf & _
                    ActiveDocument.Paragraphs(LparaNo).Range.Text & vbCrLf ' & _
'                    ActiveDocument.Paragraphs(LparaNo + 1).Text & vbCrLf & _
'                    ActiveDocument.Paragraphs(LparaNo + 2).Range.Text & vbCrLf '& _

            objRange01.Move Unit:=wdParagraph, Count:=1 ' I am trying unsuccessfully to move the selected text to the beginning
            '                                            '   of the next paragraph
            objRange01.Move Unit:=wdCharacter, Count:=-1 ' I am trying unsuccessfully to move the selected text from the beginning
            '                                            '   of the next paragraph, to the end of the preceding paragraph, i.e.,
            '                                            '   to the end of the selected text's paragraph of origin.
            c.Find.Execute

        Wend ' While c.Find.Found

    End With

End Sub 'subFindTextAndMoveItToEndOfTheSameParagraphAndUnderlineIt_03
Reply With Quote
  #2  
Old 11-28-2012, 09:04 PM
gmaxey gmaxey is offline Get para no where txt is found, and move text to end of paragraph using Word 201 Windows 7 32bit Get para no where txt is found, and move text to end of paragraph using Word 201 Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Something like this perhaps:

Code:
Sub subFindTextAndMoveItToEndOfTheSameParagraphAndUnderlineIt()
Dim oRng As Range
Dim oRngParent As Range
Dim strText As String
  strText = "Falsification  45 C.F.R. ?" & Chr(160) & "6891(a)(2):  "
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .ClearFormatting
    .Text = strText
    .Forward = True
    .Wrap = wdFindStop
    While .Execute
      With oRng
        .Select 'Why do you need to select it?
        .Text = Trim(.Text)
        .Font.Underline = wdUnderlineSingle
        Set oRngParent = .Paragraphs(1).Range
        With oRngParent
          .Characters.Last.Previous.InsertAfter " "
          .MoveEnd wdCharacter, -1
          .Collapse wdCollapseEnd
        End With
        .Cut
        With oRngParent
          .Paste
          .Characters.Last.Delete
          .Start = oRngParent.Paragraphs(1).Range.Start
          .Characters.First.Case = wdUpperCase
        End With
        .Start = oRngParent.End
      End With
    Wend 
  End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 12-20-2012, 11:42 AM
marceepoo marceepoo is offline Get para no where txt is found, and move text to end of paragraph using Word 201 Windows 7 64bit Get para no where txt is found, and move text to end of paragraph using Word 201 Office 2010 64bit
Novice
Get para no where txt is found, and move text to end of paragraph using Word 201
 
Join Date: Sep 2012
Posts: 22
marceepoo is on a distinguished road
Default

Dear Greg:

Thank you so much.
Still working on understanding how it works.
Much appreciated.

Marc
Reply With Quote
Reply

Tags
find, move, range



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get para no where txt is found, and move text to end of paragraph using Word 201 Word 2007: Insert Para after Table rphox2003 Word 10 03-06-2013 11:13 PM
Get para no where txt is found, and move text to end of paragraph using Word 201 Generate TOC Without Paragraph Text sbianco Word 8 09-22-2012 06:54 AM
multiple text boxes collapse on move apricoti Word 2 01-26-2012 05:55 PM
templates 2003 can't be found in 2007 word tintincute Word 1 12-23-2009 08:55 PM
I want to change the Move Graphics with text default. Renee Hendershott Office 0 01-22-2006 03:43 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:04 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft