3

Having a lot of trouble with this macro:

 ActiveDocument.CopyStylesFromTemplate NormalTemplate.FullName
    With Selection
        .Style = ActiveDocument.Styles("Normal")
        .Range.HighlightColorIndex = 0
        .Font.Shading.Texture = wdTextureNone
        .Font.Name = ActiveDocument.Styles("Normal").Font.Name
        .Font.Size = ActiveDocument.Styles("Normal").Font.Size
    End With

It works perfectly with a line like: This text should be formatted without bold or italics.

And converts it to" This text should be formatted without bold or italics.

However, it only works if I select up to right before the last character and run it. When I double click the line or select the entire line manually and run the macro it seems to have no effect at all.

Any ideas what might be causing this or if there is a different method I should use to remove bold/italicized text within a selection and convert all of the text to Normal style/font?

Edit: This may have to do with running the macro from a keyboard shortcut as it works fine when run manually via the macro menu.

R3uK
  • 14,417
  • 7
  • 43
  • 77
pavja2
  • 397
  • 3
  • 9
  • 20

2 Answers2

3

If you want to clear all formatting, I'd suggest this:

With Selection
    .ClearFormatting
End With

This resets the paragraph style to Normal and resets all fonts and attributes (that I've tried to set). Note that it resets the paragraph style for the paragraph that includes the selection (so if you've only selected a single word in the paragraph, it still resets the style for the paragraph).

ref: MSDN: Selection.ClearFormatting

I didn't see any difference when I added ActiveDocument.CopyStylesFromTemplate NormalTemplate.FullName but I expect this would depend on the complexities of your documents.

GregHNZ
  • 7,946
  • 1
  • 28
  • 30
  • This works fine but only when I run it manually via the macros menu. When run via a keyboard shortcut it has the same problem as the original macro. Really strange since my other macros work fine from the keyboard.... – pavja2 Jun 23 '13 at 02:27
  • Never mind - the real problem was merely that the person who had written the template before it came my way had misnamed a macro and I was editing the wrong one. Thanks for the quick reply. – pavja2 Jun 23 '13 at 02:36
  • You can also apply the "Default Paragraph Font" style here; that will only affect the selected text (it will not reset the paragraph style to Normal, though). – Christina Jun 25 '13 at 15:44
0

Turns out that the problem was poor documentation by a previous developer of the template I was using - I was merely editing the wrong macro and thus got partially correct results from a flawed macro despite my fixes not showing anything.

pavja2
  • 397
  • 3
  • 9
  • 20