Forum Discussion
Tom_Griffith
May 16, 2023Copper Contributor
VBA - Range to include numbered lists
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...
Tom_Griffith
May 18, 2023Copper Contributor
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.
May 18, 2023
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
- TomGriffith3Oct 13, 2023Brass Contributor
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.
- Jul 14, 2023
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
- Tom_GriffithJul 14, 2023Copper ContributorHello. 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_GriffithJun 14, 2023Copper Contributorok, thanks so much for your help, that's what I did.