keep text information in the userfrom And code for shape text

Started by Dusty_Rhodes, December 27, 2018, 09:52:55 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dusty_Rhodes

Hello,

I have a userform that fills in information on the visio drawing. The information (text) gets put into the right boxes and the text written is visible in the userform during the session but as soon as the drawing is closed and reopened the text is still on the drawing (as it should be ) but the userform is blank. Is there some way to have the textboxes on the form display the information that is visible on the drawing after it is reopened ? i assume this should be pretty easy ti achive.

I also find it pretty hard to know how the boxes (shapes) are defined with code. I have currently been recording macros and then cleaning the code a little since i dont know how to refrence the location of a shape with code. The current code for the textboxes on the userform looks like this. It is not pretty but it works. Is there some easier way to write the code for this function of filling text ?


'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Dim vsoCharacters1 As Visio.Characters
    Set vsoCharacters1 = Application.ActiveWindow.Page.Shapes.ItemFromID(210).Characters
    vsoCharacters1.Begin = 0
    vsoCharacters1.End = 10
    vsoCharacters1.Text = TEXTBOX1.Value

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices



Dusty_Rhodes

Solved the issue with geting the text to appear in the textboxes when the userform is loaded.

Code for getting the shape filled with text. The "Background" is the name of the page
Private Sub Text()
'Makes the text writen in the textbox  appear on the drawing
Set vsoShape = Application.ActiveDocument.Pages.ItemU("Background").Shapes("Shape.1")
            vsoShape.Text = Textbox1.Value
End Sub


Code for retaining the information when the userform is opened again
[/Private Sub UserForm_Activate()
Set vsoShape = Application.ActiveDocument.Pages.ItemU("Background ").Shapes("Shape.1")
    Me.TextBox1.Value = vsoShape.Text
End Sub
code]

Yacine

Hello Dusty Rhodes,
I think it is generally bad design to store data in named shapes.
These shapes are too "volatile". They can be erased, renamed, etc.
A better idea is to store general information in the page's or the doc's shapesheet.

Say "project name" is the information to store and display - make an according prop field in the docs prop section - manually or by code and reference it in your display shape.
Check the attachment.
Yacine

Dusty_Rhodes

#3
Hello Yacine,

Thx for the attachment and help. The attachment does excactly what i have been trying to do with my userform. But it is probably just me that is to unfamiliare with reading and creating vba code, i cant seem to figure out how your code works.

I am probably just slow but i have no idea what this code in Module 1 refrences or does.

The same goes for the code added in the userform. i Have never seen comands like CellExistsU, visRowLast, visSectionProp, AddNamedRow, ResultStrU in a visio code before.


[/Public Sub makeProp()
    ActiveDocument.DocumentSheet.AddNamedRow visSectionProp, "project_name", visRowLast
End Subcode]


[code][/Private Sub cmOK_Click()
    If ActiveDocument.DocumentSheet.CellExistsU("prop.project_name", visExistsAnywhere) Then
        ActiveDocument.DocumentSheet.Cells("prop.project_name").FormulaU = Chr(34) & textProjectName.Value & Chr(34)
    End If
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    If ActiveDocument.DocumentSheet.CellExistsU("prop.project_name", visExistsAnywhere) Then
        textProjectName.Value = ActiveDocument.DocumentSheet.Cells("prop.project_name").ResultStrU("")
    End If
End Subcode]

Yacine

Hello Dusty Rhodes,

Programming for Visio means for a very big part modifying properties of the objects of your document. Therefore you need to know Visio's object model.
https://docs.microsoft.com/en-us/visualstudio/vsto/visio-object-model-overview?view=vs-2017
https://msdn.microsoft.com/en-us/data/cc160740(v=vs.80)

Of course you can't know everything from the beginning, but you need to keep googling them object by object, property by property and command by command. That's how everybody started here.

For this special case:
in makeProp you take your ActiveDocument, then its shapesheet (DocumentSheet) and apply a method called AddNamedRow which does exactly what it says - it adds a named row to the shapesheet.
Googling this method will show you that it takes arguments
- where to add the row? In the prop section, given as number which itself is declared in a constant named visSectionProp
- "project_name" has quotation marks, --> it is obviously a hard coded string for the name of the row.
- at what position shall the new row be inserted? the last one - it is given as number - here again defined by constant = visRowLast.
So basically the sole purpose of this routine is to add a new row and give it name.

In the form I used some code to avoid errors if the row doesn't exist - I used the method CellExistsU - Google it.
In cmOK_Click I write the value to the text box to the custom property defined earlier. I get the cell by calling the "cells" property of the shapesheet of the document. To be able to write its content I use the formulaU property, and so on.

Actually easy. Just avoid being discouraged if you don't know something and google the commands ... and if you show that you've done your homework, you can even ask questions in the forum :D .
Yacine

Dusty_Rhodes

#5
Thx for the help Yacine

I am just discouraged since i have a deadline to meet for this project and i have been working on this for faaaaaar to long with what feels like zero progress.
I am currently trying to do what u did in your atacchment but for more shapes on the same page and i am not succeeding curently . All i get is error in formula when i try to make a prop with a different name for a different shape

Yacine

Quote from: Dusty_Rhodes on December 31, 2018, 08:00:42 AM
All i get is error in formula when i try to make a prop with a different name for a different shape

???
Yacine

Dusty_Rhodes

Hello Yacine sorry for the late response. Have been on winter holliday.

Here is a dumbd down version of the fields and code i have created. I have no idea how to do it the way u did without getting error messages. I would also need to create a COM-add in instead of only VBA in the document and the COM-add in does not allow me to add text like i have done here in the attached file.