Author Topic: Adding Custom Shape Fields/Data Programmatically  (Read 15753 times)

0 Members and 1 Guest are viewing this topic.

nnakizo

  • Newbie
  • *
  • Posts: 8
Adding Custom Shape Fields/Data Programmatically
« on: June 05, 2008, 03:10:21 PM »
I have a program that is drawing rectangular shapes on the fly.  On certain ones I want to create custom shape properties and then, of course, populate them with data.  I have figured out the populating part...just not how to define them for a shape on the fly.  Any code snippets or thoughts?  I can't seem to find anything.  Thanks!!

Lars-Erik

  • Sr. Member
  • ****
  • Posts: 278
Re: Adding Custom Shape Fields/Data Programmatically
« Reply #1 on: June 05, 2008, 03:57:02 PM »
One way is to make a shape (no drawing, so basicly a blank group) with the data. Then drag the group onto the page and add the shape to the group. thats one way to do this, not the nicest one though.

Second way to do this is to use some VBA code:
Code: [Select]
Sub OnTheFly()
    On Error GoTo Error
    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Undo the action")
    Dim vsoShape1 As Visio.Shape
    Dim intPropRow2 As Integer
    Set vsoShape1 = Application.ActiveWindow.Selection(1)
    'Add all shape data
'*************Copy this******************
    intPropRow2 = vsoShape1.AddRow(visSectionProp, visRowLast, visTagDefault)
    vsoShape1.Section(visSectionProp).Row(intPropRow2).NameU = "TheName"
    vsoShape1.CellsSRC(visSectionProp, intPropRow2, visCustPropsLabel).FormulaU = """TheLabel"""
    vsoShape1.CellsSRC(visSectionProp, intPropRow2, visCustPropsType).FormulaU = "0"
    vsoShape1.CellsSRC(visSectionProp, intPropRow2, visCustPropsFormat).FormulaU = ""
    vsoShape1.CellsSRC(visSectionProp, intPropRow2, visCustPropsLangID).FormulaU = "1043"
    vsoShape1.CellsSRC(visSectionProp, intPropRow2, visCustPropsCalendar).FormulaU = ""
    vsoShape1.CellsSRC(visSectionProp, intPropRow2, visCustPropsPrompt).FormulaU = ""
    vsoShape1.CellsSRC(visSectionProp, intPropRow2, visCustPropsValue).FormulaU = ""
    vsoShape1.CellsSRC(visSectionProp, intPropRow2, visCustPropsSortKey).FormulaU = ""
'****************And insert more as needed**************
    Application.EndUndoScope UndoScopeID1, True
   
GoTo Done
Error:
Done:
End Sub
This will add one property to your shape, you can always make more if you need.

A nice tip: use the macro recorder, start a new macro. Create all the changes to your shapes. stop the macro recorder and now just run the macro it created while selecting your shapes. Also a nice way to figure out the way to set other properties then mentioned above if you need others.

- Lars

nnakizo

  • Newbie
  • *
  • Posts: 8
Re: Adding Custom Shape Fields/Data Programmatically
« Reply #2 on: June 09, 2008, 01:59:47 PM »
cool thanks..though now i want to pass a variable.  In the macro I write some data..shows up like:

shpObj.CellsSRC(visSectionProp, intPropRow2, visCustPropsValue).FormulaU = """SSE-123"""

Instead of the string "SSE-123" I want to set it to a variable "foo" where foo=SSE-123.  I can't get correct combo of quotes or I'm just a retard:

for example, following doesn't work...
shpObj.CellsSRC(visSectionProp, intPropRow2, visCustPropsValue).FormulaU = foo

Lars-Erik

  • Sr. Member
  • ****
  • Posts: 278
Re: Adding Custom Shape Fields/Data Programmatically
« Reply #3 on: June 09, 2008, 02:20:52 PM »
to get the value in the shapesheet to be foo (no quotes) you should use single quotes so formulaU = "foo"
to get it quoted, you need """foo""".

If I'm not mistaken this is because, the first quote tells VBA that theres a string comming, the second closes the string, so "" would make VBA thing the string is empty, but we really wanted a quote in the string, so we use 3 quotes, """ to tell it we want a quote inside the string. To prefent confusion on VBA's side. For humans like us (I'm guessing) it might not always make sense.

Or thats what I 've been told anyway :)

nnakizo

  • Newbie
  • *
  • Posts: 8
Re: Adding Custom Shape Fields/Data Programmatically
« Reply #4 on: June 10, 2008, 11:03:13 AM »
nope....the code below just puts "x" in the cell, I want value "foo" put in the cell.  I've tried different numbers of quotes....

x="foo"
shpObj.CellsSRC(visSectionProp, intPropRow2, visCustPropsValue).FormulaU = """x"""

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1669
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: Adding Custom Shape Fields/Data Programmatically
« Reply #5 on: June 10, 2008, 01:16:36 PM »
People,

Quotes are evil and they will drive you nuts. Define a constant or use character codes:

shp.Cells("User.foo").FormulaForceU = Chr ( 34 ) & "Bob" & Chr ( 34 )
shp.Cells("User.foo").FormulaForceU = ControlChars.Quote & "Bob" & ControlChars.Quote

Const QT as String = Chr ( 34 )
shp.Cells("User.foo").FormulaForceU = QT  & "Bob" &  QT

The code is arguably easier to read and you will make fewer mistakes.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010