페이지

레이블이 Add-In인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Add-In인 게시물을 표시합니다. 모든 게시물 표시

2011년 2월 22일 화요일

Visio 2007 Programming: Add master and change its name when new shape is added to a page

This code replace master name with its shape name.
Originally master is named like master.. The following code changes the name, master. to shape name such as Square, Pentagon, and etc.

Private Sub Application_ShapeAdded(ByVal Shape As Microsoft.Office.Interop.Visio.Shape) Handles Application.ShapeAdded

Dim strShapeName As String = ""
Dim nCount As Integer

Dim nMasterCnt As Integer
Dim tempMaster As Visio.Master
Dim tempMasterName As String

' Check whether a same type of shape is already added
' If a same type of shape exists, the name of newly added shape will
' ShapeName.Number. The period mark will be used to determine
' the duplication of shapes and the number represents
' the total number of shapes in the document
strShapeName = Shape.NameU

' Place newly added shape to the stencil as a master
vsoStencil.Drop(Shape, 1, 1)

' Set name for newly added master
' Get the number of masters in the stencil
nMasterCnt = vsoStencil.Masters.Count
For i = 1 To nMasterCnt
tempMaster = vsoStencil.Masters.Item(i)
tempMasterName = tempMaster.Shapes.ItemU(1).NameU
If tempMasterName = Shape.NameU Then
vsoStencil.Masters.ItemU(i).Name = Shape.NameU
End If
Next
End Sub

2011년 2월 20일 일요일

Visio 2007 Programming: Change stencil title

Imports Microsoft.Office.Interop.Visio
Public Class ThisAddIn
Private vsoDoc As Visio.Document
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs ) _
Handles Me.Startup
vsoDoc = Application.Documents.Add("")
vsoDoc = Application.Documents.OpenEx("Basic Shapes.vss", _
VisOpenSaveArgs.visOpenDocked + VisOpenSaveArgs.visOpenCopy)
End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub
End Class

Above code creates a blank document with docked basic shape stencil. However, the title of stencil window is "Stencil 2" and the number is constantly changing whenever a new document is created. So, How do I set the title with a meaningful name?

To solve this problem, add following statement after a document is created or opened.

vsoDoc.Title = "Basic Shapes"

Visio 2007 Programming: Create new document with basic shape stencil

Following skeleton codes are generated through "New Project" -> Visual Basic -> Visio Add-in.

Public Class ThisAddIn
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs ) _
Handles Me.Startup

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub

End Class


1. Import Visio Interoperability reference before the class definition and define a variable, vsoDoc for Visio document.

Imports Microsoft.Office.Interop.Visio
Public Class ThisAddIn
Private vsoDoc As Visio.Document
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs ) _
Handles Me.Startup

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub

2. Add codes of Visio document creation in ThisAddIn_Startup()

Imports Microsoft.Office.Interop.Visio
Public Class ThisAddIn
Private vsoDoc As Visio.Document
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs ) _
Handles Me.Startup
vsoDoc = Application.Documents.Add("")
vsoDoc = Application.Documents.OpenEx("Basic Shapes.vss", _
visOpenDocked + visOpenCopy)

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub

3. To resolve errors with the flags visOpenDocked and visOpenCopy, append VisOpenSaveArgs to each flag. If VisOpenSaveArgs is not used, Visual Studio shows errors such as "Name 'visOpenDocked' is not declared."

Imports Microsoft.Office.Interop.Visio
Public Class ThisAddIn
Private vsoDoc As Visio.Document
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs ) _
Handles Me.Startup
vsoDoc = Application.Documents.Add("")
vsoDoc = Application.Documents.OpenEx("Basic Shapes.vss", _
VisOpenSaveArgs.visOpenDocked +
VisOpenSaveArgs.visOpenCopy)
End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub

P.S Test following codes and see the difference with the above codes

Imports Microsoft.Office.Interop.Visio
Public Class ThisAddIn
Private vsoDoc As Visio.Document
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Startup

vsoDoc = Application.Documents.Add("")
vsoDoc = Application.Documents.add("Basic Shapes.vss")

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Shutdown

End Sub

End Class