VBA on dynamic connector in Visio - vba

I am just beginner at programming with VBA on Visio. What I am looking to do is to write a code that automatically draws a connector between two shapes.
Searching on internet I was able to find a code that does that :
Public Sub AutoConnect_Example()
Dim vsoShape1 As Visio.Shape
Dim vsoShape2 As Visio.Shape
Dim vsoConnectorShape As Visio.Shape
Set vsoShape1 = Visio.ActivePage.Shapes("Decision")
Set vsoShape2 = Visio.ActivePage.Shapes("Process")
Set vsoConnectorShape = Visio.ActivePage.Shapes("Dynamic connector")
vsoShape1.AutoConnect vsoShape2, visAutoConnectDirRight, vsoConnectorShape
End Sub
The only thing I want to do now is to be able to add a text on the connector. Let's say add "SSL" on the connector and if possible change to color of the connector to red.
Does anyone knows what is the formula please ? My objective will be to add it to the code just below.
Hope my English isn't too bad. Thanks all for any help.
Raph

I modify and mix codes from official MS manuals
Sub ForRaphael()
' This part of the code contains the declaration of variables: existing shapes and connector
' Code borrowed from: https://learn.microsoft.com/en-us/office/vba/api/visio.shape.autoconnect
Dim vso1DShape As Visio.Shape
Dim vso2DShape1 As Visio.Shape
Dim vso2DShape2 As Visio.Shape
Dim vsoCellGlueFromBegin As Visio.Cell
Dim vsoCellGlueFromEnd As Visio.Cell
' Existing connector
Set vso1DShape = Visio.ActivePage.Shapes("Dynamic connector")
' Existing Decision shape
Set vso2DShape1 = Visio.ActivePage.Shapes("Decision")
' Existing Process shape.
Set vso2DShape2 = Visio.ActivePage.Shapes("Process")
' -------
' This part of the code contains gluing the ends of the connector to the existing shapes.
' The idea of this part is borrowed: https://learn.microsoft.com/en-us/office/vba/api/visio.cell.gluetopos
' -------
Set vsoCellGlueFromBegin = vso1DShape.Cells("BeginX")
Set vsoCellGlueFromEnd = vso1DShape.Cells("EndX")
'Use the GlueToPos method to glue the begin point of the 1D shape
'to the top center of the lower 2D shape.
vsoCellGlueFromBegin.GlueToPos vso2DShape1, 0.5, 1
'Use the GlueToPos method to glue the endpoint of the 1D shape
'to the bottom center of the upper 2D shape.
vsoCellGlueFromEnd.GlueToPos vso2DShape2, 0.5, 0
' =======
' This part of the code is suggested by Surrogate
' =======
vso1DShape.Text = "SSL" ' add text to connector
vso1DShape.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = 2 ' make connector red
vso1DShape.CellsSRC(visSectionObject, visRowLine, visLineEndArrow).FormulaU = "1" ' add arrow to end
End Sub
Hope this code works at your side :)
About colors
You can use RGB colors with syntax like
vso1DShape.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = "RGB(7,4,0)"
But i prefer built-in colors.
About line ends
Visio have 45 built-in line ends (aka arrows). For arrows you can get its number, but without first 0 (from range 0 to 9).
For example if you want use Arrow type 07 and Arrow type 40 from picture you must need code
vso1DShape.CellsSRC(visSectionObject, visRowLine, visLineBeginArrow).FormulaU = "7"
vso1DShape.CellsSRC(visSectionObject, visRowLine, visLineEndArrow).FormulaU = "40"

The only thing I want to do now is to be able to add a text on the
connector. Let's say add "SSL" on the connector and if possible change
to color of the connector to red.
Raph, try add these lines to your code
vsoConnectorShape.Text = "SSL" ' add text to connector
vsoConnectorShape.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = 2 ' make connector red
vsoConnectorShape.CellsSRC(visSectionObject, visRowLine, visLineEndArrow).FormulaU = "1" ' add arrow to end

Related

Changing colour of text segments in a powerpoint presentation

