Share via

AutoFormat not working with macro

Anonymous
2015-08-21T06:56:33+00:00

Hi, I recorded the following macro to quickly italicize titles in a reference list:

Selection.TypeText Text:="_"

    Selection.Find.ClearFormatting

    With Selection.Find

        .Text = "."

        .Forward = True

        .Wrap = wdFindContinue

    End With

    Selection.Find.Execute

    Selection.MoveRight Unit:=wdCharacter, Count:=-1

    Selection.TypeText Text:="_"

I put my insertion point at the beginning of the title, and the macro inserts an underscore there and at the end of the title (as defined by a period). I assumed that this would trigger Word's "real formatting," which I have ticked on both the AutoFormat and AutoFormat as You Type tabs. But it doesn't work. All I get are the underscores. This is odd, since my other macro that relies on autoformatting (to make straight quotes curly) is working fine.

Any idea what's going on?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

8 answers

Sort by: Most helpful
  1. Anonymous
    2015-08-24T04:21:59+00:00

    Thanks. Doug's macro works great and is probably faster than my clunky recorded one.

    I'm still perplexed, though, that AutoFormat didn't work in this case but does work for the curly quotes macro. That macro globally replaces quotation marks, which Word then reformats as curly using the same AutoFormat tab.

    Was this answer helpful?

    0 comments No comments
  2. Stefan Blom 342.4K Reputation points MVP Volunteer Moderator
    2015-08-22T23:41:45+00:00

    The settings on the AutoFormat tab in the AutoCorrect dialog box only apply if you run AutoFormat on the document, which you can do manually by adding the AutoFormat command to the Quick Access Toolbar. (There is also an AutoFormat As You Type tab, whose settings apply as you type, but running a macro isn't considered "typing.")

    You may want to try Doug's macro instead.

    Was this answer helpful?

    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2015-08-21T23:59:05+00:00

    If you run a macro containing the following code, it will italicise the text from the location of the selection until the next period in the document:

    Dim irange As Range

    Set irange = Selection.Range

    irange.MoveEndUntil Cset:="."

    irange.Font.Italic = True

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2015-08-21T21:23:00+00:00

    If you switch on "real formatting" in the AutoFormat tab, you can add italics to text by enclosing it in underscores. Word automatically italicizes the text and then deletes the underscores (ditto for asterisks and bolding). So I recorded this macro to add underscores, but the problem is that the AutoFormat function is not triggered, even though it works when I manually add the underscores. In other words, the AutoFormat seems not to be working with the macro. As I said, though, this is strange given that I have a macro that replaces straight quotes with curly ones, a process that relies on AutoFormat and that is working fine.

    Was this answer helpful?

    0 comments No comments
  5. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2015-08-21T07:18:02+00:00

    The underscore character _ and underlining are two different things.

    However, a quick way of what you want to do would be to triple click in the heading paragraph, which should cause the whole of the paragraph to be selected and then click the Italic I button in the Font section of the home tab of the ribbon.

    Was this answer helpful?

    0 comments No comments