Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.
Sub RemoveParaOrLineBreaks()
Dim staticTC As String
staticTC = "--:--:--:--,--:--:--:--,10"
Dim static_next_line As String
static_next_line = "80 80 80"
Dim found, boolvar, nulls2 As Boolean
Dim tpar, parnum, slidenum As Variant
Dim slide_s1 As String
Dim slide_s2 As String
Dim parasel, new_replace As String
Dim vararray() As String
Dim newsel, whole_slide As Range
tpar = ActiveDocument.Paragraphs.count
boolvar = True
nulls2 = False
slide_s1 = ""
slide_s2 = ""
Set whole_doc = Selection.Range
whole_doc.WholeStory
Do Until boolvar = False
found = False
Set whole_slide = Selection.Range
whole_slide.WholeStory
'find the first pattern of text: 4 digits followed by a space followed by a colon followed by a line break
With whole_slide.Find
.ClearFormatting
.Execute FindText:="^#^#^#^#" & Chr(32) & ":" & Chr(13), Forward:=True, _
Format:=False, Wrap:=wdFindStop
'if we find it, tell us what we found and set found to true
If .found = True Then
whole_slide.Find.Execute FindText:="^#^#^#^#"
slidenum = whole_slide
continue = True
found = True
firstsel = False
'MsgBox slidenum
End If
End With
'if found is still not true, find the second pattern: 4 digits, followed by a letter, followed by a colon followed by a line break
If found = False Then
With whole_slide.Find
.ClearFormatting
.Execute FindText:="^#^#^#^#^$" & ":" & Chr(13), Forward:=True, _
Format:=False, Wrap:=wdFindStop
'if we find it, tell us what we found and set found to true. Also, parse found data and set slidenum to actual slide num
If .found = True Then
whole_slide.Find.Execute FindText:="^#^#^#^#^$"
slidenum = whole_slide
continue = True
found = True
firstsel = False
'MsgBox slidenum
End If
End With
End If
If found = False Then
boolvar = False
Exit Sub
End If
If (continue = True And boolvar = True) Then
'Assign sentence after slide number to slide_s1 var
slide_s1 = whole_slide.Next(Unit:=wdSentence, count:=1)
'if there are elipses, it's a special case we need to account for
If slilde_s1 = " ..." Then
slide_s1 = slide_s1 & whole_slide.Next(Unit:=wdSentence, count:=2)
End If
'If there are two spaces, it's a special case we need to account for
If Right(slide_s1, 2) = Chr(32) & Chr(32) Then
slide_s1 = slide_s1 & whole_slide.Next(Unit:=wdSentence, count:=2)
nulls2 = True
End If
'MsgBox "|" + slide_s1 + "|"
'Assign second sentence to slide_s2 var
slide_s2 = whole_slide.Next(Unit:=wdSentence, count:=2)
'Check if slide_s2 winds up being part of the next slide or if nulls2 is valid. If so, set it to null.
If (slide_s2 Like "*####*" = True Or nulls2 = True) Then
slide_s2 = ""
End If
' 'MsgBox "|" + slide_s2 + "|"
'Decide if slide has one or two sentences
If Len(slide_s1) <> 0 And Len(slide_s2) < 1 Then
'replace text and move whole_slide selection to end of slide
whole_slide.MoveEnd wdParagraph, 2
With whole_doc.Find
.Text = whole_slide
.ClearFormatting
.Replacement.Text = Chr(13) & Chr(13) & slidenum & " : " & staticTC & Chr(11) & static_next_line & Chr(11) & "C2N03 " & slidenum & " : " & Chr(11) & "C2N03" & Chr(32) & Chr(32) & slide_s1
.Execute Replace:=wdReplaceAll
End With
ElseIf Len(slide_s1) <> 0 And Len(slide_s2) <> 0 Then
'truncate line breaks at end of second sentence
slide_s2 = Replace(slide_s2, Chr(13), "")
slide_s2 = Left$(slide_s2, Len(slide_s2) - 1)
'replace text and move whole_slide selection to end of slide
whole_slide.MoveEnd wdParagraph, 3
With whole_doc.Find
.Text = whole_slide
.ClearFormatting
.Replacement.Text = Chr(13) & Chr(13) & slidenum & " : " & staticTC & Chr(11) & static_next_line & Chr(11) & "C2N03 " & slidenum & " : " & Chr(11) & "C2N03" & Chr(32) & Chr(32) & slide_s1 & "C2N03 " & slide_s2
.Execute Replace:=wdReplaceAll
End With
End If
End If
Loop
End Sub
Do more with
Dim parasel, new_replace As String
Dim parasol As Variant, new_replace As String
So always declare the type explicitly.Option Explicit
Public Sub ProcessDocument()
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Execute FindText:="^#^#^#^#" & Chr(32) & ":" & Chr(13), Forward:=True, Format:=False, Wrap:=wdFindStop
Do While Selection.Find.found
ProcessRange Selection.Range
Selection.Find.Execute
Loop
End Sub
Private Sub ProcessRange(ByVal CMatch As Word.Range)
Const STATIC_TC As String = "--:--:--:--,--:--:--:--,10"
Const STATIC_NEXT_LINE As String = "80 80 80"
Dim nulls2 As Boolean
Dim slide_s1 As String
Dim slide_s2 As String
nulls2 = False
slide_s1 = ""
slide_s2 = ""
'Assign sentence after slide number to slide_s1 var
slide_s1 = CMatch.Next(Unit:=wdSentence, Count:=1)
'if there are elipses, it's a special case we need to account for
If slide_s1 = " ..." Then
slide_s1 = slide_s1 & CMatch.Next(Unit:=wdSentence, Count:=2)
End If
'If there are two spaces, it's a special case we need to account for
If Right(slide_s1, 2) = Chr(32) & Chr(32) Then
slide_s1 = slide_s1 & CMatch.Next(Unit:=wdSentence, Count:=2)
nulls2 = True
End If
'Assign second sentence to slide_s2 var
slide_s2 = CMatch.Next(Unit:=wdSentence, Count:=2)
'Check if slide_s2 winds up being part of the next slide or if nulls2 is valid. If so, set it to null.
If (slide_s2 Like "*####*" = True Or nulls2 = True) Then
slide_s2 = ""
End If
CMatch.Text = "<" & Replace(CMatch.Text, vbCr, "") & ">" & vbCr
'Decide if slide has one or two sentences
If Len(slide_s1) > 0 And Len(slide_s2) < 1 Then
'replace text and move whole_slide selection to end of slide
CMatch.MoveEnd wdParagraph, 2
CMatch.Find.Text = CMatch
CMatch.Find.ClearFormatting
CMatch.Find.Replacement.Text = Chr(13) & Chr(13) & CMatch & " : " & STATIC_TC & Chr(11) & STATIC_NEXT_LINE & Chr(11) & "C2N03 " & CMatch & " : " & Chr(11) & "C2N03" & Chr(32) & Chr(32) & slide_s1
CMatch.Find.Execute Replace:=wdReplaceAll
ElseIf Len(slide_s1) > 0 And Len(slide_s2) > 0 Then
'truncate line breaks at end of second sentence
slide_s2 = Replace(slide_s2, Chr(13), "")
slide_s2 = Left$(slide_s2, Len(slide_s2) - 1)
'replace text and move whole_slide selection to end of slide
CMatch.MoveEnd wdParagraph, 3
CMatch.Find.Text = CMatch
CMatch.Find.ClearFormatting
CMatch.Find.Replacement.Text = Chr(13) & Chr(13) & CMatch & " : " & STATIC_TC & Chr(11) & STATIC_NEXT_LINE & Chr(11) & "C2N03 " & CMatch & " : " & Chr(11) & "C2N03" & Chr(32) & Chr(32) & slide_s1 & "C2N03 " & slide_s2
CMatch.Find.Execute Replace:=wdReplaceAll
End If
End Sub
It's not exactly what you code does, you need to adjust the internals.
Option Explicit
Public Sub ProcessDocument()
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Execute FindText:="^#^#^#^#" & Chr(32) & ":" & Chr(13), Forward:=True, Format:=False, Wrap:=wdFindStop
Do While Selection.Find.found
ProcessRange Selection.Range
Selection.Find.Execute
Loop
End Sub
Private Sub ProcessRange(ByVal CMatch As Word.Range)
Debug.Print "Match: '" & CMatch.Text & "'."
End Sub
You can see the output of Debug.Print in the immediate window (CTRL-G, when not open).
Sub RemoveParaOrLineBreaks()
'use constants here instead of variables
Const staticTC = "--:--:--:--,--:--:--:--,10"
Const static_next_line = "80 80 80"
'added this
Const marker_code = "C2N03"
Dim bStop As Boolean
'variant for previous and next slide number
Dim vSlide As Variant, vNext As Variant
'we need 3 range variables:
Dim rSlide As Range, rNext As Range, rDoc As Range
'start and end long integer markers for rSlide range:
Dim iStart As Long, iEnd As Long
'length of slide number line:
Dim iPos As Long
'variable to hold replacement text:
Dim sText As String
'initial setup
Set rDoc = ActiveDocument.Range
'create rSlide initially as zero size. NB do NOT initialise both rNext and rSlide to the whole document range as well, since
'this non-obviously makes them both the same range, and changing one will change the other
Set rSlide = ActiveDocument.Range(0, 0)
'set up the find parameters before you loop through the document, so you don't repeat unnecessary setup on each loop:
With rDoc.Find
.ClearFormatting
.Format = False
.Wrap = wdFindStop
.Forward = True
.MatchWildcards = False
End With
Do
'find the next (or first) slide, using carriage-return number number number number
With rDoc.Find
.Execute FindText:="^p^#^#^#^#"
If .found Then
'set end of previous slide range
rSlide.End = rDoc.Start - 1
'set range of rNext to the temporary values for rDoc that result from the "find", and then extend to include 4 more characters
Set rNext = ActiveDocument.Range(rDoc.Start, rDoc.End)
rNext.End = rNext.End + 4
'grab the resulting text to variable vNext
vNext = rNext.Text
'check the sixth character and include it as part of the slide number if it's not a space, using VBA string function Mid
If Mid(vNext, 6, 1) <> Chr(32) Then
vNext = Mid(vNext, 2, 5)
Else
'otherwise just use the first four characters (omitting the carriage return at the start)
vNext = Mid(vNext, 2, 4)
End If
'if no further slide numbers found, then exit at end of loop
Else
bStop = True
'set final slide range.. assuming text is in a table, as in your example, this needs to be the end of the table cell
'if text is not in a table then use activedocument.range.end-1
rSlide.End = rSlide.Cells(1).Range.End - 2
End If
End With
'process the previous slide -
'this eliminates all the stuff with deciding how many sentences there are etc, because we are
'simply replacing the previous slide's range with new text
If rSlide.Start > 0 Then 'check start >0 so we don't try to process anything before slide 1
'work where possible with a string variable rather than a document range. MUCH faster
sText = rSlide.Text
'cut the initial number line altogether by finding a colon and carriage return
iPos = InStr(sText, ":" & Chr(13))
sText = Mid(sText, iPos + 1) ' returns everything to the right of iPos+1
'remove unwanted double carriage returns
While InStr(sText, Chr(13) & Chr(13))
sText = Replace(sText, Chr(13) & Chr(13), Chr(13))
Wend
'cut any final CR
If Right(sText, 1) = Chr(13) Then sText = Left(sText, Len(sText) - 1)
'add marker code at each text line break - replacing need to count lines etc
sText = Replace(sText, Chr(13), Chr(11) & marker_code & " ")
'assemble full text, including the sText we have already built
sText = vSlide & " : " & staticTC & Chr(11) & static_next_line & Chr(11) & marker_code & " " & _
vSlide & " : " & sText & Chr(13) & Chr(13)
'replace the text in the pre-defined range rSlide. Much quicker than using the built-in Word find and replace
rSlide.Text = sText
End If
'set new start point for previous slide's range
rSlide.Start = rSlide.End + 1
'make vNext into vSlide
vSlide = vNext
Loop Until bStop
End Sub
Sub allOff()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
End Sub
Sub allOn()
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.ScreenRefresh
End Sub
Sub RemoveParaOrLineBreaks()
'constant and variable declarations here...
'trap errors:
On Error GoTo errortrap
'turn off the screen
alloff
'code here:
'xxxxxxx
'end of code
'turn screen back on
allOn
'exit routine if all ok
Exit Sub
'handle error if not ok
errortrap:
allon
MsgBox ("An error has occurred") 'or whatever..
End Sub
If rSlide.Tables.Count = 0 Then
rSlide.End = ActiveDocument.Range.End - 1
Else
rSlide.End = rSlide.Cells(1).Range.End - 2
End If
A 50 page document with no table now processes in 7.93 seconds on my laptop. This is probably because we are saving the time overhead cased by Word adjusting the table size, even with screen updates turned off.
Premium Content
You need an Expert Office subscription to comment.Start Free Trial