I’ve created a macro in Excel 365, using VBA.
It’s function is to loop through some e-mails stored in Outlook 365 and save each one to a .msg file and a .html file.
So far it is working fine, except the html files are being written with illegal characters.
The html file itself is created correctly with UTF-8 encoding, as checked with visual studio code, but the meta tag shows:
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
Also, when a special character like “á” is present in the e-mail, it’s saved instead, with a character like this “�”
I don’t know if it’s relevant, but my computer’s language is set to “Portuguese (Brazil)”
I’ve tried:
- Finding a way to change the command “.saveAs” encoding to UTF-8 or Unicode, but it seems to only exist for text files, not e-mails.
- Save the the body of the e-mail to a database directly, without creating a html file, but I need images to be present in the same way showed in outlook, so it didn’t work either.
Here is the relevant code used:
Sub loopEmail(ByVal emails As outlook.Items)
Dim email As Object
Dim qtdEmails As Integer: qtdEmails = 1
'Loops through each e-mail
Dim i As Integer
For i = emails.Count To 1 Step -1
Set email = emails(i)
'Verify if it's an email
If TypeOf email Is outlook.MailItem Then
qtdEmailsTotal = qtdEmailsTotal + 1
qtdEmails = qtdEmails + 1
'Saves ".msg" file
email.SaveAs "c:testemail.msg", olMSGUnicode
'Salves ".html" file
email.SaveAs "c:testemail.html", olHTML
End If
Next
Set email = Nothing
End Sub