PDA

View Full Version : Solved: Targeting all Story Ranges using “ConvertNumbersToText”



Ice-Tea-Jan
05-03-2011, 07:50 PM
Hello,


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

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?

:anyone:
Thanking you,
Janet


Sub ConvertNumbersToText()
'
ActiveDocument.ConvertNumbersToText
'
End Sub



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

gmaxey
05-04-2011, 03:41 AM
Jan,

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

Ice-Tea-Jan
05-04-2011, 08:57 AM
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

Frosty
05-04-2011, 01:40 PM
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...


For i = 1 to 1500
ActiveDocument.ConvertNumbersToText
Next

Ice-Tea-Jan
05-04-2011, 02:12 PM
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,:hi:
Janet

gmaxey
05-05-2011, 04:09 AM
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.


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

Ice-Tea-Jan
05-05-2011, 10:07 AM
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! :thumb
Regards,
Janet

gmaxey
05-05-2011, 02:34 PM
You're welcome and yes it is a great time to be associated in any manner with the United States Navy SEALS.