PDA

View Full Version : Solved: Replace "Emphasis"/"Strong" paragraph styles with good 'ol fashion bold/italics?



rgmatthes
05-28-2010, 01:31 PM
Hi again,

I'm sure someone here has encountered this problem before, but the archives revealed nothing, so...

Occasionally my users will send me docs with the Emphasis/Strong styles peppered in. Most of my users don't have a clue about what styles are, so to keep things consistent, I format everything to bold/italics instead. Today I attempted to make a search and replace to do that job for me. Here's as far as I got (replacing the "Strong" style only)


With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("Strong")
.Replacement.Style = ActiveDocument.Styles("Default Paragraph Font")
.Replacement.Font.Bold = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

It looks alright, but the "Replacement.Font.Bold" line doesn't do anything - I think it has something to do with replacing the style with "Default Paragraph Font". If I replace with "Normal" instead, my paragraphs can lose their indents. So I'm stuck.

Any ideas?

TonyJollans
05-29-2010, 02:10 AM
I don't think there is anything you can do about this. You are trying to make two changes of the same type in a single replace operation, and relying on a sequence of events that you can't control; if the bolding is applied first then the application of the style will override it.

Paul_Hossler
05-30-2010, 08:26 AM
I also have the problem with the character styles coming in all willy-nilly, so this was a good thread.

I think you can do it in two steps

This is fairly specific, but can be generalized


Option Explicit

Sub drv()
On Error Resume Next
Call ActiveDocument.Styles("MakeThisBold").Delete
On Error GoTo 0

WordBasic.FormatStyle Name:="MakeThisBold", NewName:="", _
BasedOn:="Emphasis", NextStyle:="", Type:=1, _
FileName:="", Link:=""
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("Emphasis")
.Replacement.Style = ActiveDocument.Styles("MakeThisBold")
.Wrap = wdFindContinue
.Format = True

.Execute Replace:=wdReplaceAll

.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("MakeThisBold")
.Replacement.Font.Bold = True
.Wrap = wdFindContinue
.Format = True

.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Styles("MakeThisBold").Delete
End Sub


Seems to work on the test docs I used. I'd be interested in how you make out

Paul

fumei
05-31-2010, 11:39 AM
I am confused.

"Most of my users don't have a clue about what styles are, so to keep things consistent, I format everything to bold/italics instead. "

If they "don't have a clue", why are you essentially doing the same thing...manually formatting instead of properly using styles?

Paul, I do not understand what you are doing.

1. you look for "Emphasis", and change it to "MakeThisBold" (replacing all)

2. you look for "MakeThisBold"...and then manually make the text bold! HUH???

3. then you delete the style "MakeThisBold"



Say what????

rgmatthes
05-31-2010, 12:47 PM
I am confused.

"Most of my users don't have a clue about what styles are, so to keep things consistent, I format everything to bold/italics instead. "

If they "don't have a clue", why are you essentially doing the same thing...manually formatting instead of properly using styles?

I knew some folks here would disagree with this decision... but I stand by it.

Word doesn't always work well when both Emphasis & italics or Strong & bolding are used. Here's a test you can run yourself (discovered here: http://www.eggheadcafe.com/software/aspnet/29965794/how-to-remove-italic-form.aspx):

In some arbitrary text, type "eit".
Apply the Emphasis style to "e", italic to "i".
Select "eit".
Click Home -> Italicize (or Ctrl+I).Basically, Word will de-italicize either the "e" or the "i", but not both. Clicking Ctrl+I again with toggle both. Maybe this isn't a bug, but it's certainly not intuitive. You may argue this problem doesn't occur very often, but I just had it happen last week, and even I was confused. And it's certainly not the only problem that can occur.

If my goal is to prevent user confusion, it makes sense to use either italics/bold or Emphasis/Strong, not both. And since the vast majority of my users understand bold/italics and specifically *don't* understand Emphasis/Strong, I'm going with bold/italics.

Paul, I also had some problems with your code, but I figured out your strategy. This worked for me:

WordBasic.FormatStyle Name:="MakeThisBold", NewName:="", _
BasedOn:="Strong", NextStyle:="", Type:=1, _
FileName:="", Link:=""

WordBasic.FormatStyle Name:="MakeThisItalic", NewName:="", _
BasedOn:="Emphasis", NextStyle:="", Type:=1, _
FileName:="", Link:=""

With Selection.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("Strong")
.Replacement.Style = ActiveDocument.Styles("MakeThisBold")
.Wrap = wdFindContinue
.Format = True
.Execute Replace:=wdReplaceAll
End With

With Selection.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("MakeThisBold")
.Replacement.Font.Bold = True
.Wrap = wdFindContinue
.Format = True

.Execute Replace:=wdReplaceAll
End With

With Selection.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("Emphasis")
.Replacement.Style = ActiveDocument.Styles("MakeThisItalic")
.Wrap = wdFindContinue
.Format = True
.Execute Replace:=wdReplaceAll
End With

With Selection.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("MakeThisItalic")
.Replacement.Font.Italic = True
.Wrap = wdFindContinue
.Format = True

.Execute Replace:=wdReplaceAll
End With

ActiveDocument.Styles("MakeThisBold").Delete
ActiveDocument.Styles("MakeThisItalic").Delete

Thanks for your help! I'm marking this as solved. :)

fumei
05-31-2010, 01:23 PM
1. "Word doesn't always work well when both Emphasis & italics or Strong & bolding are used."

Which is why one should always use explicit Styles. And ONLY explicit styles. In the example at that link, this happens with:

•Apply the Emphasis style to "e", italic to "i".

The application of italics is a manual formatting.

Once you do ANY manual format - at all - then you have slipped from a proper use of styles.

"If my goal is to prevent user confusion, it makes sense to use either italics/bold or Emphasis/Strong, not both."

I am all for avoiding confusion. However, the answer (to my mind) is not to use italics/bold...but an explicit style (however defined). That is...no manual formatting whatsoever.

I still do not get the point of this:
.Style = ActiveDocument.Styles("MakeThisItalic")
.Replacement.Font.Italic = True
.Wrap = wdFindContinue
.Format = True

.Execute Replace:=wdReplaceAll
End With

ActiveDocument.Styles("MakeThisBold").Delete
ActiveDocument.Styles("MakeThisItalic").Delete


Again...

1. look for test with the style "MakeThisItalic" already applied
2. apply italics to that text
3. then delete the style "MakeThisItalic"

WHY are you deleting the style?
WHY are you applying italics to text that already has MakeThisItalic applied to it? One would assume that a style named "MakeThisItalic"...has the text in italics.

I do not get it.

Paul_Hossler
05-31-2010, 01:40 PM
Paul, I do not understand what you are doing.
1. you look for "Emphasis", and change it to "MakeThisBold" (replacing all)
2. you look for "MakeThisBold"...and then manually make the text bold! HUH???
3. then you delete the style "MakeThisBold"


Well, I was going by Tony's #2 about not being able to do 2 operations with a single Find/Replace. So I tried 2 Find/Replace operations, each one operation

1. I created a temporary style

2. The first Find/Replace re-styled any text with the BuiltIn style Emphasis (which cannot be deleted) with my temporary one

2. The second Find/Replace made any text in my style (which can be deleted) with Bold text.

4. Deleting my temporary style left the Bold text in the Normal style, but still in Bold.

5. At the end, text styled in Emphasis ends up in Normal, but Bolded.

I believe that was the OP's intention

Seems to work, but might be easer ways to do it.

I do agree that this is violating one of the standard rules by applying a formatting attribute manually, but rules are guidelines and sometimes you need bend them

Paul

fumei
05-31-2010, 02:37 PM
Let's read Tony's post...

I don't think there is anything you can do about this. You are trying to make two changes of the same type in a single replace operation, and relying on a sequence of events that you can't control; if the bolding is applied first then the application of the style will override it.And this is true, but the key point is the last one. The style will override it.

Yes.

The style will override it.

Yes.

And.........the point being...use a style. Define the style precisely as you wish it to be, and apply then. Done.

If the point is to have the style as Normal, but manually formatted (bold, italics, wingding...it does not matter) then I shall step aside, as this is against all I teach about using Word. But...
Sub MyYadda()
Dim r As Range
Set r = ActiveDocument.Range

With r.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Emphasis")
Do While .Execute(Findtext:="", Forward:=True) = True
If .Found = True Then
With r
.Style = "MyBoldItalics"
.Collapse 0
End With
End If
Loop
End With
End Sub
Demo attached. Click "My Yadda" on the top toolbar. This changes all text as Emphasis to an explicit style MyBoldItalics. Note that this is a Character style, so therefore the underlying style is....Default Paragraph Format. In other words, it is still Normal.

The code above can be adjusted to deal with Strong, or a combination of Emphasis and Strong.

I completely agree that rules can be dispensed with under the right circumstances. This, IMO, is not one of them.

Paul_Hossler
05-31-2010, 04:41 PM
I would have thought that this would have been simpler


Sub SonOfMyYadda()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Emphasis")
.Replacement.ClearFormatting
.Replacement.Style = ActiveDocument.Styles("MyBoldItalics")
.Format = True
.Execute Replace:=wdReplaceAll
End With
End Sub


but I believe I recall that was a reason to use .Collapse inside.

Can you remind me, please ?

Paul

fumei
06-01-2010, 10:16 AM
No, your version is correct. I forgot that I had extra instructions (testing for both Emphasis and Strong) and so had an extra internal loop that I took out...but not all of it.. However, applying against one condition (Emphasis) your version is the correct one.