0

I'm currently trying to create and save an Outlook signature.
The problem is, that it is always saved in a kind of 'default' encoding.
Trying to change this in multiple places to UTF-8 (!) programmatically does not seem to work.

I assume the correct place would only be the change to the WebOptions as the other encodings are already set to UTF-8 upon inspecting the previous value.

Is there actually a way to change the encoding and also let Outlook save it in that way ?

This is a sample to demonstrate the problem:

using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;

namespace CreateSignature
{
    class TestCreation
    {
        static void Main(string[] args)
        {
            Create();
        }

        public static void Create()
        {
            const string _LineBreak = "\v";

            string signature = string.Empty;

            Application word = new Application();
            var document = word.Documents.Add();
            var selection = word.Selection;

            word.Options.DefaultTextEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
            document.WebOptions.Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;

            selection.Font.Bold = 0;
            selection.Font.Name = "Verdana";
            selection.Font.Size = 9;
            selection.Font.Color = WdColor.wdColorBlack;

            selection.TypeText("Best regards");
            selection.TypeText(_LineBreak);

            selection.TypeText("Special text ### üäöß····· ###");
            selection.TypeText(_LineBreak);

            document.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
            document.Saved = true;

            var signatureName = "Test.Signature";

            var emailOptions = word.EmailOptions.EmailSignature;

            emailOptions.EmailSignatureEntries.Add(signatureName, document.Range());

            //emailOptions.NewMessageSignature = signatureName;
            //emailOptions.ReplyMessageSignature = signatureName;

            word.Quit(WdSaveOptions.wdSaveChanges);
            Marshal.ReleaseComObject(word);
        }
    }
}

The .htm file will contain this

<meta http-equiv=Content-Type content="text/html; charset=windows-1252">

where it should be utf-8 instead of windows-1252.

It seems to be smiliar to this question, though this is not a loaded document and the suggested change does not work anyway (Fields.Count == 0).

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Blacktempel
  • 3,935
  • 3
  • 29
  • 53
  • Which .htm file? – PepitoSh Jun 08 '18 at 05:29
  • @PepitoSh One of the files being created when the signature is added to Outlook via `EmailSignatureEntries.Add`. The path would be `%appdata%\Microsoft\Signatures`. – Blacktempel Jun 08 '18 at 05:30
  • Unless you have to have a general purpose solution, I'd recommend just editing that .htm file. – PepitoSh Jun 08 '18 at 05:32
  • @PepitoSh The files are generated on each login for a few hundred users. I would rather not edit it manually. – Blacktempel Jun 08 '18 at 05:39
  • Is it mandated to use Word? – PepitoSh Jun 08 '18 at 05:57
  • @PepitoSh It has to create the mail signatures in a specified format. The data to write in there is being read from multiple locations (AD, DB...). I guess Word was the best choice a while ago. – Blacktempel Jun 08 '18 at 06:05
  • I ran your code and created a signature with Chinese characters included, that obviously cannot be covered by windows-1252. The result was fine, it displayed the text properly, though the encoding was still windows-1252. Go figure... – PepitoSh Jun 08 '18 at 06:34

0 Answers0