Author Topic: Insert Static Text Into Shape With Text fields  (Read 10996 times)

0 Members and 1 Guest are viewing this topic.

DCook

  • Jr. Member
  • **
  • Posts: 11
Insert Static Text Into Shape With Text fields
« on: May 14, 2008, 04:46:50 PM »
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.

Code
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!
« Last Edit: May 15, 2008, 02:43:40 AM by Visio Guy »

Lars-Erik

  • Sr. Member
  • ****
  • Posts: 278
Re: Insert static text into Shape
« Reply #1 on: May 15, 2008, 01:02:20 AM »
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
« Last Edit: May 15, 2008, 01:56:45 AM by Lars-Erik »

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1723
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: Insert static text into Shape
« Reply #2 on: May 15, 2008, 02:42:46 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:

Code
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
« Last Edit: May 15, 2008, 02:44:19 AM by Visio Guy »
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

  • Sr. Member
  • ****
  • Posts: 278
Re: Insert Static Text Into Shape With Text fields
« Reply #3 on: May 15, 2008, 03:06:25 AM »
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

  • Jr. Member
  • **
  • Posts: 11
Re: Insert Static Text Into Shape With Text fields
« Reply #4 on: May 15, 2008, 12:44:28 PM »
Cool!  Thanks guys, I should have thought of that.  Simply set Characters to end and then add text the Characters.  Nice!

DCook

  • Jr. Member
  • **
  • Posts: 11
Re: Insert Static Text Into Shape With Text fields
« Reply #5 on: May 15, 2008, 01:33:55 PM »
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!
« Last Edit: May 19, 2008, 02:35:54 AM by DCook »

DCook

  • Jr. Member
  • **
  • Posts: 11
Re: Insert Static Text Into Shape With Text fields
« Reply #6 on: May 19, 2008, 02:12:18 PM »
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

  • Administrator
  • Hero Member
  • *****
  • Posts: 1723
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: Insert Static Text Into Shape With Text fields
« Reply #7 on: May 19, 2008, 04:21:35 PM »
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

  • Sr. Member
  • ****
  • Posts: 278
Re: Insert Static Text Into Shape With Text fields
« Reply #8 on: May 19, 2008, 04:32:03 PM »
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