Hi,
I've taken the code above, converted it to VB (hopefully!), and used the diagram file you had in the zip file.
Now, when I run the code, it falls over at the 'save' method, with the error:
Error in element Shapes in \VisioDocument\Pages\Page[0]. Object reference not set to an instance of an object.
The code I have used is:
(Note: I had to remove .ID from the line connector.Master = dynamicConnectorMaster.ID;)
Can you confirm whether the problem is with my code translation, or something else?
Many Thanks!
<?XML:NAMESPACE PREFIX = O />
Dim vsdDiagram As Diagram
Private Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
Dim st As New FileStream(Server.MapPath("~\Drawing1.vdx"), FileMode.Open)
vsdDiagram = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Diagram</SPAN>(st)
PopulateDiagram2()
<SPAN style="COLOR: green">'Save inpupt VSD as VDX</SPAN>
vsdDiagram.Save(<SPAN style="COLOR: #a31515">"Drawing3.vdx"</SPAN>, <SPAN style="COLOR: #2b91af">SaveFileFormat</SPAN>.VDX)
st.Close()
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">Sub</SPAN></PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"><SPAN style="COLOR: blue"><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"><SPAN style="COLOR: blue">Private</SPAN> <SPAN style="COLOR: blue">Sub</SPAN> PopulateDiagram2()
<SPAN style="COLOR: blue">Dim</SPAN> dynamicConnectorMaster <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Master</SPAN>
<SPAN style="COLOR: green">'Search for the dynamic connector master.</SPAN>
<SPAN style="COLOR: blue">For</SPAN> <SPAN style="COLOR: blue">Each</SPAN> oMaster <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Master</SPAN> <SPAN style="COLOR: blue">In</SPAN> vsdDiagram.Masters()
<SPAN style="COLOR: blue">If</SPAN> oMaster.Name = <SPAN style="COLOR: #a31515">"Dynamic connector"</SPAN> <SPAN style="COLOR: blue">Then</SPAN>
dynamicConnectorMaster = oMaster
<SPAN style="COLOR: blue">Exit For</SPAN>
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>
<SPAN style="COLOR: blue">Next</SPAN>
<SPAN style="COLOR: green">'Now we can not read the master from stencil, so exit if the master was not found.</SPAN>
<SPAN style="COLOR: blue">If</SPAN> dynamicConnectorMaster <SPAN style="COLOR: blue">Is</SPAN> <SPAN style="COLOR: blue">Nothing</SPAN> <SPAN style="COLOR: blue">Then</SPAN>
<SPAN style="COLOR: blue">Throw</SPAN> <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">ApplicationException</SPAN>(<SPAN style="COLOR: #a31515">"The dynamic connector master was not found."</SPAN>)
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>
<SPAN style="COLOR: blue">Dim</SPAN> oPage <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Page</SPAN> = vsdDiagram.Pages(0)
<SPAN style="COLOR: blue">Dim</SPAN> shape1 <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN> = findShapeByID(oPage, 1)
<SPAN style="COLOR: blue">Dim</SPAN> oConnection <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN> = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
oConnection.IX = 0
oConnection.X.Value = shape1.XForm.LocPinX.Value
oConnection.Y.Value = 0
oConnection.DirX.Value = 0
oConnection.DirY.Value = 1
shape1.Connections.Add(oConnection)
oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
oConnection.IX = 1
oConnection.X.Value = shape1.XForm.Width.Value
oConnection.Y.Value = shape1.XForm.LocPinY.Value
oConnection.DirX.Value = -1
oConnection.DirY.Value = 0
shape1.Connections.Add(oConnection)
oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
oConnection.IX = 2
oConnection.X.Value = shape1.XForm.LocPinX.Value
oConnection.Y.Value = shape1.XForm.Height.Value
oConnection.DirX.Value = 0
oConnection.DirY.Value = -1
shape1.Connections.Add(oConnection)
oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
oConnection.IX = 3
oConnection.X.Value = 0
oConnection.Y.Value = shape1.XForm.LocPinY.Value
oConnection.DirX.Value = 1
oConnection.DirY.Value = 0
shape1.Connections.Add(oConnection)
<SPAN style="COLOR: blue">Dim</SPAN> shape2 <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN> = findShapeByID(oPage, 2)
oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
oConnection.IX = 0
oConnection.X.Value = shape2.XForm.LocPinX.Value
oConnection.Y.Value = 0
oConnection.DirX.Value = 0
oConnection.DirY.Value = 1
shape2.Connections.Add(oConnection)
oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
oConnection.IX = 1
oConnection.X.Value = shape2.XForm.Width.Value
oConnection.Y.Value = shape2.XForm.LocPinY.Value
oConnection.DirX.Value = -1
oConnection.DirY.Value = 0
shape2.Connections.Add(oConnection)
oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
oConnection.IX = 2
oConnection.X.Value = shape2.XForm.LocPinX.Value
oConnection.Y.Value = shape2.XForm.Height.Value
oConnection.DirX.Value = 0
oConnection.DirY.Value = -1
shape2.Connections.Add(oConnection)
oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
oConnection.IX = 3
oConnection.X.Value = 0
oConnection.Y.Value = shape2.XForm.LocPinY.Value
oConnection.DirX.Value = 1
oConnection.DirY.Value = 0
shape2.Connections.Add(oConnection)
<SPAN style="COLOR: blue">Dim</SPAN> oConnector <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN> = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN>()
oConnector.Master = dynamicConnectorMaster <SPAN style="COLOR: green">' HAD TO REMOVE .ID FROM THIS LINE</SPAN>
oConnector.ID = 3 <SPAN style="COLOR: green">'normally you have to find the maximum of shape IDs and take next value</SPAN>
oConnector.Type = <SPAN style="COLOR: #2b91af">TypeValue</SPAN>.Shape
oConnector.XForm1D.BeginX.Ufe.F = <SPAN style="COLOR: blue">String</SPAN>.Format(<SPAN style="COLOR: #a31515">"PAR(PNT({0}!Connections.X1,{1}!Connections.Y1))"</SPAN>, _
ShapeToFormulaString(shape1, vsdDiagram), ShapeToFormulaString(shape1, vsdDiagram))
oConnector.XForm1D.BeginY.Ufe.F = <SPAN style="COLOR: blue">String</SPAN>.Format(<SPAN style="COLOR: #a31515">"PAR(PNT({0}!Connections.X1,{1}!Connections.Y1))"</SPAN>, _
ShapeToFormulaString(shape1, vsdDiagram), ShapeToFormulaString(shape1, vsdDiagram))
oConnector.XForm1D.EndX.Ufe.F = <SPAN style="COLOR: blue">String</SPAN>.Format(<SPAN style="COLOR: #a31515">"PAR(PNT({0}!Connections.X3,{1}!Connections.Y3))"</SPAN>, _
ShapeToFormulaString(shape2, vsdDiagram), ShapeToFormulaString(shape2, vsdDiagram))
oConnector.XForm1D.EndY.Ufe.F = <SPAN style="COLOR: blue">String</SPAN>.Format(<SPAN style="COLOR: #a31515">"PAR(PNT({0}!Connections.X3,{1}!Connections.Y3))"</SPAN>, _
ShapeToFormulaString(shape2, vsdDiagram), ShapeToFormulaString(shape2, vsdDiagram))
oPage.Shapes.Add(oConnector)
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">Sub</SPAN></PRE></SPAN></PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"><SPAN style="COLOR: blue"><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"> <SPAN style="COLOR: blue">Private</SPAN> <SPAN style="COLOR: blue">Function</SPAN> findShapeByID(<SPAN style="COLOR: blue">ByVal</SPAN> oPage <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Page</SPAN>, <SPAN style="COLOR: blue">ByVal</SPAN> id <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Integer</SPAN>) <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN>
<SPAN style="COLOR: blue">For</SPAN> <SPAN style="COLOR: blue">Each</SPAN> oShape <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN> <SPAN style="COLOR: blue">In</SPAN> oPage.Shapes
<SPAN style="COLOR: blue">If</SPAN> oShape.ID = id <SPAN style="COLOR: blue">Then</SPAN>
<SPAN style="COLOR: blue">Return</SPAN> oShape
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>
<SPAN style="COLOR: blue">Next</SPAN>
<SPAN style="COLOR: blue">Throw</SPAN> <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">ApplicationException</SPAN>(<SPAN style="COLOR: blue">String</SPAN>.Format(<SPAN style="COLOR: #a31515">"The shape with ID = '{0}' was not found."</SPAN>, id))
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">Function</SPAN>
<SPAN style="COLOR: blue">Private</SPAN> <SPAN style="COLOR: blue">Function</SPAN> ShapeToFormulaString(<SPAN style="COLOR: blue">ByVal</SPAN> oShape <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN>, <SPAN style="COLOR: blue">ByVal</SPAN> oDiagram <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Diagram</SPAN>) <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">String</SPAN>
<SPAN style="COLOR: blue">Dim</SPAN> nameU <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">String</SPAN> = oShape.NameU
<SPAN style="COLOR: blue">If</SPAN> (nameU & <SPAN style="COLOR: #a31515">""</SPAN>).Length > 0 <SPAN style="COLOR: blue">Then</SPAN>
<SPAN style="COLOR: blue">Return</SPAN> nameU
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>
<SPAN style="COLOR: blue">For</SPAN> <SPAN style="COLOR: blue">Each</SPAN> oMaster <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Master</SPAN> <SPAN style="COLOR: blue">In</SPAN> vsdDiagram.Masters
<SPAN style="COLOR: blue">If</SPAN> oMaster.ID = oShape.Master.ID <SPAN style="COLOR: blue">Then</SPAN>
<SPAN style="COLOR: blue">Return</SPAN> oMaster.NameU + <SPAN style="COLOR: #a31515">"."</SPAN> & oShape.ID
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>
<SPAN style="COLOR: blue">Next</SPAN>
<SPAN style="COLOR: blue">Throw</SPAN> <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">ApplicationException</SPAN>(<SPAN style="COLOR: #a31515">"Cannot convert shape to its formula string."</SPAN>)
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">Function</SPAN></PRE></SPAN></PRE>