2013-10-31 6 views
2

Visio를 처음 사용하는 VBA 사용자는 여기! I의 Visio 2010을 사용하고Visio 2010 VBA 자동 연결

내가 VBA를 사용하여 시스템 아키텍처 다이어그램의 드로잉을 자동화하기 위해 노력하고

프로. 데이터 원본은 Excel 시트입니다. (! 모두 감사합니다) Hopefully this is the result...

나는 엑셀 시트를 읽을 VBA를 작성했습니다, 그리고 인터넷의 도움의 비트와 함께 페이지의 모양을 만들 수 있습니다

을 내가 데리고보고 된 경로이었다

  • 드롭 레코드를 통해 자동 연결 루프를 사용
  • 제 시스템의 각 목적과 가입일
    • 시스템 사이 (통합을 도시)의 링크를 그리는 Excel 데이터에서 링크는 연결된 도형의 이름을 알고 있습니다 (페이지에 도형을 놓을 때 shape.name을 할당합니다).

내가 할 수있는 더 좋은 또는 쉬운 방법이 있나요

(자동 연결 방법에 대한 매개 변수로 사용될 수) 독특한 모양의 물체를 식별 할 모양의 이름을 사용하는 방법을 모른다 이?

자동 연결 예제 (http://msdn.microsoft.com/en-us/library/office/ms427221%28v=office.12%29.aspx)를 보았습니다. 어떤 개체가 런타임에 만든 개체에 대한 핸들 (즉, 각 개체에 대한 변수를 만든 경우. 내 경우에는, 나는 어디에도 저장하지 않습니다.) 나는이 정보를 배열에 저장 한 다음 동일한 루핑을 고려했습니다. 개체를 찾을 수 있습니다.

나는 내가 Visio가 안돼서 감안할.이 작업을 수행하는 가장 좋은 방법에 관해서는 어떤 생각을하고 싶습니다, 일부 샘플 (작업은?) 코드를 잘 수신 할 수있다.

코드 나는 특히 정렬에 관심이 있습니다. "도형을 연결하십시오."라고 주석을 달았습니다.

VBA를 실행할 때마다 새로운 스텐실이 만들어집니다. 마스터하지 않고 마스터 에스?

감사합니다.

나는/내가 작성한 코드를 첨부 정보 사람들이 내가 그렇게 달성하기 위해 노력하고 무엇인지에 아이디어를 얻을 필요가 얼마나 확실하지 않았다 해킹/날짜

Public Sub DrawSystem() 

Dim strConnection As String 
Dim strCommand As String 
Dim vsoDataRecordset As Visio.DataRecordset 

strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _ 
        & "User ID=Admin;" _ 
        & "Data Source=" + "b:\visio\Objects2;" _ 
        & "Mode=Read;" _ 
        & "Extended Properties=""HDR=YES;IMEX=1;MaxScanRows=0;Excel 12.0;"";" _ 
        & "Jet OLEDB:Engine Type=34;" 

strCommand = "SELECT * FROM [Sheet1$]" 

' load the data ... 
Set vsoDataRecordset = ActiveDocument.DataRecordsets.Add(strConnection, strCommand, 0, "Objects") 

'Stencil document that contains master 
Dim stnObj As Visio.Document 
'Master to drop 
Dim mastObj As Visio.Master 
'Pages collection of document 
Dim pagsObj As Visio.Pages 
'Page to work in 
Dim pagObj, activePageObj As Visio.Page 
'Instance of master on page 
Dim shpObj As Visio.Shape 
Dim shpFrom As Variant 
Dim shpTo As Variant 

Set stnObj = Documents.Add("Basic Shapes.vss") 

' create a new page in the document 
Set pagObj = ThisDocument.Pages.Add 
pagObj.Name = "Page-" & Pages.Count 

' ------------------------------------------------------- 
' LOOP THROUGH THE RECORDSET 
' ------------------------------------------------------- 
Dim lngRowIDs() As Long 
Dim lngRow As Long 
Dim lngColumn As Long 
Dim varRowData As Variant 

' process the ENTITY records 
Debug.Print "PROCESSING ENTITY RECORDS" 
lngRowIDs = vsoDataRecordset.GetDataRowIDs("") 

' draw rectangles for systems 
Set mastObj = stnObj.Masters("Rectangle") 

'Iterate through all the records in the recordset. 
For lngRow = LBound(lngRowIDs) To UBound(lngRowIDs) 

    varRowData = vsoDataRecordset.GetRowData(lngRow) 

    If varRowData(2) = "ENTITY" Then 

     ' draw a new object on the created page with the correct details 
     ' TODO - work out how to programmatically draw them in an appropriate location 
     Set shpObj = pagObj.Drop(mastObj, lngRow/2, lngRow/2) 

     ' set the appropriate attributes on the new object from the dataset 
     shpObj.Name = varRowData(3) 
     shpObj.Text = varRowData(7) 
     shpObj.data1 = varRowData(3) 
     shpObj.data2 = varRowData(7) 
     shpObj.Data3 = varRowData(8) 

     shpObj.Cells("Width") = 0.75 
     shpObj.Cells("Height") = 0.5 

     Debug.Print ("Created Object: " & varRowData(3) & " : ID = " & shpObj.ID) 
    Else 
     Debug.Print ("SKIPPED:" & varRowData(2) & " : " & varRowData(0)) 
    End If 

Next lngRow 

' process the LINK records 
Debug.Print "PROCESSING LINK RECORDS" 
lngRowIDs = vsoDataRecordset.GetDataRowIDs("") 

Set mastObj = stnObj.Masters("Dynamic Connector") 

'Iterate through all the records in the recordset. 
For lngRow = LBound(lngRowIDs) To UBound(lngRowIDs) 

    ' only process LINK records 
    If varRowData(2) = "LINK" Then 

     Debug.Print ("Joining! " & varRowData(4) & " - " & varRowData(5) & " with " & varRowData(6)) 

     Set shpObj = pagObj.Drop(mastObj, 2 + lngRow * 3, 0 + lngRow * 3) 
     varRowData = vsoDataRecordset.GetRowData(lngRow) 

     shpObj.Name = varRowData(6) 
     shpObj.Text = varRowData(7) 

     ' connect the shapes ... 
     shpFrom = activePageObj.Shapes(varRowData(4)) 
     shpTo = activePageObj.Shapes(varRowData(5)) 
     shpFrom.AutoConnect shpTo, visAutoConnectDirNone 

    Else 
     Debug.Print ("LINK SKIPPED:" & varRowData(2) & " : " & varRowData(0)) 
    End If 

Next lngRow 

에 표절 ... 여기

내가 테스트하는 데 사용 된 데이터 파일입니다 하위 (복사 및 Excel로 붙여 넣기) 종료

1,,ENTITY,A,,,1,1: A,ONE 
2,,ENTITY,B,,,2,2: B,TWO 
3,,ENTITY,C,,,3,3: C,THREE 
13,1,LINK,LINK1,A,B,13.1,13.1: LINK1,LINK1 
13,2,LINK,LINK2,A,C,13.2,13.2: LINK2,LINK2 
13,2,LINK,LINK2,C,B,13.2,13.2: LINK2,LINK2 

답변

0

를이 코드는 당신을 위해 일해야합니다

Public Sub DrawSystem() 

Dim strConnection As String 
Dim strCommand As String 
Dim vsoDataRecordset As Visio.DataRecordset 

strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _ 
        & "User ID=Admin;" _ 
        & "Data Source=" + "d:\Book1.xlsx;" _ 
        & "Mode=Read;" _ 
        & "Extended Properties=""HDR=YES;IMEX=1;MaxScanRows=0;Excel 12.0;"";" _ 
        & "Jet OLEDB:Engine Type=34;" 

strCommand = "SELECT * FROM [Sheet1$]" 

Set vsoDataRecordset = ActiveDocument.DataRecordsets.Add(strConnection, strCommand, 0, "Objects") 

Dim stnObj As Visio.Document 
Dim mastObj As Visio.Master 
Dim pagsObj As Visio.Pages 
Dim pagObj, activePageObj As Visio.Page 
Dim shpObj As Visio.Shape 
Dim shpFrom As Visio.Shape 
Dim shpTo As Visio.Shape 

Set stnObj = Documents.OpenEx("Basic Shapes.vss", visOpenDocked) 

Set pagObj = ThisDocument.Pages.Add() 

Dim lngRowIDs() As Long 
Dim lngRow As Long 
Dim lngColumn As Long 
Dim varRowData As Variant 

Debug.Print "PROCESSING ENTITY RECORDS" 
lngRowIDs = vsoDataRecordset.GetDataRowIDs("") 

Set mastObj = stnObj.Masters("Rectangle") 

For lngRow = LBound(lngRowIDs) To UBound(lngRowIDs) 

    varRowData = vsoDataRecordset.GetRowData(lngRow) 

    If varRowData(2) = "ENTITY" Then 

     Set shpObj = pagObj.Drop(mastObj, lngRow/2, lngRow/2) 

     shpObj.Name = varRowData(3) 
     shpObj.Text = varRowData(7) 
     shpObj.Data1 = varRowData(3) 
     shpObj.Data2 = varRowData(7) 
     shpObj.Data3 = varRowData(8) 

     shpObj.Cells("Width") = 0.75 
     shpObj.Cells("Height") = 0.5 

    End If 

Next lngRow 

lngRowIDs = vsoDataRecordset.GetDataRowIDs("") 

Set mastObj = stnObj.Masters("Dynamic Connector") 

For lngRow = LBound(lngRowIDs) To UBound(lngRowIDs) 

    varRowData = vsoDataRecordset.GetRowData(lngRow) 
    Debug.Print ("!ddd!!" & varRowData(2)) 

    If varRowData(2) = "LINK" Then 

     Dim fromName As String 
     fromName = varRowData(4) 

     Dim toName As String 
     toName = varRowData(5) 

     Dim conName As String 
     conName = varRowData(6) 


     Set shpCon = pagObj.Drop(mastObj, 2 + lngRow * 3, 0 + lngRow * 3) 
     varRowData = vsoDataRecordset.GetRowData(lngRow) 

     shpCon.Name = conName 
     shpCon.Text = varRowData(7) 

     Set shpFrom = ActivePage.Shapes(fromName) 
     Set shpTo = ActivePage.Shapes(toName) 
     shpFrom.AutoConnect shpTo, visAutoConnectDirNone, shpCon 
    End If 

Next lngRow 
End Sub 
+0

감사합니다. Saveenr. 그건 완벽하게 작동합니다. 나는 디버깅을 시도하기 전에 몇 시간을 보냈으며 의심의 여지없이 당신의 노력으로 저를 조금 더 절약 할 수있었습니다. 다시 한번 감사합니다. 엠. – Markus