How to speed up Excel and Word 2003,2007 and 2010 launch times when using Automation in C#

One of the biggest impacts on the launch time for Office is the speed at which all of the addins load. Unfortunately, poorly written or unnecessary addins add a startup cost which isn’t something you want to have to pay when loading Office via automation.
I wrote a nice helper class that allows you save the pain of loading them, without impacting the users experience.
Here is the class:

[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Win32;
 
namespace Seekford.Office
{
    public class OfficeAddinManagementUtility : IDisposable
    {
        private Dictionary<string, int > wordLoadBehaviorStorage = new Dictionary<string, int >();
        private Dictionary<string ,int > excelLoadBehaviorStorage = new Dictionary<string, int >();
 
        private const string _wordAddinRootKey = @"SoftwareMicrosoftOfficeWordAddins";
        private const string _excelAddinRootKey = @"SoftwareMicrosoftOfficeExcelAddins";
        private const string _loadString = "LoadBehavior";
 
        public void DisableAllWordAddins()
        {
            if (wordLoadBehaviorStorage.Any()) return;//unbalanced?
            DisableAllAddinsInHive(_wordAddinRootKey, wordLoadBehaviorStorage);
        }
 
        public void DisableAllExcelAddins()
        {
            if (excelLoadBehaviorStorage.Any()) return;//unbalanced?
            DisableAllAddinsInHive(_excelAddinRootKey, excelLoadBehaviorStorage);
        }
 
 
        private void DisableAllAddinsInHive(string hiveLocation, Dictionary<string, int > storage)
        {
            var userKey = Registry.CurrentUser.OpenSubKey(hiveLocation);
            if (userKey != null)
            {
                var subKeys = userKey.GetSubKeyNames();
                if (subKeys != null)
                {
                    foreach (var s in subKeys)
                    {
                        var subKey = userKey.OpenSubKey(s, true);
                        int val = (int)subKey.GetValue(_loadString, 0);
                        if (val != 0)
                        {
                            subKey.SetValue(_loadString, 0);
                            storage.Add(s, val);
                        }
                    }
                }
            }
        }
 
        private void RevertAllAddinsInHive(string hiveLocation, Dictionary<string, int > storage)
        {
            var userKey = Registry.CurrentUser.OpenSubKey(hiveLocation);
            if (userKey != null)
            {
                var subKeys = userKey.GetSubKeyNames();
                if (subKeys != null)
                {
                    foreach (var s in subKeys)
                    {
                        if (storage.ContainsKey(s))
                        {
                            var subKey = userKey.OpenSubKey(s, true);
                            if(subKey != null)
                            {
                                subKey.SetValue(_loadString, storage[s]);
                                storage.Remove(s);
                            }
                        }
                    }
                }
            }
        }
 
        public void RevertAllAddins()
        {
            RevertWordAddin();
            RevertExcelAddin();
        }
 
        public void RevertWordAddin()
        {
            if(wordLoadBehaviorStorage.Any())
                RevertAllAddinsInHive(_wordAddinRootKey, wordLoadBehaviorStorage);
        }
 
        public void RevertExcelAddin()
        {
            if(excelLoadBehaviorStorage.Any())
                RevertAllAddinsInHive(_excelAddinRootKey, excelLoadBehaviorStorage);
        }
       
        public void Dispose()
        {
            RevertAllAddins();
        }
    }
} 
[/csharp]

See how easy that is.

Just wrap your Word or Excel code, call the right methods and away you go.

[csharp]
//code below is a snippet showing you to disable addins, create your automated word,
// then reenable the addins for future word users
// use the excel methods for excel addins.
               var addinManager = new OfficeAddinManagementUtility();
                try
                {
                        addinManager.DisableAllWordAddins();
			myWord = new Word();
                }
                finally
                {
                    addinManager.RevertWordAddin();
                    addinManager.Dispose();//same as above, but lets comply with regs
                }
                return myWord;
[/csharp]

The utility disables all the Office Addins and then enables them back for the next launch of Office.

It is an easy to use C# class for disabling Word 2007/2010 and Excel 2007/2010 Addins. Should work with 2003 as well.

Happy Coding!

I can’t drop a database user: The user owns one or more databases

This was frustrating me a bit, as I wish SQL Management Studio would just prompt me to remove myself as owner from those db’s and let me drop. Nope. It doesn’t.

So, what to do? Well, I used management studio to select another one of my accounts. I get the awesome message of “The proposed new database owner is already a user or aliased in the database.”. So, no go there.

I then realized I never mapped SA to these db. Nice and simple. Assign each db owner to SA, then your done.

Or run this script and keep changing the db name. Probably could automate this more by querying the db’s and looping.

 

alter authorization on DATABASE::[MyDBName] to [SA]

Hope this helps someone.

Happy Coding!