PDA

View Full Version : [SOLVED:] Continue macro if paragraph not exist



dagerr
03-23-2018, 01:22 PM
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

dagerr
04-04-2018, 02:28 AM
Anyone helps?...

gmaxey
04-04-2018, 02:33 PM
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?

dagerr
04-05-2018, 12:42 AM
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

gmaxey
04-05-2018, 05:10 AM
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

dagerr
04-20-2018, 04:35 AM
Solved, I've added Your code at the end of entire macro and it is OK