I have a Powerpoint-Slide with pasted, formatted source code in the form of text shapes. Sadly the contrast of some part of that text is bad on a projector, so I would like to change every colour occurence for a specific font with a different colour. In this specific example I want to replace the orange colour:
Iterating over all shapes and accessing the whole text of a shape is not a problem, but I can't find any property that allows me to enumerate over the styled text segments:
Sub ChangeSourceColours()
For Each pptSlide In Application.ActivePresentation.Slides
For Each pptShape In pptSlide.Shapes
If (pptShape.Type = 1) And (pptShape.TextFrame.TextRange.Font.Name = "Consolas") Then
' Iterate over styled segments and change them if the previous colour is orangey
MsgBox pptShape.TextFrame.TextRange
End If
Next
Next
End Sub
The TextRange2 property looked helpful at a first glance, but looking at the variables in the debugger I see nothing that looks like a series of formatted segments. I would expect to find something like <span> in HTML to check and possibly change the colour.
The textFrame2.textRange.Font is valid for the whole text. If you want to access the single characters and their individual formatting, you need to access textRange.Characters.
The following routine changes the text color for all characters that have a specific color to a new color:
Sub ChangeTextColor(sh As Shape, fromColor As Long, toColor As Long)
Dim i As Long
With sh.TextFrame2.TextRange
For i = 1 To .Characters.Length
If .Characters(i).Font.Fill.ForeColor.RGB = fromColor Then
.Characters(i).Font.Fill.ForeColor.RGB = toColor
End If
Next i
End With
End Sub
You call it from your code with
Dim pptSlide as Slide
For Each pptSlide In Application.ActivePresentation.Slides
Dim pptShape As Shape
For Each pptShape In pptSlide.Shapes
If (pptShape.Type = 1) And (pptShape.TextFrame.TextRange.Font.Name = "Consolas") Then
ChangeTextColor pptShape, RGB(255, 192, 0), vbRed
End If
Next
Next
You will have to adapt the RGB-Code to the orange you are using, or instead of using RGB, you can use ObjectThemeColor. To avoid a type mismatch, you need to declare the pptShape variable as Shape - you should declare all your variables and use Option Explicit anyhow.
Note that you can use the ChangeTextColor-routine also in Excel (and probably also in Word). Shapes are defined similar in Excel and Powerpoint. Advantage in Excel: You can use the macro recorder to get some insights how a Shape can be used in Office VBA.

Iterate through dynamic connectors to change colour based on text

I need to iterate through all dynamic connectors on a visio document and change the colour of the connector line based on the dynamic connector text label - The code is based off a previous question to change line width of dynamic connectors.
I cant find a box defining the text label
Sub Macro1()
Dim shp As Shape, mst As Master
' iterate all shapes per page
For Each shp In ActivePage.Shapes
' declare parent master for current shape
Set mst = shp.Master
' Process only shapes that have parent master-shape
If Not (mst Is Nothing) Then
' change only shapes, which master-shape is dynamic connector
If mst.Name = "Dynamic connector" Then
' Now i dont know how to proceed - forgive me i am new to coding - i know the syntax is wrong, im just trying to give somthing to go off
If shp.Text = "A" Then shp.Cells("LineColour").Formula = RGB(255,255,0))
Else If shp.Text = "G" Then shp.Cells("LineColour").Formula = RGB(0,255,0))
Else If shp.Text = "R" Then shp.Cells("LineColour").Formula = RGB(255,0,0))
End If
End
But shape text does not appear to be a property - even though the connector defiantly has text, and properties of this text such as font do appear.
Any help would be great - cheers
Maybe:
ActivePage.Shapes(shp.Name).TextFrame.Characters.Text = "Hello World!"
'or
shp.TextFrame.Characters.Text = "Hello World!"
' This macro works in Excel
Sub Macro1()
Dim shp As Shape
Me.Shapes.AddShape msoShapeLeftArrow, 200, 200, 10, 10
For Each shp In Me.Shapes
shp.TextFrame.Characters.Text = "A"
Debug.Print shp.name, shp.TextFrame.Characters.Text
Next
End Sub

VBA in MS Visio - highlighting connectors of selected shape

