Consulting

Results 1 to 8 of 8

Thread: Solved: Targeting all Story Ranges using “ConvertNumbersToText”

  1. #1

    Question Solved: Targeting all Story Ranges using “ConvertNumbersToText”

    Hello,


    This forum has always helped me tremendously, and now I’m back again for more.

    In Word 2007, I need to target all story ranges with the “ConvertNumbersToText” method. I fully understand that this targets paragraphs that are numbered with the bullets & numbering dialog box, and the LISTNUM field. This is precisely what I want since I can then excerpt & copy certain sentences and still keep the numbers static.

    I have two “newbie” macro samples below. The first is a one-liner and performs very fast, but I’m not sure it passes through every story type and every story instance.

    The second macro was something I revised in order to get it to fly. (See the commented line where I inserted “ActiveDocument” ahead of “ConvertNumbersToText.”) Conversely, this runs VERY SLOW, and thus I’m pretty sure something is amiss.

    Can somebody please advise me on how to pass “ConvertNumbersToText” so that is passes through each instance of each story type in a Word 2007 document?


    Thanking you,
    Janet

    [vba]
    Sub ConvertNumbersToText()
    '
    ActiveDocument.ConvertNumbersToText
    '
    End Sub
    [/vba]


    [vba]
    Sub ConvertNumbersToTextInRanges()
    '
    Dim prange As Range, para As Paragraph
    For Each prange In ActiveDocument.StoryRanges
    Do
    For Each para In prange.Paragraphs
    ActiveDocument.ConvertNumbersToText 'Added ~ActiveDocument~ to get it to run
    Next para

    Set prange = prange.NextStoryRange

    Loop Until prange Is Nothing

    Next

    End Sub
    [/vba]
    "If" is a big word.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Jan,

    As best I can tell you first macro does convert numbers to text in all the storyranges. What problem are you experiencing?
    Greg

    Visit my website: http://gregmaxey.com

  3. #3

    Smile

    Hello Greg,

    Thanks for responding.

    The problem I’m experiencing is my own understanding of how one line: “ConvertNumbersToText” -- passes through all the story layers?

    Is it because it is a VBA “method” – and therefore works upon the entire document?

    I thought this needed to pass through all story layers, and thus the second macro was my attempt at making sure all story layers were accessed – but this second macro runs VERY SLOW.

    Can I simply rely on the first macro to access all document stories?

    Thanking you,
    Janet
    "If" is a big word.

  4. #4
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    I started to have a big long response explaining the concept of story ranges... and then I looked at the help file on the ConvertNumbersToText Method.

    It operates on the document object. Since it operates on the document object (and not a Range object), yes... for better or worse, you can run your first macro... and the only thing your second macro is doing it simply running the first macro as many times as you have paragraphs in all of the various story ranges in your document (which can be a pretty big number, depending on how complicated your document is).

    That's why the second one is slow. You're probably running the exact same line of code a bunch of times, instead of just once.

    i.e., the following code would be slow too...

    [vba]
    For i = 1 to 1500
    ActiveDocument.ConvertNumbersToText
    Next
    [/vba]

  5. #5

    Smile Thank you Frosty & Greg!

    Thank you Frosty and Greg for your prompt answers.
    I was hoping the first smaller macro would do what the second macro was doing anyway.
    I was just in the process of testing it on different story layers as I read your answers.
    Thanks for saving me time, and for giving me a better understanding.
    This forum is second to none.
    Thanking you,
    Janet
    "If" is a big word.

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Jan,

    I replied "as best I could tell" because like Jason I was preparing to offer an explaination of processing storyranges (probably not as big or as long) when it dawned on me that being a document method, ConvertNumbersToText did act on the entire document and not just a range. I was convinced when I embedded a numbered list in the text frame of a shape object in the header of a document. That is one place in a document that even procecssing storyranges won't get to unless you dig a little deeper as the following may illustrate. Add a shape the header of a document and then add a REF field to the shape, to the header, and in the document. Run this code with the Select Case statement stetted out as shown. You will see that the REF fields in the document and in the header will update. Unstet the Select Case statement and run the code again. All fields will update.


    [VBA]Sub myUpdateFields()
    Dim rngStory As Word.Range
    Dim oShp As Word.Shape
    For Each rngStory In ActiveDocument.StoryRanges
    Do
    On Error Resume Next
    rngStory.Fields.Update
    'Select Case rngStory.StoryType
    'Case 6, 7, 8, 9, 10, 11
    'If rngStory.ShapeRange.Count > 0 Then
    'For Each oShp In rngStory.ShapeRange
    'If oShp.TextFrame.HasText Then
    'oShp.TextFrame.TextRange.Fields.Update
    'End If
    'Next oShp
    'End If
    'Case Else
    'Do Nothing
    'End Select
    On Error GoTo 0
    'Get next linked story (if any)
    Set rngStory = rngStory.NextStoryRange
    Loop Until rngStory Is Nothing
    Next rngStory
    End Sub

    [/VBA]
    Greg

    Visit my website: http://gregmaxey.com

  7. #7

    Smile Greg, Thanks for Follow-Up!

    Greg,
    Thanks for your follow up.
    I was doing pretty much the same testing as you were, and as you stated, ConvertNumbersToText did act on the entire document and not just a range.
    Interestingly, I was “looking/studying” that same VBA code (with stories and select case statement) that you assisted me with some time ago.
    The “ConvertNumbersToText” will save me a tremendous amount of work!
    This forum provides “Navy Seal” class support!
    Regards,
    Janet
    "If" is a big word.

  8. #8
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    You're welcome and yes it is a great time to be associated in any manner with the United States Navy SEALS.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •