VBA - Range to include numbered lists

Occasional 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.

9 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