0

I made an excel macro to send multiple emails at once based off user data. I changed the .body to .htmlbody to allow the use of bolding, etc.; however, when I call the email signature, it's placing  before the actual signature in the email. I found out that it's due to UTF-BOM encoding, however, in this instance, I'm not able to simply update the encoding of the file. I need the script to actually open the file with the updated (UTF-8) encoding, but not altering the file at all. Here's my code so far. I left most of the code out since I believe the only update will be done on the "GetBoiler" function.

Dim SigString As String
Dim Signature As String


SigString = Environ("appdata") & _
    "\Microsoft\Signatures\Default English - Main.htm"

If Dir(SigString) <> "" Then
    Signature = GetBoiler(SigString)
Else
    Signature = ""
End If

Function GetBoiler(ByVal sFile As String) As String
    Dim fso As Object
    Dim ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
    GetBoiler = ts.readall
    ts.Close
End Function
Tom
  • 13
  • 1
  • 3
  • *Welcome to [so]!* See "[ask]" as well as **how to create a [mcve]** (yours isn't). Also, here's a "perfect question" [checklist](//codeblog.jonskeet.uk/stack-overflow-question-checklist/) from the sites' top user. – ashleedawg Aug 31 '18 at 18:07
  • @ashleedawg I added all the information needed... I just need to know how to open a file with a different encoding.... – Tom Aug 31 '18 at 18:10
  • 1
    See the links provided (specifically [mcve]) to see what should/shouldn't be included in an example, so that others can reproduce your problem. If you "just need to know how to open a file with a different encoding", that should be the focus of the question and only *relevant code* to that should be included. – ashleedawg Aug 31 '18 at 18:18
  • @ashleedawg Your answers are not helpful. I said in my question exactly what is occurring, with the code that is producing the issue. I explained the exact issue and what I believe would need to be fixed, but added the code in the event that a simple change is needed. How is that incomplete in any way? If you can't provide a solution, then why are you commenting? – Tom Aug 31 '18 at 18:23
  • 2
    At a start, what you are doing with `Signature` would be helpful to know. The idea behind a [mcve] is that people can easily recreate the problem. It's also a good exercise for you to go through, as it can often narrow down a problem and save you having to post a question at all! – miken32 Aug 31 '18 at 18:36
  • For starters, you have told us the contents of your signature and two you left us in the dark about what `getBoiler` is/where you found it. There’s no need to be rude. – Marcucciboy2 Aug 31 '18 at 18:41
  • @ashleedawg is among the site's [top answerers](https://stackoverflow.com/tags/vba/topusers) in the VBA tag, I wouldn't dismiss their answers as "unhelpful" that fast; they're trying to help you shape up your question so that it's useful to everyone that comes across a similar problem, which is exactly what this site is about (SO isn't "please help *me* debug *my* code" - take the [tour] to see for yourself), and ensures your questions are well-received so that you enjoy your SO experience. Please [be nice](https://stackoverflow.com/help/behavior), it makes a better Internet for everyone. – Mathieu Guindon Aug 31 '18 at 18:58
  • Also, the BOM should not appear in the middle of text, only before it. It is not part of the text. It is metadata. Therefore, it shouldn't even appear in a `String`. If your method of converting bytes to `String` copies it in, get rid of it in the same function. – Tom Blodget Aug 31 '18 at 23:47

1 Answers1

1

FSO can't read UTF-8

From the documentation:

The FSO can read only ASCII text files. You cannot use the FSO to read Unicode files or to read binary file formats such as Microsoft Word or Microsoft Excel.

Since you got weird characters, I guess that's somewhat incorrect and the file was read in some 8-bit windows code page because if it really could read only ASCII, you would have seen ????

Anyway, if you can use ADO, you can do this:

Dim objStream, strData

Set objStream = CreateObject("ADODB.Stream")

objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile("C:\Users\admin\Desktop\ArtistCG\folder.txt")

strData = objStream.ReadText()

objStream.Close
Set objStream = Nothing

(Source)


Outlook Signatures

ashleedawg
  • 20,365
  • 9
  • 72
  • 105