PDA

View Full Version : Convert document to end after finding required word



dagerr
01-29-2018, 12:42 PM
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

gmayor
01-30-2018, 01:53 AM
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

dagerr
01-30-2018, 03:29 AM
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.

gmayor
01-30-2018, 04:51 AM
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

dagerr
02-02-2018, 12:09 PM
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?

gmayor
02-02-2018, 09:55 PM
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