Consulting

Results 1 to 6 of 6

Thread: Convert document to end after finding required word

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location

    Convert document to end after finding required word

    Hi again,
    I want to convert document from founded word, in this case: "FOTO:" to end of document or end of paragraph. It depends where will be "^p". Right after "FOTO:" or at end next paragraph.
    Here is my poorly code:

    Sub convert_paragraph_after_finding()
    
    
    
    
    Dim oPara As Paragraph
        For Each oPara In ActiveDocument.Paragraphs
            If oPara.Range.Words = "FOTO:" Then
                  .Font.Name = "Times New Roman"
                 .Font.Size = 8
        With Selection.ParagraphFormat
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 10
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceMultiple
            .LineSpacing = LinesToPoints(0.9)
            .Alignment = wdAlignParagraphLeft
            .WidowControl = True
            .KeepWithNext = False
            .KeepTogether = False
            .PageBreakBefore = False
            .NoLineNumber = False
            .Hyphenation = True
            .FirstLineIndent = CentimetersToPoints(0)
            .OutlineLevel = wdOutlineLevelBodyText
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            .CharacterUnitFirstLineIndent = 0
            .LineUnitBefore = 0
            .LineUnitAfter = 0
            .MirrorIndents = False
            .TextboxTightWrap = wdTightNone
        End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
            End If
        Next oPara
        
    End Sub
    Thanks

  2. #2
    Can we assume that you want the paragraph after the paragraph containing the word to be formatted?
    It would make more sense to create a style and apply it, however
    Sub convert_paragraph_after_finding()
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            'Find the word
            Do While .Execute(FindText:="FOTO:", MatchWholeWord:=True)
                'move the end of the range to the end of the paragraph containing the found word
                oRng.End = oRng.Paragraphs(1).Range.End
                'collapse the range to its end
                oRng.Collapse 0
                'move the end of the range to the end of the following paragraph
                oRng.End = oRng.Next.Paragraphs(1).Range.End
                'format the range
                With oRng
                    .Font.Name = "Times New Roman"
                    .Font.Size = 8
                    With .ParagraphFormat
                        .LeftIndent = CentimetersToPoints(0)
                        .RightIndent = CentimetersToPoints(0)
                        .SpaceBefore = 0
                        .SpaceBeforeAuto = False
                        .SpaceAfter = 10
                        .SpaceAfterAuto = False
                        .LineSpacingRule = wdLineSpaceMultiple
                        .LineSpacing = LinesToPoints(0.9)
    
                        'what alignment do you want. You had both?
                        '.Alignment = wdAlignParagraphLeft
                        .Alignment = wdAlignParagraphJustify
                        '
                        .WidowControl = True
                        .KeepWithNext = False
                        .KeepTogether = False
                        .PageBreakBefore = False
                        .NoLineNumber = False
                        .Hyphenation = True
                        .FirstLineIndent = CentimetersToPoints(0)
                        .OutlineLevel = wdOutlineLevelBodyText
                        .CharacterUnitLeftIndent = 0
                        .CharacterUnitRightIndent = 0
                        .CharacterUnitFirstLineIndent = 0
                        .LineUnitBefore = 0
                        .LineUnitAfter = 0
                        .MirrorIndents = False
                        .TextboxTightWrap = wdTightNone
                    End With
                End With
                'and stop looking
                Exit Do
            Loop
        End With
    lbl_Exit:
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Thanks for answer,
    I receive files with that kind formatting where searched word "FOTO:" is in the same paragraph or after "FOTO:"^p is a next paragraph as You designed, or it is empty paragraph and after it paragraph wich should be formatted.

  4. #4
    A properly formatted document should not have empty paragraphs. It should have paragraph spacing to create the space - however, if the following paragraph might be empty, you need to test for that and choose the next paragraph instead e.g.

    Do While .Execute(FindText:="FOTO:", MatchWholeWord:=True)
                'move the end of the range to the end of the paragraph containing the found word
                oRng.End = oRng.Paragraphs(1).Range.End
                'collapse the range to its end
                oRng.Collapse 0
                'move the end of the range to the end of the following paragraph
                oRng.End = oRng.Next.Paragraphs(1).Range.End
                'If the paragraph is empty
                If Len(oRng) = 1 Then
                    oRng.Collapse 0
                    'move the end of the range to the end of the following paragraph
                    oRng.End = oRng.Next.Paragraphs(1).Range.End
                End If
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Many thanks, especially for a comments in code, it really helps to understand it.
    The solution is simple with giving end of paragraph sign after "FOTO:", just 'find and repleace' command and then I can put Your code. So my problem seems to be solved.
    But for me interesting is: how to after finding searched word could we set a range from next paragraph to end of document. Is it possible?
    Last edited by dagerr; 02-02-2018 at 12:50 PM.

  6. #6
    It depends where you are starting from. The following will select from the start of the paragraph following the start of the current oRng to the end of the document, assuming there is a paragraph after the start of the current paragraph.
        oRng.End = ActiveDocument.Range.End
        If oRng.Paragraphs.Count > 1 Then
            oRng.Start = oRng.Paragraphs(2).Range.Start
            oRng.Select 'for testing only
        Else
            MsgBox "There are no more paragraphs!"
        End If
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •