C# Development in Visio 2007

W

WPfeffer

I am trying to capture events in a C# AddIn and am currently having some
difficulty. I am able to trap the 'ShapeAdded' event and manipulate things at
that point, but now I want to trap the event that is fired when a new master
is added to a Visio Stencil. I have set up a trap for the 'MasterAdded' event
(visEvtAdd + visEvtMaster), but it doesn't appear to fire when I add a shape
to the stencil. (I'm doing this by dragging the shape from the Visio drawing
page and dropping it on the stencil.) I have also tried trapping the
'EnterScope' event and nothing happens. All that I am doing in the trap code
is displaying a dialog telling me that the event fired (for now).
 
J

JuneTheSecond

Did you trap MasterAdded events to the stencil?
for simplicty, i have tested the event in VBA.
Option Explicit

Private WithEvents myDoc As Visio.Document

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
Set myDoc = Application.Documents(2)
End Sub

Private Sub Document_DocumentSaved(ByVal doc As IVDocument)
Set myDoc = Application.Documents(2)
End Sub

Private Sub myDoc_MasterAdded(ByVal Master As IVMaster)
Debug.Print Master.Name
End Sub
 
W

WPfeffer

I'm not sure how to trap the events at the stencil from within an AddIn. I
thought that when an AddIn is loaded, it has control over all open document
windows, including the .vss stencil files.

Inside of the Connect.Onconnection I have the following code:


public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array
custom)
{
vsoApplication =
(Microsoft.Office.Interop.Visio.Application)application;
vsoApplication.Documents.Open(@"C:\Documents and
Settings\wep85069\My Documents\test.vsd");
vsoApplication.Documents.OpenEx(@"C:\Documents and
Settings\wep85069\My Documents\My Shapes\Vision.vss",
(short)VisOpenSaveArgs.visOpenRW + (short)VisOpenSaveArgs.visOpenDocked);
addInInstance = addInInst;

// Listen to the marker events.
EventList applicationEventList = vsoApplication.EventList;
markerEvent =
applicationEventList.AddAdvise((short)VisEventCodes.visEvtApp +
(short)VisEventCodes.visEvtMarker, applicationEventSink, "", "");
shapeAddedEvent =
applicationEventList.AddAdvise((short)(-(int)VisEventCodes.visEvtAdd) +
(short)VisEventCodes.visEvtShape, applicationEventSink, "", "");
documentAddedEvent =
applicationEventList.AddAdvise((short)(-(int)VisEventCodes.visEvtAdd) +
(short)VisEventCodes.visEvtDoc, applicationEventSink, "", "");
masterAddedEvent =
applicationEventList.AddAdvise((short)(-(int)VisEventCodes.visEvtAdd) +
(short)VisEventCodes.visEvtMaster, applicationEventSink, "", "");
enterScopeEvent =
applicationEventList.AddAdvise((short)VisEventCodes.visEvtCodeEnterScope,
applicationEventSink, "", "");
selectionAddedEvent =
applicationEventList.AddAdvise((short)VisEventCodes.visEvtCodeSelAdded,
applicationEventSink, "", "");
documentChangedEvent =
applicationEventList.AddAdvise((short)VisEventCodes.visEvtMod +
(short)VisEventCodes.visEvtDoc, applicationEventSink, "", "");
}

and then inside the EventSink.IVisEventProc.VisEventProc I have the following:


object IVisEventProc.VisEventProc(short eventCode, object source,
int eventID, int eventSeqNum, object subject, object moreInfo)
{
Shape shapeSubject = (Shape)subject;

try
{
// Determine which event fired from the Event Code.
switch (eventCode)
{
case (short)(-(int)VisEventCodes.visEvtAdd) +
(int)VisEventCodes.visEvtShape:
System.Windows.Forms.MessageBox.Show("Shape added to
page.");
break;
case (short)(-(int)VisEventCodes.visEvtAdd) +
(int)VisEventCodes.visEvtMaster:
System.Windows.Forms.MessageBox.Show("Master added
to stencil.");
break;
case (int)VisEventCodes.visEvtApp +
(short)VisEventCodes.visEvtMarker:
System.Windows.Forms.MessageBox.Show("Marker event
fired.");
break;
case (short)VisEventCodes.visEvtCodeEnterScope:
System.Windows.Forms.MessageBox.Show("Enter Scope
event fired.");
break;
case (short)(-(int)VisEventCodes.visEvtAdd) +
(int)VisEventCodes.visEvtDoc:
System.Windows.Forms.MessageBox.Show("Enter
DocumentAdded event.");
break;
case (short)VisEventCodes.visEvtCodeSelAdded:
System.Windows.Forms.MessageBox.Show("Enter
SelectionAdded event.");
break;
case (short)VisEventCodes.visEvtMod +
(short)VisEventCodes.visEvtDoc:
System.Windows.Forms.MessageBox.Show("Enter
DocumentChanged event.");
break;
}
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine(err.Message);
}

return null;
}

The only event that seems to be being reliably trapped is the ShapeAdded
event. What am I doing wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top