-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisualBasic_Split_Text_Multilines.txt
56 lines (44 loc) · 2.02 KB
/
VisualBasic_Split_Text_Multilines.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Sub SplitTextIntoObjects()
Dim shp As Visio.Shape
Dim text As String
Dim textArray() As String
Dim i As Integer
Dim newShape As Visio.Shape
Dim xPos As Double
Dim yPos As Double
Dim lineHeight As Double
Dim textWidth As Double
' Check if there is a selected shape
If Visio.ActiveWindow.Selection.Count = 0 Then
MsgBox "Please select a shape with multi-line text."
Exit Sub
End If
Set shp = Visio.ActiveWindow.Selection.PrimaryItem
' Get the shape's text
text = shp.text
' Split the shape's text into lines using both CR and LF
textArray = Split(Replace(text, vbCrLf, vbLf), vbLf)
' Debug: Show the number of lines detected
MsgBox "Number of lines detected: " & UBound(textArray) - LBound(textArray) + 1
' Get the position and width of the original shape
xPos = shp.CellsU("PinX").ResultIU
yPos = shp.CellsU("PinY").ResultIU
textWidth = shp.CellsU("Width").ResultIU
' Debug: Show the original shape position and width
MsgBox "Original shape position (X, Y): (" & xPos & ", " & yPos & ")" & vbCrLf & "Width: " & textWidth
' Set the height for each line of text
lineHeight = shp.CellsU("Height").ResultIU / (UBound(textArray) - LBound(textArray) + 1)
' Loop through each line of text and create a new text shape for each line
For i = LBound(textArray) To UBound(textArray)
Set newShape = Visio.ActivePage.DrawRectangle(xPos - textWidth / 2, yPos - (i * lineHeight), xPos + textWidth / 2, yPos - ((i + 1) * lineHeight) + lineHeight)
newShape.text = textArray(i)
newShape.CellsU("Height").ResultIU = lineHeight
newShape.CellsU("Width").ResultIU = textWidth
' Debug: Show the text of the new shape
MsgBox "Created shape with text: " & textArray(i)
Next i
' Delete the original shape
shp.Delete
' Debug: Confirm deletion
MsgBox "Original shape deleted."
End Sub