0

I am writing a VSTO Application in Visual Studio for Outlook in C#, which pastes an HTML template on top of an e-mail.

For 'cleanliness' I wanted the content of the HTML File to be separate and not inside the code block, therefore the HTML file is stand alone and embedded within the resource. I then use the following code to fetch the content and write it to my email:

    private void Noteduedate()
    {
        //reflection magic to get the content of an embedded file
        //kindly appropriated from:
        //https://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file/3314213#3314213

        Assembly _assembly;
        StreamReader _textStreamReader;

        try
        {
            _assembly = Assembly.GetExecutingAssembly();
            _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.HTML_table_template.html"));
            HTML_template = _textStreamReader.ReadToEnd();
        }
        catch
        {
            MessageBox.Show("Error accessing resources!");
        }

        //read the Mail and proceed to add due date
        Outlook.Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer();
        if (explorer != null)
        {
            Object obj = explorer.Selection[1];
            if (explorer.Selection.Count == 1)
            {
                if (obj is Outlook.MailItem)
                {
                    Outlook.MailItem mailitem = (obj as Outlook.MailItem);
                    string bodycontent;
                    bodycontent = mailitem.HTMLBody;
                    mailitem.HTMLBody = HTML_template + bodycontent;
                }
            }
        }
    }

Thus leaving me with the problem, that I do not know, how to insert the variable {date_lt.Value} from another object I need to be visible in the final result, into the HTML code:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
        <table cellpadding="0" cellspacing="0" width="640" align="center" border="1">
            <tr>
                <td>
                    <table cellpadding="0" cellspacing="0" width="318" align="left" border="1">
                        <tr>
                            <td>{date_lt.Value}</td>
                        </tr>
                    </table>
                    <table cellpadding="0" cellspacing="0" width="318" align="left" border="1">
                        <tr>
                            <td>{date_kf.Value}</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
</body>
</html>

Any help or ideas on how to do it better are highly appreciated, Thanks!

Cheesecake
  • 11
  • 1

1 Answers1

1

To Answer my own question, I've built a method to replace the string within the string. Solves the issue.

    private string ContentReplacer(string input)
    {

        StringBuilder builder = new StringBuilder(input);
        builder.Replace("{date_lt.Value}", date_lt.Text);
        builder.Replace("{date_wv.Value}", date_wv.Text);

        string output = builder.ToString(); // contains the cleaned up output including dynamic values
        return output;
    }
Cheesecake
  • 11
  • 1
  • Hi, glad to know you've found the solution to resolve this issue! Please consider accepting it as answer to change its status to Answered. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – Jingmiao Xu-MSFT Feb 28 '22 at 01:56