Consulting

Results 1 to 6 of 6

Thread: Continue macro if paragraph not exist

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

    Continue macro if paragraph not exist

    Hi,
    The following code change next paragraph if searched word is founded. I want modify code to continue macro if paragraph after finding searched word not exist or founded word is last in document.
    Thanks


    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
         Do While .Execute(FindText:="FOTO", MatchWholeWord:=True)
        oRng.End = oRng.Paragraphs(1).Range.End
        oRng.Collapse 
       
     'if praragraph not exist??
       If (oRng.End = oRng.Next.Paragraphs(1).Range.End) = False Then 
            
                Exit Sub
                Else
                End If
                
            
            oRng.End = oRng.Next.Paragraphs(1).Range.End
       
        If Len(oRng) = 1 Then
            oRng.Collapse 0
            oRng.End = oRng.Next.Paragraphs(1).Range.End
         
        End If
                 
                With oRng
                    .Font.Name = "Times New Roman"
                    .Font.Size = 8
                    .Font.Italic = wdToggle
                    .bold = False
                    With .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
                    .Collapse 0
                End With
            Loop
        End With
    lbl_Exit:
        Set oRng = Nothing

  2. #2
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Anyone helps?...

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Based on your location flag, I realize that English is not likely your first language. Speaking only English and not very well at that, I don't understand what you are trying to explain or achieve. Can you give an example of before\after?
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    ok, I will try explain it better.
    If text in document looks like this, Macro above works properly - situation 1:


    FOTO
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.



    but when text in document looks like that, it stop with error - situation 2:


    FOTO
    [no more text - end of document]



    So my goal is to continue macro to next part if there is a situation 2


    Thanks

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    I'm sorry, but that doesn't help much. In both cases

    Case 1 (the complete document text is):

    FOTO
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.


    Case 2 (the complete document text is):

    FOTO

    The macro performs exactly the same. It runs to the line Exit Sub and exits without error.

    If the "next part" is intended to format text following the instance of "FOTO", then how could you possibly continue and format something that isn't there? I have tried to deduce what you are trying to do:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 4/5/2018
    Dim oRng As Range
    Dim oRngFormat As Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        Do While .Execute(FindText:="FOTO", MatchWholeWord:=True)
          oRng.Collapse wdCollapseEnd
          On Error GoTo Err_Handler
          Set oRngFormat = oRng.Paragraphs(1).Next.Range
          With oRngFormat
            .Font.Name = "Times New Roman"
            .Font.Size = 8
            .Font.Italic = wdToggle
            .Bold = False
            With .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
            .Collapse 0
          End With
        Loop
      End With
    lbl_Exit:
      Set oRng = Nothing
      Exit Sub
    Err_Handler:
      Resume lbl_Exit
    End Sub
    When you collapse a range to the its start point then it will "NEVER" be equal to the end of the next paragraph (if i
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Solved, I've added Your code at the end of entire macro and it is OK

Posting Permissions

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