Author Topic: [VB.NET/Visio] Need to copy only bold text from a shape's shape.text property  (Read 3239 times)

0 Members and 1 Guest are viewing this topic.

yourwifeandkids

  • Newbie
  • *
  • Posts: 2
Hello,

I'm writing an addin for Visio 2010 using VB.NET in Visual Studio 2013. I need to select text from a visio shape (shape.text property), where only bold text is selected, so that I may proceed to store only that text as a string to be stored as a formula for a cell in an excel sheet. See the code below for context.

Further context:

  • cableList stores many of my user defined cable objects
  • a cable has a property called box, which is a shape
  • the box.text has many lines of text, but only the first few lines are bold, and those are the lines I need to copy
  • furthermore, there may be newline characters within the bold text, and I need to ignore those when outputting to excel

I hope that's enough information! I've been searching all over for any solutions, to no avail, and since I'm writing the addin with VB.NET, the VB6 code that the Visio 2010 macro recorder is spitting out has been very little help.

Thanks!

Code
With xlWorksheet
    If cableList.Count > 0 Then
        For i = 1 To cableList.Count

            'SOLUTION
            Dim boxText As String = ?
            ...
            'END SOLUTION

            .Range("a4").EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown, Excel.XlInsertFormatOrigin.xlFormatFromRightOrBelow)
            .Range("a4").Formula = boxText
            'many more cell formula assignments irrelevant to this forum post
            rowIndex += 1
        Next 'i in cableList count
    End If

    .Columns("a:j").autofit()

End With 'xlworksheet
« Last Edit: July 28, 2016, 05:40:09 PM by yourwifeandkids »

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4917
  • Ideas Visio-lized into solutions
I've attached a simple file to show some of the Visio text quirks.  When dealing with Visio text, generally, it's all about "characters". 

If you open the file, there's a single shape with regular and bold text.  Open the shapesheet, scroll down to the Characters section.  You will see multiple line entries.  The left column is the character count.  Believe it starts with 0, not 1.  Could be wrong.  Two of the rows have style 17.  These are your bold characters.

Now open the VBA edit window <alt> + F11.  Under Modules>NewMacros you'll find 2 macros.  Each of those selects a line of bold text.  You'll see a correlation between the code and the shapesheet. 

So, you need to search the characters section, find a row that has style 17, then copy the set of characters that span that row.

HTH
Wapperdude

BTW, code was generated by the macro recorder.  It really is a handy tool.
Visio 2019 Pro

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4917
  • Ideas Visio-lized into solutions
Turns out, finding a working solution was not as straight forward as I envisioned.  Actual solution is not that involved, but, understanding the Visio object model is a challenge.  Attached file has example of searching thru a shape to find the Bold text.  Below is the code needed (and included in the file).  The code does not include transportation to Excel. 

Code
Sub findBoldText()
'Macro to read the Characters secton of a shapesheet

    Dim vsoChars As Visio.Characters
    Dim chrRunBeg As Integer
    Dim chrRunEnd As Integer
    Dim chrRowCnt As Integer
    Dim chrStyle As Integer
   
    chrRowCnt = 0                              'This is first row.
   
    If ActiveWindow.Selection.Count <> 1 Then
        MsgBox "Select a shape, then re-run macro."
        Exit Sub
    End If
   
    Set vsoshp = ActiveWindow.Selection(1)
    Set vsoChars = vsoshp.Characters
    chrCnt = vsoChars.CharCount
    Debug.Print "Total number of text chars = " & chrCnt
   
    For i = 0 To chrCnt - 1
        vsoChars.Begin = i
        vsoChars.End = i + 1    'This selects just one character, representative of a row of identical characters
       
        chrRunBeg = vsoChars.RunBegin(Visio.VisRunTypes.visCharPropRow)
        chrRunEnd = vsoChars.RunEnd(Visio.VisRunTypes.visCharPropRow)
        chrStyle = vsoshp.CellsSRC(visSectionCharacter, chrRowCnt, visCharacterStyle).ResultStr(none)
       
'At this point, if style = 17, then we have the row of Bold chars.
'Set vsoChars.begin = chrRunBeg, and vsoChars.end - chrRunEnd
'vsoChars.copy and then paste or do whatever with the captured text

        If chrStyle = "17" Then
            Debug.Print "Row Count = " & chrRowCnt
            Debug.Print "Beginning char for the run = " & chrRunBeg & Chr(9) & "Ending char for the run = " & chrRunEnd
            Debug.Print "Char style = " & chrStyle
       
            vsoChars.Begin = chrRunBeg
            vsoChars.End = chrRunEnd
            vsoChars.Copy
            Debug.Print vsoChars
            Debug.Print ""
        End If
       
        i = chrRunEnd                           'Don't need to cycle thru each character, just each row.
        chrRowCnt = chrRowCnt + 1
    Next
End Sub

Wapperdude
Visio 2019 Pro

yourwifeandkids

  • Newbie
  • *
  • Posts: 2
Thank you so much for the help, wapperdude! I will try to implement your solution now and edit my post soon to let you know how it worked out.