Reputation: 3
So I'm trying to format many texts on MS Word, and these texts come from a website.
The problem is that when I paste it on Word, breaks are not included. So, I have a huge paragraph with every parts stuck altogether.
I managed to separate most of the parts by using the function Find and Replace, but one problem remains :
Each time there is a lowercase character directly followed by an uppercase character, I need to insert a break between them.
For instance : Roses are redViolets are blue.
I tried to write a macro on VBA but it doesn't work, and returns me "Error 424 : Object Required". At this point, I haven't practiced any VBA for years, and I'm lost. If someone could help me, that would be great !
Here's what I wrote :
Dim doc As Word.Document
Set doc = ActiveDocument
Dim string1 As String
Dim Char1 As String
Dim Char2 As String
With doc.Range
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Text = "[a-z][A-Z]"
.Execute
If Find.Found = 1 Then
string1 = Selection.Text
Char1 = Left(string1, 1)
Char2 = Right(string1, 1)
string1 = Char1 + vbNewLine + vbNewLine + Char2
End If
.Replacement.Text = string1
End With
End With
Thank you !
Upvotes: 0
Views: 84
Reputation: 51
I was able to achieve what you are looking for with the below mentioned code. Please see if it works for you too.
Sub Find_Replace()
Dim string1 As String
Dim Char1 As String
Dim Char2 As String
Do While ActiveDocument.Range.Find.Execute("[a-z][A-Z]", , , True)
With Selection.Find
.Text = "[a-z][A-Z]"
.Wrap = wdFindContinue
.Forward = True
.MatchWildcards = True
.Execute
If .Found Then
string1 = Selection.Text
Char1 = Left(string1, 1)
Char2 = Right(string1, 1)
string1 = Char1 + vbNewLine + vbNewLine + Char2
End If
.Execute ReplaceWith:=string1
End With
Loop
MsgBox "Task Complete"
End Sub
Upvotes: 1