Search code examples
outlookvstooutlook-addinadd-inoffice-addins

Escaping ampersand in a path to Outlook VSTO add-in


I have a VSTO add-in for Outlook. It is loaded by a helper add-in through code by manually adding relevant values to the registry, contacting Outlook to launch the add-in and then cleaning the registry.

It happened so that for one of the customers the path to the add-in contained an ampersand (&) symbol. This prevents the add-in from being loaded with the following exception:

System.ArgumentException: Value does not fall within the expected range.
  at Microsoft.VisualStudio.Tools.Applications.Deployment.ClickOnceAddInDeploymentManager.GetManifests(TimeSpan timeout)
  at Microsoft.VisualStudio.Tools.Applications.Deployment.ClickOnceAddInDeploymentManager.InstallAddIn()

After some research I have found that the ampersand in the path was to blame. I am looking for a way to overcome this limitation. I've created a simple empty test add-in and tried to install it via the *.vsto file that is located in a folder with ampersand symbol in the path it succeeded. Outlook loaded the add-in just fine. So I know there is definitely a way how VSTO does it.

I've tried escaping the path to manifest using && or &amp ; but it didn't help. Is there some other way I can approach this? Here is a sample of the way I am loading the add-in from the code:

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Office\Outlook\Addins", true))
{
    key.DeleteSubKey("MyAddIn", false);
    using (RegistryKey addinKey = key.CreateSubKey("MyAddIn"))
    {
        addinKey.SetValue("Manifest", $"{ new Uri(addInPath).AbsoluteUri) }|vstolocal");
        addinKey.SetValue("Description", "My Outlook Addin");
        addinKey.SetValue("FriendlyName", "My Outlook Addin");
        addinKey.SetValue("LoadBehavior", 0);
    }

    _outlookApplication.COMAddIns.Update();

    key.DeleteSubKey("MyAddIn", false);
}

foreach (COMAddIn comAddin in _outlookApplication.COMAddIns)
{
    if (comAddin.Description == "My Outlook Addin")
    {
        ConnectAddIn(comAddin);

        break;
    }
}

Solution

  • Try to url encode (%26) the ampersand.