Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Won't exit out of loop after finding italic text 1

Status
Not open for further replies.

LCJ18786

Programmer
Nov 23, 2011
9
GB
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
 
Try:
Code:
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
With Rng
  With .Find
    .ClearFormatting
    .Format = True
    .Font.Bold = True
    .Forward = True
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found And .InRange(ActiveDocument.Bookmarks("EndText").Range)
    .Duplicate.Select
    MsgBox .Duplicate.Text & " " & "is bold"
    .Collapse Direction:=wdCollapseEnd
    .Find.Execute
  Loop
End With
 'Checks for italic occurances inbetween bookmarks
Set Rng = ActiveDocument.Bookmarks("EndText").Range
With Rng
  With .Find
    .ClearFormatting
    .Format = True
    .Font.Italic = True
    .Forward = True
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found And .InRange(ActiveDocument.Bookmarks("EndText").Range)
    .Duplicate.Select
    MsgBox .Duplicate.Text & " " & "is italic"
    .Collapse Direction:=wdCollapseEnd
    .Find.Execute
  Loop
End With

Cheers
Paul Edstein
[MS MVP - Word]
 
Try:
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
With Rng
With .Find
.ClearFormatting
.Format = True
.Font.Bold = True
.Forward = True
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found And .InRange(ActiveDocument.Bookmarks("EndText").Range)
.Duplicate.Select
MsgBox .Duplicate.Text & " " & "is bold"
.Collapse Direction:=wdCollapseEnd
.Find.Execute
Loop
End With
'Checks for italic occurances inbetween bookmarks
Set Rng = ActiveDocument.Bookmarks("EndText").Range
With Rng
With .Find
.ClearFormatting
.Format = True
.Font.Italic = True
.Forward = True
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found And .InRange(ActiveDocument.Bookmarks("EndText").Range)
.Duplicate.Select
MsgBox .Duplicate.Text & " " & "is italic"
.Collapse Direction:=wdCollapseEnd
.Find.Execute
Loop
End With

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

This worked beautifully. Thank you so much. Just for my learning purposes, could you please explain why you choose to use ".Duplicate.Select" and ".Find.Found "?

Im still unsure why my method did not initially work??

Louis
 
Hi Louis,

Amongst other things, .Duplicate often provides a significant speed increas. As well, it allows you to change the starting or ending character position of the duplicate range without changing the original range. In all likelihood, it's not having much effect in this particular scenario.

I'm not sure what needs explaining about the Do While .Find.Found ... Loop. You'll note that I have a .Execute statement before it, which sets up the .Found condition as either True or False, then there's another .Find.Execute within the loop to refresh the .Found state.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top