After selecting a shape (f.e. square or more squares) all the connectors glued to this shape would highlight red, yellow whatever.
The found code below is not working for me, any advice? (I am not coder, so please have patience with me)
Set shpAtEnd = cnx(1).ToSheet
' use HitTest to determine whether Begin end of connector
' is outside shpAtEnd
x = shpAtEnd.HitTest(shpTaskLink.Cells("BeginX"), _
shpTaskLink.Cells("BeginY"), 0.01)
If x = visHitOutside Then
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 2
Else
' do other stuff
End If
This is my first answer on stackoverflow and I hope the following VBA code can solve your problem on how to highlight connectors or connected shapes in Visio!
Public Sub HighlightConnectedShapes()
Dim vsoShape As Visio.Shape
Dim connectedShapeIDs() As Long
Dim connectorIDs() As Long
Dim intCount As Integer
' Highlight the selected shape
Set vsoShape = ActiveWindow.Selection(1)
vsoShape.CellsU("Fillforegnd").FormulaU = "RGB(146, 212, 0)"
vsoShape.Cells("LineColor").FormulaU = "RGB(168,0,0)"
vsoShape.Cells("LineWeight").Formula = "2.5 pt"
' Highlight connectors from/to the selected shape
connectorIDs = vsoShape.GluedShapes _
(visGluedShapesAll1D, "")
For intCount = 0 To UBound(connectorIDs)
ActivePage.Shapes.ItemFromID(connectorIDs(intCount)).Cells("LineColor").FormulaU = "RGB(168,0,0)"
ActivePage.Shapes.ItemFromID(connectorIDs(intCount)).Cells("LineWeight").Formula = "2.5 pt"
Next
' Highlight shapes that are connected to the selected shape
connectedShapeIDs = vsoShape.connectedShapes(visConnectedShapesAllNodes, "")
For intCount = 0 To UBound(connectedShapeIDs)
ActivePage.Shapes.ItemFromID(connectedShapeIDs(intCount)).Cells("LineColor").FormulaU = "RGB(168,0,0)"
ActivePage.Shapes.ItemFromID(connectedShapeIDs(intCount)).Cells("LineWeight").Formula = "2.5 pt"
Next
End Sub
To run the macro, you can consider associating with double-click behavior of shapes.
If you only need to highlight incoming/outgoing connectors and incoming/outgoing shapes, replace visGluedShapesAll1D with visGluedShapesIncoming1D/visGluedShapesOutgoing1D and visConnectedShapesAllNodes with visConnectedShapesIncomingNodes/visConnectedShapesOutgoingNodes.
Learn more at visgluedshapesflags and visconnectedshapesflags. Good luck!
The following code will loop though all 1d-Shapes glued to the first shape in your Selection and write their name to the Immediate window. This should be a good starting point.
Visio has no Event that fires if a Shape is selected (at least not without some workarounds), so maybe bind the macro to a keybind.
The visGluedShapesAll1D flag can be replace with another filter as described here: Microsoft Office Reference
Sub colorConnectors()
If ActiveWindow.Selection(1) Is Nothing Then Exit Sub
Dim selectedShape As Shape
Set selectedShape = ActiveWindow.Selection(1)
Dim pg As Page
Set pg = ActivePage
Dim gluedConnectorID As Variant 'variant is needed because of "For Each" Loop
For Each gluedConnectorID In selectedShape.GluedShapes(visGluedShapesAll1D, "")
Debug.Print pg.Shapes.ItemFromID(gluedConnectorID).NameU
Next gluedConnectorID
End Sub

catia vba Drafting sheet format frame display

I have a little macro that adds a new sheet to a drawing intended for creating a .dxf file for the laser cut-out of sheet-metal parts.
Sub CATMain()
Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument
Dim drawingSheets1 As DrawingSheets
Set drawingSheets1 = drawingDocument1.Sheets
Dim drawingSheet1 As DrawingSheet
Set drawingSheet1 = drawingSheets1.Add("Laser")
drawingSheet1.PaperSize = catPaperA0
drawingSheet1.[Scale] = 1#
drawingSheet1.Orientation = catPaperLandscape
CATIA.StartCommand "Unfolded View"
End Sub
I'd like to make an addition to this macro where it removes the border of the sheet format.
The manual method of doing this is shown in the following screenshot:
So I either need to find a VBA command to untick that box, or a command to use Sheet Style "NoBorderTest" (as seen in the screenshot).
I couldn't find a way to do either, any help would be appreciated.
I'm trying to accomplish exactly the same thing you are, I'm making drawings that contain a view scaled 1:1 that can be exported as a DXF for 3 axis machining. The format is very annoying, it makes it difficult to see the part profile if its size is similar to the paper dimensions. The drawing format "shadow" hides the geometry.
The work around that I came up with was to set the paper height and paper width to very small numbers, 0.0000001 seemed to work fine. The paper height and paper width properties are exposed APIs that you can work with:
Dim DXFRoot As DrawingRoot = DXFRepRef.GetItem("CATDrawingAccess")
Dim DXFSheets As DrawingSheets = DXFRoot.Sheets
Dim DXFSheet As DrawingSheet = DXFSheets.ActiveSheet
DXFSheet.PaperSize = CatPaperSize.catPaperUser
DXFSheet.Scale = 1
DXFSheet.SetPaperHeight(0.0000001)
DXFSheet.SetPaperWidth(0.0000001)

Change connector style

I want to highlight connectors of a Shape object, but it doesn't give me any LineStyle properties for the connector itself. Here's what I got so far:
For i = 0 To UBound(lngShapeIDs)
Dim shp As Shape
Dim connect As connect
Set shp = ActivePage.Shapes.ItemFromID(lngShapeIDs(i))
shp.LineStyle = "Guide"
shp.BringToFront
Set connect = shp.FromConnects.Item(i + 1)
Next
Based in your comments this is what I think you are looking for
Dim shape As shape
For Each shape In ActivePage.Shapes
If (shape.OneD <> 0) Then
shape.CellsU("LineColor").Formula = "rgb(255,0,0)"
shape.BringToFront
End If
Next
This sample enumerates through all the shapes in the page. It assumes and "1-D" shapes are connectors that need to be modified. For those connectors, their line color is set to red and they are each brought to the front.
So if this was the initial state of the drawing:
Then after running the VBA code, the drawing will look like this: