Log in

View Full Version : Solved: Delete text from range



MWE
02-11-2006, 11:23 PM
I am sure this has been answered before, but after searching and looking through lots of hits, ...

I wish to delete text in a Word range, say from character index I to character index J. I have some ugly code that does this, but I am sure there is a simple and elegant way to do it.

Also, how would I change the font type, font size and bold/not for, again, a text block from char index I to char index J.

Thanks

mdmackillop
02-12-2006, 03:14 AM
Hi MWE
Here's an input box method for both Delete and Format operations, easily extendible for more options. I added a bit to use the current selection, if appropriate.
Regards
MD


Option Compare Text
Sub FormatDelete()
Dim MyRange As Range
Dim FixText As String, MyStart As String

MyStart = ""
'Set default operation
If Len(Selection) > 1 Then
MyStart = Selection.Start & ", " & Selection.End & ", " & "Format"
End If
FixText = InputBox("Enter Start, End, Operation", "Fix Text", MyStart)

Set MyRange = ActiveDocument.Range(Start:=Split(FixText, ",")(0), _
End:=Split(FixText, ",")(1))

Select Case Trim(Split(FixText, ",")(2))
Case "Delete"
MyRange.Delete
Case "Format"
With MyRange.Font
.Name = "Arial"
.Size = 16
.Bold = wdToggle
End With
End Select
Set MyRange = Nothing
End Sub

fumei
02-12-2006, 05:24 AM
If you KNOW the character index number, then you can just set a range object for it, and do what you like.Dim oRange As Word.Range
Set oRange = ActiveDocument.Range(Start:=I, End:=J)
With oRange
.Font.Bold = True
.Font.Size = 16
' etc etc
End With
Set oRange = Nothing

MWE
02-12-2006, 11:05 AM
Hi MWE
Here's an input box method for both Delete and Format operations, easily extendible for more options. I added a bit to use the current selection, if appropriate.
Regards
MD


Option Compare Text
Sub FormatDelete()
Dim MyRange As Range
Dim FixText As String, MyStart As String

MyStart = ""
'Set default operation
If Len(Selection) > 1 Then
MyStart = Selection.Start & ", " & Selection.End & ", " & "Format"
End If
FixText = InputBox("Enter Start, End, Operation", "Fix Text", MyStart)

Set MyRange = ActiveDocument.Range(Start:=Split(FixText, ",")(0), _
End:=Split(FixText, ",")(1))

Select Case Trim(Split(FixText, ",")(2))
Case "Delete"
MyRange.Delete
Case "Format"
With MyRange.Font
.Name = "Arial"
.Size = 16
.Bold = wdToggle
End With
End Select
Set MyRange = Nothing
End Sub


Thanks. Your reply was quite helpful. However, I do not understand something about how Word handles Ranges. In your code above, the start and end of the selection range and the start/end of the entire document range seem to be the same. I did a little testing and that seems to be true. Why? When I Msgbox Selection.Range to the screen, only the selected text is displayed. What am I missing?

I would prefer to operate on the selection with relative indices not absolute for the document. I can do the latter, but that means adding offsets.

Thanks

fumei
02-13-2006, 10:36 AM
If Selection.Range and document range start/end are the same, then the whole document has been selected. Simple as that.
I would prefer to operate on the selection with relative indices not absolute for the document. This seems contrary to your original post.
I wish to delete text in a Word range, say from character index I to character index J. If you are using actual index numbers (I or J) then they are explicit numbers, not relative. Those explicit numbers can of course be set relatively, but the numbers themselves are explicit.

If you just want to action the Selection...then action the Selection. Actions on the Selection do not need the Range of it, or the numbers.
Also, how would I change the font type, font size and bold/not for, again, a text block from char index I to char index J.This is not a request on the Selection, this is request for a Range. Which is it?

You can certainly set up a Range by relative numbers TO the current Selection. I am trying to understand exactly what you are doing. Are you actioning the Selection, or a Range?

In any case, Msgbox Selection.Range will indeed give the Selection range. If it returns the entire document, then the entire document was selected.

fumei
02-13-2006, 10:45 AM
I just want to reiterate that with Malcolm's code, if you do not have the entire document selected, then the start and end of the selection range is not the same as start/end of the document. His code precisely picks up the Selection range, whatever it is.