none
Selecting visio shape and running add-in for the selected shape RRS feed

  • Question

  • Hello,

    I have created a visio add-in using vb.net. The add-in, when clicked, shows a form which is used to insert shape data rows for a selected shape.

    Process (in visio drawing):

    Drop shape on page >> Select the shape (if not selected) >> Click the add-in button >> Windows form shows up.

    This works perfectly when I drop the first shape and run the add-in. But when I drop another shape from the same master and click the add-in button, the form does not show up.

    I have this code for it:

    Public sub data()
       Dim xPS as Visio.Shape
       Dim xCT as New Form1
    
       xPS = Globals.ThisAddIn.Application.ActiveWindow.Selection.PrimaryItem
    
        If xPS Is Nothing Then
            MsgBox("ERROR: " & vbCrLf & "No 'AA' shape selected. Please select an 'AA' shape and rerun the command")
            Exit Sub
        Else
            'If a shape other than 'AA' is selected, a message box is displayed with error description.
            If Left(xPS.Name, 2) <> "AA" Then
                MsgBox("ERROR: " & vbCrLf & "Incorrect shape selected." & vbCrLf & "Please select 'AA' shape and rerun the command.")
                Exit Sub
            Else
                 xCT.Show()    
               'Shows the user form
            End If
       End If
    End Sub

    I have a feeling that I am not using the correct method to run the add-in for selected shape. Especially this syntax:

    xPS = Globals.ThisAddIn.Application.ActiveWindow.Selection.PrimaryItem

    Can someone please tell me why the form does not show up for the second shape, but it does for the first one? 

    Thanks for the help.

    - Miki


    Friday, May 8, 2015 6:38 PM

Answers

  • Hello Miki,

    To get the definitive answer you need to debug the code and see what happens under the hood. I'd suggest from breaking the chain of property and method calls and declaring each property and method call on a separate line of code. Thus, you will be able to see what each property or method call returns.  

    Friday, May 8, 2015 7:19 PM
  • Hi hgajjar,

    >> This works perfectly when I drop the first shape and run the add-in. But when I drop another shape from the same master and click the add-in button, the form does not show up.

    Did you get any confirm message like "ERROR:" after you click the add-in button in second time? Based on your code, your form will show up only in the situation of "Left(xPS.Name, 2)" equals "AA".

    >> I have a feeling that I am not using the correct method to run the add-in for selected shape.

    Selection.PrimaryItem returns the Shape object that is a Selection object's primary item. If a Selection object contains no Shape objects, or the primary Shape object is one that isn't enumerated given the Selection object's IterationMode property, the PrimaryItem property returns Nothing and raises no exception. If you have selected a shape, it will return Shape object or Nothing.

    >> Can someone please tell me why the form does not show up for the second shape, but it does for the first one?

    As the reply from Eugene, I will recommend you put breakpoints in the line "xPS = Globals.ThisAddIn.Application.ActiveWindow.Selection.PrimaryItem" to check the object of the xPS.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 13, 2015 8:44 AM

All replies

  • Hello Miki,

    To get the definitive answer you need to debug the code and see what happens under the hood. I'd suggest from breaking the chain of property and method calls and declaring each property and method call on a separate line of code. Thus, you will be able to see what each property or method call returns.  

    Friday, May 8, 2015 7:19 PM
  • Hi hgajjar,

    >> This works perfectly when I drop the first shape and run the add-in. But when I drop another shape from the same master and click the add-in button, the form does not show up.

    Did you get any confirm message like "ERROR:" after you click the add-in button in second time? Based on your code, your form will show up only in the situation of "Left(xPS.Name, 2)" equals "AA".

    >> I have a feeling that I am not using the correct method to run the add-in for selected shape.

    Selection.PrimaryItem returns the Shape object that is a Selection object's primary item. If a Selection object contains no Shape objects, or the primary Shape object is one that isn't enumerated given the Selection object's IterationMode property, the PrimaryItem property returns Nothing and raises no exception. If you have selected a shape, it will return Shape object or Nothing.

    >> Can someone please tell me why the form does not show up for the second shape, but it does for the first one?

    As the reply from Eugene, I will recommend you put breakpoints in the line "xPS = Globals.ThisAddIn.Application.ActiveWindow.Selection.PrimaryItem" to check the object of the xPS.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 13, 2015 8:44 AM