Option Explicit
Dim AppVisio As Object
Sub VisioFromExcel()
Dim oCharacters As Object
Dim lRowIndex As Long
Dim sChar As String
Dim sMaster As String
Dim sngXPos As Single
Dim sngYPos As Single
Dim lShapeCount As Long
Dim lXOffset As Long
Dim varItem As Variant
Dim lShapeIndex As Long
'calculate x & y positions to place shapes on a grid with 1.25 inch spacing, 6 across
Set AppVisio = CreateObject("visio.application")
AppVisio.Visible = True
AppVisio.Documents.AddEx "basicd_u.vst", 0, 0 ',0,=visMSDefault
'Open Stencils
AppVisio.Documents.OpenEx "comps_u.vss", 2 + 4 'visOpenRO + visOpenDocked
AppVisio.Documents.OpenEx "periph_u.vss", 2 + 4 'visOpenRO + visOpenDocked
AppVisio.Documents.OpenEx "basic_u.vss", 2 + 4 'visOpenRO + visOpenDocked
For lRowIndex = 2 To Cells(Rows.Count, 1).End(xlUp).Row
lShapeCount = lShapeCount + 1
lXOffset = lXOffset + 1
If lXOffset = 7 Then lXOffset = 1
sngYPos = 0.75 + 1.25 * (Int((lShapeCount - 0.1) / 6))
sngXPos = 0.75 + 1.25 * (lXOffset - 1)
varItem = Range(Cells(lRowIndex, 1), Cells(lRowIndex, 10))
varItem(1, 9) = sngXPos
varItem(1, 10) = sngYPos
'varItem (1,1)Device Number (1,2...1,8)Description 1...7 (1,9)Xpos (1,10)YPos
Select Case varItem(1, 7)
Case "PC"
AppVisio.ActiveWindow.Page.Drop AppVisio.Documents.Item _
("COMPS_U.VSS").Masters.ItemU("PC"), varItem(1, 9), varItem(1, 10)
Case "Switch"
AppVisio.ActiveWindow.Page.Drop AppVisio.Documents.Item _
("PERIPH_U.VSS").Masters.ItemU("Switch"), varItem(1, 9), varItem(1, 10)
Case Else
Debug.Print "Not PC or Switch: " & varItem(1, 1), varItem(1, 7)
End Select
'Get ID of last shape dropped
lShapeIndex = AppVisio.ActiveWindow.Selection.PrimaryItem.ID
'Open Shape Sheet
AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lShapeIndex).OpenSheetWindow
'Add Shape Properties - Update the names to match the values in the Visio 13 Stencils
ShapeData_Update "AssetNumber", CStr(varItem(1, 1)) 'Device_Number
ShapeData_Update "NetworkName", CStr(varItem(1, 2)) 'Description_1 = Device Name
ShapeData_Update "Manufacturer", CStr(varItem(1, 3)) 'Description_2 = Manufacturer
ShapeData_Update "ProductNumber", CStr(varItem(1, 4)) 'Description_3 = Model Number (Not in Visio10 Stencil)
ShapeData_Update "IPAddress", CStr(varItem(1, 5)) 'Description_4 = IP Address
ShapeData_Update "MACAddress", CStr(varItem(1, 6)) 'Description_5 = MAC Address
ShapeData_Update "ProductDescription", CStr(varItem(1, 7)) 'Description_6 = Purpose
'ShapeData_Update "Description_7", CStr(varItem(1, 8)) 'Description_7 << Not used at this time >>
'Add User Properties
UserRow_Add "Device_Number", CStr(varItem(1, 1))
UserRow_Add "Description_1", CStr(varItem(1, 2))
UserRow_Add "Description_2", CStr(varItem(1, 3))
UserRow_Add "Description_3", CStr(varItem(1, 4))
UserRow_Add "Description_4", CStr(varItem(1, 5))
UserRow_Add "Description_5", CStr(varItem(1, 6))
UserRow_Add "Description_6", CStr(varItem(1, 7))
UserRow_Add "Description_7", CStr(varItem(1, 8))
'Close Shape Sheet
AppVisio.ActiveWindow.Close
Next
'Add Title at specified PinX, Piny
AppVisio.ActiveWindow.Page.Drop AppVisio.Documents.Item("BASIC_U.VSS").Masters.ItemU("Rectangle"), _
AppVisio.ActivePage.PageSheet.Cells("pagewidth").ResultIU / 2, sngYPos + 1.25
lShapeIndex = AppVisio.ActiveWindow.Selection.PrimaryItem.ID
'Modify Title Size
'AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lShapeIndex).CellsSRC(1, 1, 1).FormulaU = sngYPos + 1.25 'PinY
'AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lShapeIndex).CellsSRC(1, 1, 0).FormulaU = _
AppVisio.ActivePage.PageSheet.Cells("pagewidth").ResultIU / 2 'PinX
AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lShapeIndex).CellsSRC(1, 1, 2).FormulaU = _
AppVisio.ActivePage.PageSheet.Cells("pagewidth").ResultIU - 1 'FormWidth
AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lShapeIndex).CellsSRC(1, 1, 3).FormulaU = 1 'FormHeight
'Modify Title Characters
Set oCharacters = AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lShapeIndex).Characters
oCharacters.Begin = 0
oCharacters.End = Len(oCharacters)
sChar = Cells(lRowIndex, 1).Value
oCharacters.text = Range("I1").Value
'oCharacters.Size = 24
AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lShapeIndex).Cells("Char.Size").Formula = "20 pt"
Set oCharacters = Nothing
Set AppVisio = Nothing
End Sub
Sub AddShape(varItem As Variant)
'varItem (0)Device Number (1...7)Description 1...7 (8)Xpos (9)YPos
Dim sMaster As String
Select Case varItem(6)
Case "PC"
Application.ActiveWindow.Page.Drop Application.Documents.Item _
("COMPS_U.VSS").Masters.ItemU("PC"), varItem(8), varItem(9)
Case "Switch"
Application.ActiveWindow.Page.Drop Application.Documents.Item _
("PERIPH_U.VSS").Masters.ItemU("Switch"), varItem(8), varItem(9)
Case Else
Debug.Print "Not PC or Switch: " & varItem(1), varItem(6)
End Select
'Open Shape Sheet
Application.ActiveWindow.Page.Shapes.ItemFromID(Application.ActiveWindow.Page.Shapes.Count).OpenSheetWindow
'Add User Properties
UserRow_Add "Device_Number", CStr(varItem(0))
UserRow_Add "Description_1", CStr(varItem(1))
UserRow_Add "Description_2", CStr(varItem(2))
UserRow_Add "Description_3", CStr(varItem(3))
UserRow_Add "Description_4", CStr(varItem(4))
UserRow_Add "Description_5", CStr(varItem(5))
UserRow_Add "Description_6", CStr(varItem(6))
UserRow_Add "Description_7", CStr(varItem(7))
'Close Shape Sheet
Application.ActiveWindow.Close
End Sub
Sub ShapeData_Update(sName As String, sValue As String)
'User Section has 3 columns: Name, Value, Prompt
Dim lRowIndex As Long
lRowIndex = AppVisio.ActiveWindow.Shape.CellsRowIndex("Prop." & sName)
AppVisio.ActiveWindow.Shape.Cells("Prop." & sName).Formula = _
Chr(34) & sValue & Chr(34) 'Value Column"
End Sub
Sub UserRow_Add(sName As String, sValue As String, Optional sPrompt As String)
'User Section has 3 columns: Name, Value, Prompt
Dim lRowIndex As Long
UserRow_Delete sName
AppVisio.ActiveWindow.Shape.AddNamedRow 242, sName, 0 '242=visSectionUser 0=visTagDefault
lRowIndex = AppVisio.ActiveWindow.Shape.CellsRowIndex("User." & sName)
AppVisio.ActiveWindow.Shape.Cells("User." & sName).Formula = _
Chr(34) & sValue & Chr(34) 'Value Column"
AppVisio.ActiveWindow.Shape.CellsSRC(242, lRowIndex, 1).FormulaU = _
Chr(34) & sPrompt & Chr(34) 'Prompt Column '242=visSectionUser 1=visCustPropsPrompt
End Sub
Sub UserRow_Delete(sName As String)
Dim lRowIndex As Long
If AppVisio.ActiveWindow.Shape.CellExists("User." & sName, True) Then
lRowIndex = AppVisio.ActiveWindow.Shape.CellsRowIndex("User." & sName)
AppVisio.ActiveWindow.Shape.DeleteRow 242, lRowIndex '242=visSectionUser
End If
End Sub