![]() |
|
#4
|
||||
|
||||
|
Bearing in mind that your idea of what constitutes a 'word' and what VBA considers is a word may not coincide. The following will split the paragraphs into 50 'word' segments.
Code:
Sub DeleteTextBetweenTwoWords()
'Graham Mayor - https://www.gmayor.com - Last updated - 07 Mar 2020
Dim strFirstWord As String
Dim strLastWord As String
Dim objDoc As Document
Dim lngPara As Long
Dim oRng As Range
Set objDoc = ActiveDocument
strFirstWord = "T:"
strLastWord = "P:"
With Selection
.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strFirstWord & "*" & strLastWord
.Replacement.Text = strLastWord
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
For lngPara = objDoc.Paragraphs.Count To 1 Step -1
Set oRng = objDoc.Paragraphs(lngPara).Range
If oRng.Words.Count > 50 Then
SplitString oRng
End If
Next lngPara
With Selection
.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "%%%%"
.Replacement.Text = "^p"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
lbl_Exit:
Set objDoc = Nothing
Set oRng = Nothing
Exit Sub
End Sub
Private Sub SplitString(oRng As Range)
'Graham Mayor - https://www.gmayor.com - Last updated - 07 Mar 2020
Dim i As Integer
Dim oSplit As Range
Set oSplit = oRng
Do
oSplit.MoveStart wdWord, 50
If Len(oSplit) < 51 Then GoTo lbl_Exit
oSplit.InsertBefore "%%%%"
Loop
lbl_Exit:
Set oSplit = Nothing
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to find (highlight) two and more words in a list of 75k single words in Word 2010 | Usora | Word | 8 | 05-29-2018 03:34 AM |
Capitalize one to two words if they are the only words on a line
|
jrhlavac | Word VBA | 1 | 10-08-2015 08:19 PM |
How to mark underlined words in a sentence as A, B, C, D (beneath the words)
|
thudangky | Word | 13 | 12-12-2013 02:22 AM |
Spliting column with text and numbers
|
mjosic | Excel | 3 | 04-04-2012 05:56 AM |
| Why Words doesn’t show the style of the selected words automatically???? | Jamal NUMAN | Word | 0 | 04-14-2011 03:20 PM |