WordBasic object access yields casting error
i'm on it.
Seems to be the anonymous WordBasic object does not implement the IDispatch interface.
Moreover, my test with the interop assemblies (12.0)
object[] argValues = { 1 }; Application.WordBasic.GetType().InvokeMember("DisableAutoMacros", BindingFlags.InvokeMethod, null, Application.WordBasic, argValues, null, null, null);
failed with a TargetInvocationException(DISP_E_MEMBERNOTFOUND)
what kind of office wrapper works for you here? (+office version and any special)
thanks for informations *Sebastian
I was attempting to invoke the function by name, my bad. I wrote a little helper wrapper around WordBasic to illustrate how to do it correctly. Lots of help on the internet from places like: https://bytes.com/topic/c-sharp/answers/244327-idispatch-getidsfromnames to get me pointed in the right direction. I found a number of other users out there who had struggled with calling WordBasic functions so I thought I better do my part and share the implementation that worked for me. I wrapped it up in a WordBasicHelper class so that it looked very similar to Word Basic in use :-)
using System; using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using Word = Microsoft.Office.Interop.Word;
[Guid("00020400-0000-0000-c000-000000000046")] InterfaceType(ComInterfaceType.InterfaceIsIUnknown) public interface IDispatch {
}
namespace WordBasicInvokeRepro {
}
This issue can be closed and marked as not reproducible. This was totally user error on my part.
I have to programmatically open a Word template that has Auto macros in it and I need to prevent those auto macros from firing. Normally this is done using the WordBasic.DisableAutoMacros(1) call.
With other Office API wrappers you can do this:
but in NetOffice when you try to do anything with the WordBasic object you get an exception thrown:
System.InvalidCastException: 'Specified cast is not valid.'