I have a document which selects the range between two bookmarks and then looks for any formatting e.g. bold, italic, bold italic, underline, keep with next, bullets and num etc. The reason for this is I am copying text (unformatted text) to a new document and then reapplying all the formatting.
So far I have got the first part working for finding any bold text, but then my loop suddenly gets stuck looking for any italic text and it will not exit out after finding all italic text. I thought it maybe something to do with resetting the bookmark range in the document but that has not helped.
Any help on how to solve this would be very much appreciated.
Thanks
Louis
PS. I have posted some of my code below -
'Italics
'This is where my loop is getting stuck
Do While .Execute And rng.InRange(ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End))
rng.Select
MsgBox rng & " " & "is italic"
rng.Collapse Direction:=wdCollapseEnd
Loop
------------------------------------------------------------------------------
Application.DisplayAlerts = False
With ActiveDocument
Set rng = ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End)
'Set text in bookmark to variable copyBody
rng.text = copyBody
ActiveDocument.Bookmarks.Add "EndText", rng
End With
'Additional formatting text to be transferred to the new document
'bold, italic, bold italic, underline, font size, keep with next, bullets and num
'Checks for bold occurances inbetween bookmarks
ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End).Select
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
With rng.Find
.Format = True
.Font.Bold = True
.Forward = True
.Wrap = wdFindAsk
Do While .Execute And rng.InRange(ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End))
rng.Select
MsgBox rng & " " & "is bold"
rng.Collapse Direction:=wdCollapseEnd
Loop
End With
'Checks for italic occurances inbetween bookmarks
Set rng = ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End)
ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End).Select
Selection.Find.ClearFormatting
Selection.Find.Font.Italic = True
With rng.Find
.Format = True
.Font.Italic = True
.Forward = True
.Wrap = wdFindAsk
Do While .Execute And rng.InRange(ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End))
rng.Select
MsgBox rng & " " & "is italic"
rng.Collapse Direction:=wdCollapseEnd
Loop
End With
So far I have got the first part working for finding any bold text, but then my loop suddenly gets stuck looking for any italic text and it will not exit out after finding all italic text. I thought it maybe something to do with resetting the bookmark range in the document but that has not helped.
Any help on how to solve this would be very much appreciated.
Thanks
Louis
PS. I have posted some of my code below -
'Italics
'This is where my loop is getting stuck
Do While .Execute And rng.InRange(ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End))
rng.Select
MsgBox rng & " " & "is italic"
rng.Collapse Direction:=wdCollapseEnd
Loop
------------------------------------------------------------------------------
Application.DisplayAlerts = False
With ActiveDocument
Set rng = ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End)
'Set text in bookmark to variable copyBody
rng.text = copyBody
ActiveDocument.Bookmarks.Add "EndText", rng
End With
'Additional formatting text to be transferred to the new document
'bold, italic, bold italic, underline, font size, keep with next, bullets and num
'Checks for bold occurances inbetween bookmarks
ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End).Select
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
With rng.Find
.Format = True
.Font.Bold = True
.Forward = True
.Wrap = wdFindAsk
Do While .Execute And rng.InRange(ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End))
rng.Select
MsgBox rng & " " & "is bold"
rng.Collapse Direction:=wdCollapseEnd
Loop
End With
'Checks for italic occurances inbetween bookmarks
Set rng = ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End)
ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End).Select
Selection.Find.ClearFormatting
Selection.Find.Font.Italic = True
With rng.Find
.Format = True
.Font.Italic = True
.Forward = True
.Wrap = wdFindAsk
Do While .Execute And rng.InRange(ActiveDocument.Range(ActiveDocument.Bookmarks("Text").Range.Start, _
ActiveDocument.Bookmarks("EndText").Range.End))
rng.Select
MsgBox rng & " " & "is italic"
rng.Collapse Direction:=wdCollapseEnd
Loop
End With