Get/Set Custom Properties

R

Ryo_Ohki2

Hello,
I'm using Visio Pro 2003 with VS Basic .NET. What I'm trying to do i
pull the custom properties off of a shape to list it in a treeview. M
problem is that I can't seem to even get any custom property off of th
shape. I've tried looking for examples, but none of them seems t
work, or I'm having a hard time understanding them to get th
properties. I've also looked into the Visio SDK for examples, but the
don't seem to work. ( targetShape.Cells("Prop.MyCustomPropLabel").Resul
)

If anyone can show me a example of how to pull all the label/data fro
the custom properties, I would be extremely grateful.

Note that I have been able to pull data from one shape (and only on
shape) due to the fact I know the property name my friend's visi
template.. but when I create my own custom property on a differen
shape (by right click the shape "Shapes=>Custom Property"), I can't se
it. It's probably something to do with the ability to only modify th
labels, not the actual name..

Any case if you can show a example..

Thanks for the help
 
J

JuneTheSecond

CellsSRC property might be a help to get Labels.
For example,
Debug.Print shp.CellsSRC(visSectionProp, visRowProp,
visCustPropsLabel).Formula
 
R

Ryo_Ohki2

After pounding through different code I finally figured it out.. Ye
you are right about the CellSRC. Also found that RowNameU give me th
property's universal name which I was worried about getting as well.
Now I cycle through the properties to get all the data I require. :
 
C

Chris [Visio MVP]

Here's a sub from a class that I use, perhaps it will help. VisShapeProp is
simply a structure I use for holding custom property info - it's not shown
here, and isn't really part of Visio. The function uses CellsSRC to access
custom property info via Section, Row, and Cell indices that correspond to
ShapeSheet locations.
Private Sub _getCustomProps()

Dim iRow As Short, iRowCt As Short
Dim s As Short = Visio.VisSectionIndices.visSectionProp + 0
Dim rFirst As Short = Visio.VisRowIndices.visRowProp + 0
Dim rLast As Short
Dim cVal As Short = Visio.VisCellIndices.visCustPropsValue + 0
Dim cTyp As Short = Visio.VisCellIndices.visCustPropsType + 0
Dim cFrmt As Short = Visio.VisCellIndices.visCustPropsFormat + 0

_props = New ArrayList

rLast = Convert.ToInt16(rFirst + _visShp.RowCount(s) - 1)

For iRow = rFirst To rLast

Dim cp As New VisShapeProp

cp.RowName = _visShp.CellsSRC(s, iRow, cVal).RowNameU

cp.Value = _visShp.CellsSRC(s, iRow,
cVal).ResultStr(Visio.VisUnitCodes.visNoCast)

cp.Formula = _visShp.CellsSRC(s, iRow, cVal).Formula

cp.Type = _visShp.CellsSRC(s, iRow, cTyp).Formula

_props.Add(cp)

Next

End Sub
 
A

Al Edlund

chris,
tks, I was wondering if you could use structures in an arraylist,
hope you guys had a great set of holdays,
al
Chris said:
Here's a sub from a class that I use, perhaps it will help. VisShapeProp
is simply a structure I use for holding custom property info - it's not
shown here, and isn't really part of Visio. The function uses CellsSRC to
access custom property info via Section, Row, and Cell indices that
correspond to ShapeSheet locations.
Private Sub _getCustomProps()

Dim iRow As Short, iRowCt As Short
Dim s As Short = Visio.VisSectionIndices.visSectionProp + 0
Dim rFirst As Short = Visio.VisRowIndices.visRowProp + 0
Dim rLast As Short
Dim cVal As Short = Visio.VisCellIndices.visCustPropsValue + 0
Dim cTyp As Short = Visio.VisCellIndices.visCustPropsType + 0
Dim cFrmt As Short = Visio.VisCellIndices.visCustPropsFormat + 0

_props = New ArrayList

rLast = Convert.ToInt16(rFirst + _visShp.RowCount(s) - 1)

For iRow = rFirst To rLast

Dim cp As New VisShapeProp

cp.RowName = _visShp.CellsSRC(s, iRow, cVal).RowNameU

cp.Value = _visShp.CellsSRC(s, iRow,
cVal).ResultStr(Visio.VisUnitCodes.visNoCast)

cp.Formula = _visShp.CellsSRC(s, iRow, cVal).Formula

cp.Type = _visShp.CellsSRC(s, iRow, cTyp).Formula

_props.Add(cp)

Next

End Sub


--
Hope this helps,

Chris Roth
Visio MVP
 
R

Ryo_Ohki2

That's what I did.. used a struct. My code looked similiar using th
{Visio Shape}.CellsSRC If I didn't figure it out, your example woul
have definitely steered me to the right direction :)

BTW What's with the "+ 0" after every Visio.VisCellIndices that yo
defined
 
C

Chris [Visio MVP]

