Insert Static Text Into Shape With Text fields

Started by DCook, May 14, 2008, 09:46:50 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

DCook

Hi,

I am working with Visio 2007 and trying to insert static text using VBA in a shapes text area below existing text that uses fields to show the shape's data.  When I read the existing text to add a new line of static text, the lines that contain fields with the Shape Data are just garbage so I can't just read it in and add my static text to the end and then set it back to the shapes text.

Application.ActiveWindow.Selection(1).Text = Application.ActiveWindow.Selection(1).Text & TemplateList.Text

I then used the shapes Characters to read in existing text and then add my static text to the end and set it back into the Shape's text but then I lose the fields in the text area that shows the Shapes Data.  Can someone point me in the correct way to do this?  Here is my current code that I use to try to insert the static text at the end.


Private Sub InsertButton_Click()
    Dim shp As Visio.Shape
    Dim statements As String
    Dim shpChars As Visio.Characters
    Dim numChars, c, startRun, endRun
    Dim tmpTxt As String

    Set shp = Application.ActiveWindow.Selection(1)
    ' Get Shape formated text object.
    Set shpChars = shp.Characters
    ' Get count of characters in formatted text.
    numChars = shpChars.CharCount
    ' Init Local vars.
    startRun = 0
    endRun = 1
    tmpTxt = ""
    ' Use formmated text object to extract text by lines.
    For c = 0 To numChars
        ' Set begin marker to current position
        ' to search for next line.
        shpChars.Begin = c
        ' Set end marker to begin marker plus one.
        shpChars.End = c + 1
        ' Get start of next line
        startRun = shpChars.RunBegin(visParaRun)
        ' Get end of next line
        endRun = shpChars.RunEnd(visParaRun)
        ' Set begin and end markers to select
        ' the line we want.
        shpChars.Begin = startRun
        shpChars.End = endRun
        tmpTxt = tmpTxt & shpChars.Text
        ' Set current position to end of this line.
        c = endRun
    Next c
    ' Add a newline and Combo box's slected text to shape.
    tmpTxt = tmpTxt & vbNewLine & TemplateList.Text
    shp.Text = tmpTxt
End Sub


Thanks!

Lars-Erik

#1
I tried for about 10 min without result, ill try some more when i have some spare time.

Thats some nice code you have there, the main problem ( which i suspect you know ) is that it checks for the value and not the formula, you'll have to extract the formula that in the field, and then add something like & "your string"

Also I would try and use the field and not the shapes text, or whatever you will do you'll wreck the links to the data, it will become a static string that doesn't respond to changes.

vbNewLine is nice, I didint know this existed, awesome :D

Update: Bah, this is tricky ;) I keep getting errors "Replace user and try again"

* Lars-Erik shines the Visguy light into the clouds
lets hope he can see this from his VisCave...

- Lars-Erik

Visio Guy

#2
Quote from: Lars-Erik on May 15, 2008, 06:02:20 AM
* Lars-Erik shines the Visguy light into the clouds
lets hope he can see this from his VisCave...

Somebody been watchin' too much BatMan...

Anyway, I figured it out. I cheated and used the macro recorder to figure out the fine points.

I think DCook had the right idea, but missed setting the start of his run to the end of the text. Anyway, this little code snippet works. It appends dumb text onto inserted-field text without destroying anything:


Sub InsertDumbTextWithoutDestroyingSmartText()

  Dim shp As Visio.Shape
  Set shp = Visio.ActiveWindow.Selection(1)
 
  Dim chrs As Visio.Characters
  Set chrs = shp.Characters
 
  chrs.Begin = shp.Characters.End
  chrs.End = shp.Characters.End
  chrs.Text = vbCrLf & "Bob"
 
End Sub
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Lars-Erik

Nice, I tried the macro recorder as well, but i suppose it matters how you try to do it, different ways give different code from the recorder.
I tried editing the shape sheet, which seemed to work well, until i had to input the "new" string back into it, i kept getting an error "#NAME?"
Whats that all about?

O well.

- Lars-Erik

The Vislight seems to work, so don't mock it :D

DCook

Cool!  Thanks guys, I should have thought of that.  Simply set Characters to end and then add text the Characters.  Nice!

DCook

#5
Hi Visio Guy,

Thanks for your help!  I changed the VBA code and that works great.  Now I have another question.  I put this code in my Master shape in my Visio stencil and assigned it the shapes sheet's action to run the macro from a right click menu.  The problem is when I drop the shape into a drawing, the right-click option doesn't work.  I looked at the ShapeSheet and it shows the action RUNMACRO("MyTestStencil.Module1.InstertText") and shows up on the right click menu.  Thought it would run the macro in the master shape?  How do I make it run the master shape macro?

Thanks!

DCook

Ok, I figured out how to run the macro in the stencil from the shape dropped into the document.  ;D
I changed the right click menu action from

    RUNMACRO("MyTestStencil.Module1.InstertText")

to

    RUNMACRO("Module1.InstertText", "MyTestStencil")

and it works.

Visio Guy

If your macro is in the calling document (ie: where the shape is that called the macro), then you can get away with the short form:

RUNMACRO("InstertText")

In this case, however, it looks like the code lives in the stencil, so it is necessary to specify the project.

Regardless, you won't be able to get away with misspelling Instert in this forum, buddy!

:) :)
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Lars-Erik

People who don't speek English as a native language are bound to make some misteeks.
Right?  ::)
Though a small tip for you firefox users out there: a dictionary
And for you Internet explorer users: ...a decent browser ;)

Yes the spelling errors are intentional.

- Lars