Visio VBA question...

Started by jamesp@premiercs.co.uk, January 27, 2016, 10:53:15 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

jamesp@premiercs.co.uk

Hi guys

Two questions:
- Using Visio 2013 vba can I count the number of lines of text in a shape?
- If I want to use Visio 2013 vba change the colour of only some text in a shape, I only seem to have access to the Visio 2003 colour palette.  Is there any way around this?

Many thanks

James 

JuneTheSecond

#1
1.  number of lines
  I think it difficult, as Visio text is very strange to me.
  But this document by Visio Guy may be helpful for you.
http://www.visguy.com/2009/05/25/how-to-count-the-number-of-items-in-a-list/

2.  change the colour of only some text
  May be it is possible,
  Visio has Characters object in VBA.
  Search in Web by keywords "msdn Visio characters object" , "msdn Visio characters members" and "Visio Characters.CharProps Property".
Best Regards,

Junichi Yoda
http://june.minibird.jp/

JuneTheSecond

Now, I remember the macro recording.
Macro given by recorder is

    Dim vsoCharacters1 As Visio.Characters
    Set vsoCharacters1 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).Characters
    vsoCharacters1.Begin = 87
    vsoCharacters1.End = 89
    vsoCharacters1.CharProps(visCharacterColor) = 0#

This macro works well, if last 0# is replaced with 2# or other number.
Best Regards,

Junichi Yoda
http://june.minibird.jp/

aledlund

There was a series of articles on the MS Visio blog relative to editing Visio text. You can find them if you Google "visio character runs".
hth,
al edlund

JuneTheSecond

#4
Here is my attempt to get number of text lines in the shape.
Please note that this is not always correct, because condition can easily change in editing text.
This is made with shapesheet functions, but not with VBA.
This is shape based solution, but not document base nor application base.
Best Regards,

Junichi Yoda
http://june.minibird.jp/

jamesp@premiercs.co.uk

Thanks Junichi san.  Lines of code very useful.  Re 'vsoCharacters1.CharProps(visCharacterColor) = 0#': The colours that you can choose from this are from the 2003 colour palette and not great.  For example I can't select the orange colour. 

Al thanks for the guidance

James

JohnGoldsmith

Hello James,

Just building on Junichi's code, you can address that corresponding Characters section row using CharPropsRow.  So you can set the characters using CharProps to ensure that the Characters section row is present and then immediately set the color cell to the RGB of your choice.  Slight convoluted but it works:

Sub TestChars()
Dim shp As Shape
Set shp = ActivePage.Shapes(1)
Dim vsoCharacters1 As Visio.Characters
Set vsoCharacters1 = shp.Characters
    vsoCharacters1.Begin = 41
    vsoCharacters1.End = 42
    vsoCharacters1.CharProps(visCharacterColor) = 3#
    shp.CellsSRC(visSectionCharacter, vsoCharacters1.CharPropsRow(visBiasLetVisioChoose), visCharacterColor).FormulaU = "RGB(200,80,80)"
    Debug.Print vsoCharacters1.Text
End Sub


Best regards

John
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

hansch

Quote from: JohnGoldsmith on January 29, 2016, 02:22:27 PM
Hello James,

Just building on Junichi's code, you can address that corresponding Characters section row using CharPropsRow.  So you can set the characters using CharProps to ensure that the Characters section row is present and then immediately set the color cell to the RGB of your choice.  Slight convoluted but it works:

Sub TestChars()
Dim shp As Shape
Set shp = ActivePage.Shapes(1)
Dim vsoCharacters1 As Visio.Characters
Set vsoCharacters1 = shp.Characters
    vsoCharacters1.Begin = 41
    vsoCharacters1.End = 42
    vsoCharacters1.CharProps(visCharacterColor) = 3#
    shp.CellsSRC(visSectionCharacter, vsoCharacters1.CharPropsRow(visBiasLetVisioChoose), visCharacterColor).FormulaU = "RGB(200,80,80)"
    Debug.Print vsoCharacters1.Text
End Sub


Best regards

John

Hi John,

How would you modify this to apply to only the currently selected shape? What I'm actually trying to do is to colour each line of text within a textbox to a different colour. I managed to do it with...

vsoCharacters1.CharProps(visCharacterColor) = 0#

... but I'm limited to the basic colours. I'm trying to change colours based on RGB values. I tried incorporating your code...

shp.CellsSRC(visSectionCharacter, vsoCharacters1.CharPropsRow(visBiasLetVisioChoose), visCharacterColor).FormulaU = "RGB(200,80,80)"

... but I keep getting run time error "cannot create object".

Thanks.

Surrogate

Quote from: hansch on April 08, 2016, 02:46:18 AMto apply to only the currently selected shape?
Set shp = ActiveWindow.Selection.PrimaryItem. instead
QuoteSet shp = ActivePage.Shapes(1)
for change characters color try
Quoteshp.CellsSRC(visSectionCharacter, 0, visCharacterColor).FormulaU = "RGB(200,80,0)"