0
Hello, I’m doing a macro to edit Whatsapp conversations.
When you access Whatsapp, open a conversation, tap the three points in the upper right corner, tap "More", and then tap "Send by email", the conversation comes in a ".txt" file.
I would like to copy and paste the contents of this file into Word and use this macro to format and edit this text so that it becomes more "presentable".
Input Text:
01/12/17, 14:29 - Contact: Good morning
01/12/17, 14:29 - I: Good morning, all right?
02/12/17, 15:00 - Contact: Yes
Text as I intend it to be:
01 December 2017 at 14:29
Contact: Good morning
I: Good morning, all right?
02 December 2017 at 15:00
Contact: Yes
Text as it is getting:
//, : - Contact: Good morning
//, : - Contact: Good morning
//, : - Contact: Good morning
//, : - Contact: Good morning
//, : - Contact: Good morning
//, : - Contact: Good morning
//, : - Contact: Good morning
01/12/17, 14:29 - I: Good morning, all right?
02/12/17, 15:00 - Contact: Yes
My code:
Sub ConvertWhatsAppText()
Dim lineText As String, lineResult As String
Dim aux As String, actualyDate As String
Dim mChar As String * 1
Dim i As Integer, j As Integer, p As Integer, limitC As Integer, limitP As Integer
Dim numbers As String
numbers = "0123456789"
limitP = ActiveDocument.Paragraphs.Count
p = 1
For Each singleLine In ActiveDocument.Paragraphs
If p > limitP Then
Exit For
End If
p = p + 1
lineText = singleLine.Range.Text
limitC = Len(lineText)
For i = 1 To limitC
If InStr(numbers, Mid(lineText, i, 1)) > 0 Then
mChar = Mid(lineText, i, 17)
For j = 1 To Len(mChar)
If InStr(numbers, Mid(mChar, j, 1)) > 0 And (j = 1 Or j = 2 Or j = 4 Or j = 5 Or j = 7 Or j = 8 Or j = 11 Or j = 12 Or j = 14 Or j = 15) Then
ElseIf Mid(mChar, j, 1) = "/" And (j = 3 Or j = 6) Then
ElseIf Mid(mChar, j, 1) = " " And (j = 10 Or j = 16) Then
ElseIf Mid(mChar, j, 1) = "," And (j = 9) Then
ElseIf Mid(mChar, j, 1) = ":" And (j = 13) Then
ElseIf Mid(mChar, j, i) = "-" And (j = 17) Then
aux = mChar
If Not (actualyDate = aux) Then
lineResult = lineResult & vbCrLf & FormatDate(aux) & vbCrLf
actualyDate = aux
Else
lineResult = lineResult & vbCrLf
End If
Else
lineResult = lineResult & Mid(lineText, i, 1)
Exit For
End If
Next j
Else
lineResult = lineResult & Mid(lineText, i, 1)
End If
Next i
singleLine.Range.Text = lineResult
Next singleLine
End Sub
Function FormatDate(x As String) As String
Dim month As String
Select Case Mid(x, 4, 2)
Case "01"
month = "Janeiro"
Case "02"
month = "Fevereiro"
Case "03"
month = "Março"
Case "04"
month = "Abril"
Case "05"
month = "Maio"
Case "06"
month = "Junho"
Case "07"
month = "Julho"
Case "08"
month = "Agosto"
Case "09"
month = "Setembro"
Case "10"
month = "Outubro"
Case "11"
month = "Novembro"
Case "12"
month = "Dezembro"
End Select
FormatDate = Mid(x, 1, 2) & " de " & month & " de 20" & Mid(x, 7, 2) & " às " & Mid(x, 11, 2) & "h" & Mid(x, 14, 2) & "min"
End Function
Before declaring limitP and the p counter, when I ran the code it ended in an infinite loop and crashed Word.
A further, this link has an answer with the use of Split in a UDF.
– danieltakeshi
Thank you very much! It worked perfectly!
– Rafael F N Xavier