VBA - Range to include numbered lists

Copper Contributor

Hello. If you have a minute, I'm extracting text from a range within MS Word...for example...

 

targetRange.Start=200

targetRange.End=500

 

This returns all the text (targetRange.Text) but since unformatted (although line returns seem to be preserved), all numbered list labels (1,2,3...a, b, c sublists, etc) within the Range are stripped. Is there a way to preserve all the list designations within the text when setting a Range object? Tried targetRange.FormattedText but that doesn't seem to make a difference. thank you.

13 Replies

@Tom_Griffith What do you want to do with the range?

@Tom_Griffith 

 

You can retrieve the formatted text from a range. However, as Doug wrote, making a suggestion would be a lot easier if we knew exactly what you are trying to accomplish with your macro. 

Hi. I'm trying to extract the wording from the range in MS Wprd and put it into a text field in a db. However, the numbered list labels aren't caught with Text and FormattedText. thank you so much
Hi. yeah, it gets the format but the numbered lists don't seem to be captured. I'm trying to take the range wording and put it into a text field in a db. Thank you.

@Tom_Griffith Before extracting the text from the range, use the command

 

ConvertNumberstoText

oh, wow. Is there a reverse of ConvertNumberstoText, in order to restore? I'd rather not change the list numbering on the Word doc itself, just need the text for the db with the converted to text list numbers. I've always struggled with this DOM as I'm thinking I'm changing some object in memory and not real-time ranges, etc on the Word doc. thank you so much again.

@Tom_Griffith Either use Undo, or close the document without saving it.

Thanks again for your help on those two methods. I decided on the following (pseudo code.)

wordDoc.ConvertNumbersToText
var action_text=actionRange.FormattedText;
wordDoc.Undo
action_text=action_text.raplace(/\t\/," ")
...do insert stuff with action_text....

I was hoping to do a more precise Find.Execute on the Range to get rid of only the tabs inserted (number+tab+text) by ConvertNumberToText but I guess I'll live with a global tab replace on the text object (action_text) created from the Range. It'll dump any other tabs user may have put in but i think that'll be ok. I think doing any additional stuff after ConvertNumbersToText, like the Find.Replace, will shove the ConvertNumbersToText out as the Undo target. Thanks so much again.

@Tom_Griffith Use

 

With ActiveDocument
    .UndoClear
    .ConvertNumbersToText
    Selection.Range.Text = Replace(Selection.Range.Text, vbTab, " ")
    'do what you want to
    .Undo (3) 'may need to replace 3 to allow for other changes that you make
End With
ok, thanks so much for your help, that's what I did.
Hello. I fond something was up with this a month or so later. I'm not so sure, but I think when there are no numbered lists, the Undo doesn't see the Worddoc.ConvertNuberstoText as a change and prroceeds to undo the previous target (in this case, adding some line of text to a range). That line of text always disappears. I'm going t try to run more tests to see but is there a boolean thing I could try to check if ConvertNumbersToText applied a change? thank you.

@Tom_Griffith With the following code, if there are no numbered lists, the code will be terminated after the .ConvertNumberstoText

 

Dim blnConvert As Boolean
With ActiveDocument
    .UndoClear
    .Save
    .ConvertNumbersToText
    blnConvert = .Saved
    If blnConvert = True Then
        Exit Sub
    End If
    Selection.Range.Text = Replace(Selection.Range.Text, vbTab, " ")
    'do what you want to
    .Undo (3) 'may need to replace 3 to allow for other changes that you make
End With

@Doug_Robbins_Word_MVP thanks so much. I had to jump off this for a little while and came back to it the other day. I took your suggestion and kind of do something similar, because if there are no numbered lists, there still is processing to be done.. I parse all the paragraphs in the doc, if gwordDoc.Paragraphs(i).Range.ListFormat.ListType=3 is encountered, set the boolean blnConvert to true. If blnConvert=true, do the gWordDoc.ConvertNumbersToText, do the stuff then call the gWordDoc.Undo.

 

Otherwise, if gwordDoc.Paragraphs(i).Range.ListFormat.ListType=3 is not encountered parsing the gWordDoc paragraphs, just do the stuff.