I am exploring using NetOffice.Visio for my project VisioAutomation: https://github.com/saveenr/VisioAutomation
Here's some code which works with Microsoft.Office.Interop.Visio
// using IVisio = Microsoft.Office.Interop.Visio;
var app = new IVisio.Application();
var doc = app.Documents.Add("");
var page = app.ActivePage;
var shape = page.DrawRectangle(0, 0, 2, 3);
shape.Text = "With Microsoft.Office.Interop.Visio";
var SID_SRCStream = new short[4];
SID_SRCStream[0] = (short)shape.ID16;
SID_SRCStream[1] = (short)IVisio.VisSectionIndices.visSectionObject;
SID_SRCStream[2] = (short)IVisio.VisRowIndices.visRowFill;
SID_SRCStream[3] = (short)IVisio.VisCellIndices.visFillForegnd;
System.Array a;
page.GetFormulasU(SID_SRCStream, out a);
But the same code throws an exception with NetOffice.Visio (for .NET 4.0)
// using IVisioNetOffice = NetOffice.VisioApi;
var app = new IVisioNetOffice.Application();
var doc = app.Documents.Add("");
var page = app.ActivePage;
var shape = page.DrawRectangle(0, 0, 2, 3);
shape.Text = "With NetOffice";
var SID_SRCStream = new short[4];
SID_SRCStream[0] = (short)shape.ID16;
SID_SRCStream[1] = (short)IVisioNetOffice.Enums.VisSectionIndices.visSectionObject;
SID_SRCStream[2] = (short)IVisioNetOffice.Enums.VisRowIndices.visRowFill;
SID_SRCStream[3] = (short)IVisioNetOffice.Enums.VisCellIndices.visFillForegnd;
object[] a;
page.GetFormulasU(SID_SRCStream, out a);
The exception occurs in GetFormulas the inner exception says "{"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"}".
I suspect the problem is with the second parameter because setformulas seems to work with the same SID_SRCStream.
Any ideas what is wrong in my code?
Comments: using System; using System.Runtime.InteropServices; using System.Reflection; using System.Runtime.CompilerServices; using Visio = NetOffice.VisioApi; namespace Foo // change to what you want/need { public static class IVPageExtensions { [DefaultMember("Name"), Guid("000D0709-0000-0000-C000-000000000046"), TypeLibType(4176)] [ComImport] private interface IVPageVTable { [DispId(32)] [MethodImpl(MethodImplOptions.InternalCall)] void GetFormulas([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_I2)] [In] ref Array SID_SRCStream, [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)] out Array formulaArray); } public static void GetFormulas(this Visio.IVPage page, Int16[] sID_SRCStream, out object[] formulaArray) { formulaArray = null; Array arg1 = sID_SRCStream as Array; if(null == arg1) arg1 = new Array[0]; IVPageVTable proxy = page.UnderlyingObject as IVPageVTable; if (null != proxy) { Array formulas = null; proxy.GetFormulas(ref arg1, out formulas); if (null != formulas) { formulaArray = new object[formulas.Length]; for (int i = 0; i < formulas.Length; i++) formulaArray[i] = formulas.GetValue(i); } } else throw new InvalidCastException("Unable to cast underlying proxy into interop interface"); } } }
Here's some code which works with Microsoft.Office.Interop.Visio
// using IVisio = Microsoft.Office.Interop.Visio;
var app = new IVisio.Application();
var doc = app.Documents.Add("");
var page = app.ActivePage;
var shape = page.DrawRectangle(0, 0, 2, 3);
shape.Text = "With Microsoft.Office.Interop.Visio";
var SID_SRCStream = new short[4];
SID_SRCStream[0] = (short)shape.ID16;
SID_SRCStream[1] = (short)IVisio.VisSectionIndices.visSectionObject;
SID_SRCStream[2] = (short)IVisio.VisRowIndices.visRowFill;
SID_SRCStream[3] = (short)IVisio.VisCellIndices.visFillForegnd;
System.Array a;
page.GetFormulasU(SID_SRCStream, out a);
But the same code throws an exception with NetOffice.Visio (for .NET 4.0)
// using IVisioNetOffice = NetOffice.VisioApi;
var app = new IVisioNetOffice.Application();
var doc = app.Documents.Add("");
var page = app.ActivePage;
var shape = page.DrawRectangle(0, 0, 2, 3);
shape.Text = "With NetOffice";
var SID_SRCStream = new short[4];
SID_SRCStream[0] = (short)shape.ID16;
SID_SRCStream[1] = (short)IVisioNetOffice.Enums.VisSectionIndices.visSectionObject;
SID_SRCStream[2] = (short)IVisioNetOffice.Enums.VisRowIndices.visRowFill;
SID_SRCStream[3] = (short)IVisioNetOffice.Enums.VisCellIndices.visFillForegnd;
object[] a;
page.GetFormulasU(SID_SRCStream, out a);
The exception occurs in GetFormulas the inner exception says "{"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"}".
I suspect the problem is with the second parameter because setformulas seems to work with the same SID_SRCStream.
Any ideas what is wrong in my code?
Comments: using System; using System.Runtime.InteropServices; using System.Reflection; using System.Runtime.CompilerServices; using Visio = NetOffice.VisioApi; namespace Foo // change to what you want/need { public static class IVPageExtensions { [DefaultMember("Name"), Guid("000D0709-0000-0000-C000-000000000046"), TypeLibType(4176)] [ComImport] private interface IVPageVTable { [DispId(32)] [MethodImpl(MethodImplOptions.InternalCall)] void GetFormulas([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_I2)] [In] ref Array SID_SRCStream, [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)] out Array formulaArray); } public static void GetFormulas(this Visio.IVPage page, Int16[] sID_SRCStream, out object[] formulaArray) { formulaArray = null; Array arg1 = sID_SRCStream as Array; if(null == arg1) arg1 = new Array[0]; IVPageVTable proxy = page.UnderlyingObject as IVPageVTable; if (null != proxy) { Array formulas = null; proxy.GetFormulas(ref arg1, out formulas); if (null != formulas) { formulaArray = new object[formulas.Length]; for (int i = 0; i < formulas.Length; i++) formulaArray[i] = formulas.GetValue(i); } } else throw new InvalidCastException("Unable to cast underlying proxy into interop interface"); } } }