Visio Guy

Visio Discussions => Programming & Code => Topic started by: CoreBuild on October 23, 2014, 05:25:51 AM

Title: Pass a shape to a COM object from VBA
Post by: CoreBuild on October 23, 2014, 05:25:51 AM
I have a custom stencil that does a lot of work to manipulate shapes.  I've designed several shapes and today my VBA pops up a UI and prompts the user for information and then updates the shape sheet.

I'd like to move the UI into a .NET object that I call from the VBA macro that is launched when the shape is double clicked.  In order to do this, my understanding is that I'll need to have a .NET class registered as a COM object.  I think I can handle that.

My question is how should I pass the shape to the COM object.  Does anyone know what the function prototype would look like that receives the shape?  And what the corresponding VBA would look like to pass the shape as input?  Lastly, can you confirm that it's possible to change the shape sheet while in the .NET code?

Thanks!
Title: Re: Pass a shape to a COM object from VBA
Post by: Jumpy on October 23, 2014, 01:54:56 PM
That sound's affuly complicated (having a mix from VBA and .NET Code I mean).
Why do you want to use a .NET UI?

Maybe it would be better to write a complete AddOn or AddIn that handles all the code?

Title: Re: Pass a shape to a COM object from VBA
Post by: CoreBuild on October 23, 2014, 02:22:35 PM
The reason that I want to do this is that I want launch a UI to modify the shape's data when I add it to Page or when a user double-clicks it. Today I do this with VBA using a macro from the Stencil that is launched when the shape is added.  I want to move the UI code to .NET so that I can introduce revision quickly rather than mess with VBA code, which I find a bit fragile.

So, I was wondering if I could send the shape to a .NET COM object.  I'm not sure if an AddIn or AddOn would allow me to get this same functionality.


Title: Re: Pass a shape to a COM object from VBA
Post by: Jumpy on October 24, 2014, 08:17:07 AM
I'm not an expert in this, but I think you usually don't introduce a .Net-UI or .Net-Program as a COM object in Visio. It's the other way round. Afaik you launch an AddOn with RunAddOn/RunAddOnWargs from the ShapeSheet and in that AddOn you introduce Visio as a COM object. So the AddOn can use Visio's API to manipulate shapes and so on.

Edit: Above is slightly wrong. What I said is only true for AddOns. Introducing another program via COM in Visio makes that other program an AddIn, which is a possible way of interacting with Visio, too.

Here is the link to a document, that has some information on the subject:

http://msdn.microsoft.com/en-us/library/office/aa168138%28v=office.11%29.aspx
Title: Re: Pass a shape to a COM object from VBA
Post by: Nikolay on October 24, 2014, 09:00:19 AM
Shape is already a COM object itself, so you can just pass it.. Am I missing something?

C#:

using Microsoft.Office.Interop.Visio;

[ComVisible(true)]
public class MyDotNetComponent
{
  public void DoStuffWithShape(Shape shape)
  {
     // do stuff with shape
  }
}


VBA:

Dim shp as Shape
Set shp = ...

Set myComp = new MyDotNetComponent()
myComp.DoStuffWithShape shp
Title: Re: Pass a shape to a COM object from VBA
Post by: Visio Guy on October 24, 2014, 09:58:43 PM
Sitting on my couch, I think Nikolay is right, this should work 'for free'.

Of course, your .NET project will need to know about Visio via a reference to the type library.

This is a common pattern. Visimation used to make a template with a bare-minimum of VBA functions that would sendmshp, page, shp, page, shp, window etc. to a standard .NET interface so they could do all of the real coding in .NET, but get it running in Visio process space via VBA (which is fast).