Find/Replace macro only limited to selection when entire paragraphis selected

P

Paul

To clarify, there is a Characters collection for the whole document, and it
includes the characters, paragraph marks, etc. (as range objects) in the
main body of the document. But there are also Characters collections for
selections, or for range objects.

I see. I'll look into it. Thanks again.
 
P

Paul

OK, with help from here on m.p.w.v.g and from Stack Overflow, I finally
got it working the way I want it to. Instead of stepping through the
selection/range chraracter-wise, I'm doing it word-wise. It's fast
enough and fits the bill quite nicely. Thanks once more for all your
help. Working vba code is below:

Sub applyNewRevisedText(control as IRibbonControl)
Dim r As Range ' Create a new Range object
Set r = Selection.Range ' Assign the current selection to the Range

Dim rng As Range

For Each rng In r.Words

Set rngStyle = rng.Style

Select Case rngStyle
Case "Superscript"
rng.Style = ActiveDocument.Styles("New/Revised Text Superscript")
Case "Subscript"
rng.Style = ActiveDocument.Styles("New/Revised Text Subscript")
Case "Emphasis"
rng.Style = ActiveDocument.Styles("New/Revised Text Emphasis")
Case "Bold"
rng.Style = ActiveDocument.Styles("New/Revised Text Bold")
Case "Italic"
rng.Style = ActiveDocument.Styles("New/Revised Text Italic")
Case "Bold Italic"
rng.Style = ActiveDocument.Styles("New/Revised Text Bold Italic")
Case "Underline"
rng.Style = ActiveDocument.Styles("New/Revised Text Underline")
Case Else
rng.Style = ActiveDocument.Styles("New/Revised Text")
End Select
Next rng
End Sub
 
S

Stefan Blom

Good thinking! Since it's unlikely that you ever want to apply direct
formatting to parts of a word, using the Words collection is more clever
than using Characters, and it should also save some time.

Why didn't I think of that? :)

Anyway, I'm glad you got it sorted, and thank you for the follow-up.

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Paul" wrote in message
OK, with help from here on m.p.w.v.g and from Stack Overflow, I finally
got it working the way I want it to. Instead of stepping through the
selection/range chraracter-wise, I'm doing it word-wise. It's fast
enough and fits the bill quite nicely. Thanks once more for all your
help. Working vba code is below:

Sub applyNewRevisedText(control as IRibbonControl)
Dim r As Range ' Create a new Range object
Set r = Selection.Range ' Assign the current selection to the Range

Dim rng As Range

For Each rng In r.Words

Set rngStyle = rng.Style

Select Case rngStyle
Case "Superscript"
rng.Style = ActiveDocument.Styles("New/Revised Text
Superscript")
Case "Subscript"
rng.Style = ActiveDocument.Styles("New/Revised Text Subscript")
Case "Emphasis"
rng.Style = ActiveDocument.Styles("New/Revised Text Emphasis")
Case "Bold"
rng.Style = ActiveDocument.Styles("New/Revised Text Bold")
Case "Italic"
rng.Style = ActiveDocument.Styles("New/Revised Text Italic")
Case "Bold Italic"
rng.Style = ActiveDocument.Styles("New/Revised Text Bold
Italic")
Case "Underline"
rng.Style = ActiveDocument.Styles("New/Revised Text Underline")
Case Else
rng.Style = ActiveDocument.Styles("New/Revised Text")
End Select
Next rng
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top