+0 is my cheap conversion to Short. The code is so ungly when you have to
wrap Ctype( ) around everything. Not sure if + 0 is really a good idea, but
it works.
 
W

WalterHR

Hi,
I need modify a custom property using vba but I've not found any exemple
that show me how. I've tried with a macro using :
objshape.Shapes.Item(2).Cells("Prop.Row_1").Formula = "nnnnn"
, but visio gave me a run-time error. Thanks in advance for any idea, I'm
new in this subject.

Walter R.
Programmer-Analyst
 
A

Al Edlund

you might consider getting the v2003 sdk which is a free available download.
It has examples for set/get custom properties.


' Copyright (c) Microsoft Corporation. All rights reserved.

Public Sub ReadANamedCustomProperty(vsoShape As Visio.shape, _
strCellName As String, _
Optional blnIsLocalName As Boolean = False)

' ReadANamedCustomProperty
'
' Abstract - This procedure reads a custom property
' and displays its value.
'
' Parameters
' vsoShape Specifies the shape which has the
' custom property
'
' strCellName Specifies the name of the custom
' property to be accessed
'
' blnIsLocalName Specifies if the cell name is a
' universal or local name

Dim strPropName As String
Dim blnExists As Boolean
Dim vsoCell As Visio.Cell

On Error GoTo ReadANamedCustomProperty_Err

' Custom properties have a prefix of "Prop".
strPropName = "Prop." & strCellName

' If the property exists, get the value of the cell
' from the shape.
If blnIsLocalName Then

blnExists = vsoShape.CellExists(strPropName, _
visExistsAnywhere)

If blnExists Then
Set vsoCell = vsoShape.Cells(strPropName)
End If
Else

blnExists = vsoShape.CellExistsU(strPropName, _
visExistsAnywhere)

If blnExists Then
Set vsoCell = vsoShape.CellsU(strPropName)
End If
End If

If blnExists And _
vsoShape.Application.AlertResponse = 0 Then

' Show the value in page units.
MsgBox strPropName & ".Result in Page Units = " _
& vsoCell.ResultStr(visPageUnits)

' Show the String value attached to the custom property.
MsgBox strPropName & ".ResultStr = " _
& vsoCell.ResultStr(visNoCast)

' Show the value in inches.
MsgBox strPropName & ".Result in Inches = " _
& vsoCell.result(visInches)

' Show the value using no cast.
MsgBox strPropName & ".Result using No Cast = " _
& vsoCell.result(visNoCast)

' Show the formula attached to the custom property.
MsgBox strPropName & " FormulaU = " & vsoCell.FormulaU

ElseIf vsoShape.Application.AlertResponse = 0 Then
MsgBox strPropName & " was not found."

' We cannot proceed because the property
' does not exist.
Exit Sub
End If

Exit Sub

ReadANamedCustomProperty_Err:
Debug.Print Err.Description

End Sub


al
 
A

Al Edlund

this is vb.net, but similar to what you might want to consider
al

Public Sub SetCustomPropertyValue( _

ByVal customPropertyShape As Microsoft.Office.Interop.Visio.Shape, _

ByVal rowNameU As String, _

ByVal propertyValue As Object, _

ByVal units As Microsoft.Office.Interop.Visio.VisUnitCodes)

Const CUST_PROP_PREFIX As String = "Prop."

Dim customPropertyCell As Microsoft.Office.Interop.Visio.Cell

Dim visioApplication As Microsoft.Office.Interop.Visio.Application

visioApplication = CType(customPropertyShape.Application, _

Microsoft.Office.Interop.Visio.Application)

Try

' Verify that all incoming string parameters are not of zero

' length, except for the ones that have default values as ""

' and the output parameters.

If (rowNameU.Length() = 0) Then

Throw New System.ArgumentNullException("rowNameU", _

"Zero length string input.")

End If

' See if the shape has a custom property Value cell with the

' universal row name. If no cell exists, display an error

' message and exit this procedure.

If Not CBool(customPropertyShape.CellExistsU(CUST_PROP_PREFIX _

& rowNameU, CShort(False))) Then

If visioApplication.AlertResponse = 0 Then

System.Windows.Forms.MessageBox.Show( _

"This shape does not have a custom property " _

& vbCrLf & "with the universal name '" _

& rowNameU & "'.")

End If

Return

End If

' Get the Cell object. Note the addition of "Prop."

' to the name given to the cell.

customPropertyCell = customPropertyShape.CellsU( _

CUST_PROP_PREFIX & rowNameU)

If (units = Microsoft.Office.Interop.Visio. _

VisUnitCodes.visUnitsString) Then

SetCellValueToString(customPropertyCell, _

CStr(propertyValue))

Else

' Use Result property to set values other than String type.

customPropertyCell.Result(units) = CDbl(propertyValue)

End If

Catch err As Exception

System.Diagnostics.Debug.WriteLine(err.Message)

End Try

End Sub
 

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