0

The application below successfully works on our Dev/QA team machines:

using Autofac;
using Word = Microsoft.Office.Interop.Word;

namespace ComObjectIssue
{
    public interface IDateWriter
    {
        void WriteDate();
    }

    public class TodayWriter : IDateWriter
    {
        private readonly Word.Application wordApp;
        public TodayWriter(Word.Application wordApp)
        {
            this.wordApp = wordApp;
        }

        public void WriteDate()
        {
            //...
        }
    }

    public class Program
    {
        private static IContainer Container { get; set; }

        static void Main(string[] args)
        {
            var builder = new ContainerBuilder();
            var wordApp = new Word.Application();
            builder.RegisterInstance(wordApp).SingleInstance();
            builder.RegisterType<TodayWriter>().As<IDateWriter>();
            
            Container = builder.Build(); // throws "System.ArgumentException: The type 'System.__ComObject' is not assignable to service 'Microsoft.Office.Interop.Word.Application'."

            using (var scope = Container.BeginLifetimeScope())
            {
                var writer = scope.Resolve<IDateWriter>();
                writer.WriteDate();
            }

            wordApp.Quit();
        }
    }
}

But does not work for one of our customers — Autofac throws the following exception while creating container:

System.ArgumentException: The type 'System.__ComObject' is not assignable to service 'Microsoft.Office.Interop.Word.Application'.
   at Autofac.Builder.RegistrationBuilder.CreateRegistration(Guid id, RegistrationData data, IInstanceActivator activator, IResolvePipelineBuilder pipelineBuilder, Service[] services, IComponentRegistration target)
   at Autofac.Builder.RegistrationBuilder.CreateRegistration[TLimit,TActivatorData,TSingleRegistrationStyle](IRegistrationBuilder`3 builder)
   at Autofac.Builder.RegistrationBuilder.RegisterSingleComponent[TLimit,TActivatorData,TSingleRegistrationStyle](IComponentRegistryBuilder cr, IRegistrationBuilder`3 builder)
   at Autofac.ContainerBuilder.Build(IComponentRegistryBuilder componentRegistry, Boolean excludeDefaultModules)
   at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)

Reinstallation Microsoft Visual Studio 2010 Tools for Office Runtime did not help.


  • Autofac version: 6.2.0.0
  • Target: .NET Framework 4.8

Is there idea of what can happen or what should be checked else?


UPDATE

MS Word version: 16.0

Today we were able to fix the issue by running MS Word as Administrator. Unfortunately, such solution is not applicable, since the client's security policy does not allow to use Administrator permission.

I cannot reproduce it on my side — it works for me with a non-Administrator account. Looks like some certain permission is required, but what?

Adam Shakhabov
  • 1,012
  • 2
  • 12
  • 29
  • Maybe a COM registration issue? – Laurent Gabiot Nov 05 '21 at 06:40
  • Agree, looks like COM registration issue. But how it can be checked? – Adam Shakhabov Nov 05 '21 at 10:06
  • I'm sorry I'm not competent enough on this subject to really help you. This is hust a hunch. I'd say your client Office installation is causing the problem. What version of Office is it? Are there several Office versions installed? Can Office be reinstalled? But take my words with a lot of salt, I may be completely off. – Laurent Gabiot Nov 05 '21 at 11:41
  • "Are there several Office versions installed" — No. "Can Office be reinstalled?" — Unfortunately, not. I've updated the question. Please, check it. – Adam Shakhabov Nov 05 '21 at 13:27
  • The following may be helpful: https://docs.microsoft.com/en-us/dotnet/framework/app-domains/gac and https://docs.microsoft.com/en-us/dotnet/framework/app-domains/install-assembly-into-gac – user9938 Nov 05 '21 at 13:55
  • To see what's installed in the GAC for Word: %ProgramFiles(x86)%\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\gacutil -l | find /i "Microsoft" | find /i "Word" – user9938 Nov 05 '21 at 14:20
  • The following may be helpful: https://stackoverflow.com/questions/5572516/can-i-download-and-install-gacutil-exe-without-having-to-install-vs-or-the-sdk – user9938 Nov 05 '21 at 14:20
  • It might be related to why it works only for Administrator? – Adam Shakhabov Nov 05 '21 at 15:07
  • Is this on a server? – user9938 Nov 05 '21 at 17:01
  • You may consider using NuGet package: `DocumentFormat.OpenXml` or `NPOI` instead of `Microsoft.Office.Interop.Word` – user9938 Nov 05 '21 at 17:07
  • "Is this on a server?" No, it is on the client's laptop. – Adam Shakhabov Nov 05 '21 at 17:36
  • The following posts may be helpful: https://timwappat.info/post/2020/01/24/Office-Interop-Access-is-denied-office , https://community.spiceworks.com/topic/387015-office365-excel-not-working-unless-user-has-admin-rights and https://docs.microsoft.com/en-us/sysinternals/downloads/procmon – user9938 Nov 06 '21 at 04:59

0 Answers0