Share via

Constrain activedocument.convertnumberstotext action to only certain number formats

Anonymous
2012-01-05T18:09:26+00:00

I have a document using standard numbering for sections, subsections, etc.  Example: 1, 1.1, 1.1.1, 1.1.2, 2, 2.1, and so on.

But I'm also using custom numbering.  For example:  DO-THIS-001, DO-THIS-002, DO-THIS-003, and so on.

I can run activedocument.convertnumberstotext to convert the numbers to text, but it does it for all numbers.  I would like to have a command that would only convert the custom numbering numbers (the "DO-THIS-001" numbers).  I was thinking that I could save that particular numbering format as a style and then convert only that style, but I have no idea how to code VBA for this type of task.

Any help will be greatly appreciated.

Thanks in advance,

Andrew

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

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2012-01-06T03:25:21+00:00

Sorry.... I did warn you that I didn't test the code. The line that gave you trouble should be

.MatchWildcards = True

Although that will let the macro run, I found that it still won't do anything, because I misunderstood the original setup of the document.

If you defined a specific style that applies the DO-THIS- numbering (a style that is distinct from the styles that apply the standard numbering), then the code should look something like this (change "List Paragraph" to the actual name of your style):

Sub convertSpecific_Click()

    Dim r As Range

    Set r = ActiveDocument.Range

    With r.Find

        .Style = ActiveDocument.Styles("List Paragraph")

        .Forward = False

        While .Execute

            r.ListFormat.ConvertNumbersToText wdNumberText

            r.Collapse wdCollapseStart

        Wend

    End With

End Sub

The .Forward = False causes the macro to work from the end of the document back to the beginning. If you use the default .Forward = True, as soon as the first occurrence is converted to text, the second occurrence changes its number to 001. Working back-to-front avoids that.

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Stefan Blom 342.4K Reputation points MVP Volunteer Moderator
2012-01-05T18:32:17+00:00

You can have Word convert only paragraph (outline) numbering, only LISTNUM fields, or both. Use the wdNumberParagraph or wdNumberListNum constants (for example ActiveDocument.ConvertNumbersToText wdNumberParagraph).

But there is no way for the method to tell the difference between different formats, if that's what you're asking.

Note that you can run the method for the selection only, which you may find useful:

Selection.Range.ListFormat.ConvertNumbersToText

Also, other types of numbering, such as SEQ fields, will not be affected by the command.

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2012-01-06T00:13:36+00:00

    Thanks guys.  This is good stuff.

    Stefan's lines work for me, but I'm having trouble with the loop.  Which would be really handy to have.

    I have the loop triggered by a button, as follows:

    Sub convertSpecific_Click()

        Dim r As Range

        Set r = ActiveDocument.Range

        With r.Find

           .UseWildcards = True

           .Text = "DO-THIS-[0-9]{3}"

           While .Execute

              r.ListFormat.ConvertNumbersToText

              r.Collapse wdCollapseEnd

           Wend

        End With

    End Sub

    If I leave the .UseWildcards = True uncommented then I receive the error, "Compile Error:  Method or data member not found."  If I comment out that line then nothing happens.  I'm wondering if the syntax for the text we are trying to find is correct.  When I'm in the Define New Number Format dialog, I select the Number Format / Number Style "01, 02, 03, ..."  Then in the Number Format field I type "DO-THIS-" and the "01" is greyed. 

    Does this help?  (I confess I'm clueless how to fix it.  I tried "DO-THIS-[00-99]{3}", but that didn't work.)

    Thanks again,

    Andrew

    Was this answer helpful?

    0 comments No comments
  2. Stefan Blom 342.4K Reputation points MVP Volunteer Moderator
    2012-01-05T23:11:46+00:00

    Good idea!

    Was this answer helpful?

    0 comments No comments
  3. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2012-01-05T19:50:00+00:00

    To follow up on Stefan's suggestion, you can run a loop that uses the Find method of a Range object to locate only the custom numbers, and run ConvertNumbersToText on those ranges. Something like this (warning: "air code", not tested):

    Dim r As Range

    Set r = ActiveDocument.Range

    With r.Find

       .UseWildcards = True

       .Text = "DO-THIS-[0-9]{3}"

       While .Execute

          r.ListFormat.ConvertNumbersToText

          r.Collapse wdCollapseEnd

       Wend

    End With

    Was this answer helpful?

    0 comments No comments