Search code examples
c#office-interopvisio

c# Visio Interop Shape.Linetype valid values


I am still busy creating flow charts in Visio using C# and Interop. Now I have the need to change the LineStyle, of a connector, in a special situation. More specifically I need to get rid of the "To-arrow" on the connector. I have searched for a list of valid values for the LineStyle property, but without success. The closest I have come is to set it to "Guide" which gets me a dotted line with no arrows, whereas "Solid" or "Solid line" yields an exception error. Does anybody know where to find the possible values of LineStyle, or can somebody tell me the value to use for a solid line without arrows.


Solution

  • To set a Style for a shape the document needs to contain a style of that name, so first you'd need to create or import a custom style. You could then apply that style by setting as per the last line in the code below.

    However, if you just want to set individual formatting properties then you can address the ShapeSheet cells directly as per the main code below.

    In general I would be tempted to put this kind of logic into the connector master and map this to some Shape Data in the shape but programmatic access is fine too.

    If you're not familiar with the ShapeSheet, you might find this video that I did a few years ago useful: https://visualsignals.typepad.co.uk/vislog/2016/04/new-visio-training-videos.html

    void Main()
    {
        var vApp = MyExtensions.GetRunningVisio();
        // See this link for more details on GetRunningVisio():
        // https://visualsignals.typepad.co.uk/vislog/2015/12/getting-started-with-c-in-linqpad-with-visio.html
    
        var connShp = vApp.ActivePage.Drop(vApp.ConnectorToolDataObject, 1.75, 11.0);
    
        // Either set cells using SRC (Section, Row, Column) syntax
        connShp.CellsSRC[(short)Visio.VisSectionIndices.visSectionObject,
                         (short)Visio.VisRowIndices.visRowXForm1D,
                         (short)Visio.VisCellIndices.vis1DBeginY].ResultIU = 11.0;
        connShp.CellsSRC[(short)Visio.VisSectionIndices.visSectionObject, 
                         (short)Visio.VisRowIndices.visRowXForm1D,
                         (short)Visio.VisCellIndices.vis1DEndX].ResultIU = 2.0;
        connShp.CellsSRC[(short)Visio.VisSectionIndices.visSectionObject,
                         (short)Visio.VisRowIndices.visRowXForm1D,
                         (short)Visio.VisCellIndices.vis1DEndY].ResultIU = 10.5;
        
        // ...or by Cell name syntax
        // connShp.CellsU["BeginY"].ResultIU = 11.0;
        // connShp.CellsU["EndX"].ResultIU = 2.0;
        // connShp.CellsU["EndY"].ResultIU = 10.5;
        
        // Set appropriate cells in the 'Line Format' section
        connShp.CellsU["LinePattern"].FormulaU = "=2"; // 2 = dotted
        connShp.CellsU["EndArrow"].FormulaU = "=6"; // 6 = 'Outdented filled arrow', 0 = 'None'
        
        // if you really want to change just the line style
        // connShp.LineStyle = "MyCustomLineStyle";
    }
    

    The values for the LinePattern and EndArrow cells map to those values found in the UI.

    enter image description here