SamAPOC
SamAPOC

Reputation: 73

c# How to use Visio.Shape properties related to fields

Im having a few problems with interacting with Visio.Shape items using C#.

I am able to loop through each shape within a page, when i do myShapeObj.Text i tend to get the correct text back. However, my visio document has the shape linked to a custom field. Fields > Custom Formuls = ThisDoc!User.mycustomproperty.

I have a User-Defined Cell called "User.mycustomproperty" which holds a value such as "02a", "03" etc The string returns comes up as not recognised character when shown in visual Studio's debug window. Is it possible to parse the value as text?

Also a second related question: Is there an easy way to get/set this User-defined Cell?

Upvotes: 0

Views: 847

Answers (1)

JohnGoldsmith
JohnGoldsmith

Reputation: 2698

There are two main properties for accessing a shape's text - one is Shape.Text, as you know, and the other is Shape.Characters. The latter returns a Characters object which has a number of members for reading and manipulating the text.

Amongst these is a Characters.Text property that returns the entire text including expanded fields.

So, if you have three shapes like this:

enter image description here

...and then run this code:

void Main()
{
    var vApp = MyExtensions.GetRunningVisio();
    var vPag = vApp.ActivePage;
    foreach (Visio.Shape shp in vPag.Shapes)
    {
        Console.WriteLine($"Text: {shp.Text} \nCharacters: {shp.Characters.Text}\n");
    }
}

...you'll get the following output:

Text: Shape 1 with no field

Characters: Shape 1 with no field

Text: Shape 2 with a doc field []

Characters: Shape 2 with a doc field [02a]

Text: Shape 3 with datetime field []

Characters: Shape 3 with datetime field [Monday, 20 November 2017]

Here you can see that Shape.Text returns the collapsed field and Shape.Characters.Text returns the expanded version.

Note that GetRunningVisio is my extension method for using with LinqPad:

http://visualsignals.typepad.co.uk/vislog/2015/12/getting-started-with-c-in-linqpad-with-visio.html

...but it's up to you how you get hold of the application object.


For your second question about setting User cells then I would do something like this:

const string targetCellName = "User.mycustomproperty";
var docSheet = vDoc.DocumentSheet;
if (docSheet.CellExistsU[targetCellName, (short)0] != 0)
{
    vDoc.DocumentSheet.CellsU[targetCellName].FormulaU = @"=""04a""";
}

Upvotes: 1

Related Questions