2017-04-14 65 views

回答

2

將圖像導入到Visio中時,它們會被包裝成標準形狀(具有visTypeForeignObject的Type屬性)。

從那裏你只是在與ShapeSheet中的單元格交談。 (有關ShapeSheet http://visualsignals.typepad.co.uk/vislog/2007/10/just-for-starte.html的更多詳細信息,請參閱此帖子)

因此,您可以從Visio上的宏記錄器開始使用這類事物。圖片拖動到頁面上,會產生輸出是這樣的:

Sub Macro1() 

    'Enable diagram services 
    Dim DiagramServices As Integer 
    DiagramServices = ActiveDocument.DiagramServicesEnabled 
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150 

    Dim UndoScopeID2 As Long 
    UndoScopeID2 = Application.BeginUndoScope("Auto Size Page") 
    Application.ActiveWindow.Page.AutoSize = False 
    Application.EndUndoScope UndoScopeID2, True 

    Dim UndoScopeID3 As Long 
    UndoScopeID3 = Application.BeginUndoScope("Insert") 
    Application.ActiveWindow.Page.Import "C:\SomeImage.jpg" 
    Application.EndUndoScope UndoScopeID3, True 

    ActiveWindow.DeselectAll 
    ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(1), visSelect 
    Application.ActiveWindow.Selection.Move 2.129396, -0.904364 

    Dim UndoScopeID4 As Long 
    UndoScopeID4 = Application.BeginUndoScope("Size Object") 
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormPinX).FormulaU = "46.261665987369 mm" 
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormPinY).FormulaU = "212.02916285428 mm" 
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormWidth).FormulaU = "103.47666530807 mm" 
    Application.EndUndoScope UndoScopeID4, True 

    Dim UndoScopeID5 As Long 
    UndoScopeID5 = Application.BeginUndoScope("Size Object") 
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormPinX).FormulaU = "46.261665987369 mm" 
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormPinY).FormulaU = "185.77916321819 mm" 
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormHeight).FormulaU = "73.441667394486 mm" 
    Application.EndUndoScope UndoScopeID5, True 

    'Restore diagram services 
    ActiveDocument.DiagramServicesEnabled = DiagramServices 

End Sub 

宏錄製在當前選擇上操作,但你並不需要。此外,它使用SRC(節,行,列)語法,而不是更簡單的單元名稱語法。所以的翻譯上面可能是這樣的:

Sub TestAddImage() 
    Call DropImage(ActivePage, "C:\SomeImage.jpg") 
End Sub 

Private Sub DropImage(ByRef vPag As Visio.Page, imageFile As String) 

If Not vPag Is Nothing Then 
    Dim newShp As Visio.Shape 
    Set shpNew = vPag.Import(imageFile) 
    'Set position 
    shpNew.CellsU("PinX").FormulaU = "75mm" 
    shpNew.CellsU("PinY").FormulaU = "175mm" 
    'Set size 
    shpNew.CellsU("Width").FormulaU = "100mm" 
    shpNew.CellsU("Height").FormulaU = "80mm" 
End If 

End Sub 

C#的這個版本是這樣的:

void Main() 
{ 
    var vApp = MyExtensions.GetRunningVisio(); 
    DropImage(vApp.ActivePage, @"C:\SomeImage.jpg"); 
} 

private void DropImage(Visio.Page vPag, string imageFile) 
{ 
    if (vPag != null) 
    { 
     var shpNew = vPag.Import(imageFile); 
     //Set position 
     shpNew.CellsU["PinX"].FormulaU = "75mm"; 
     shpNew.CellsU["PinY"].FormulaU = "175mm"; 
     //Set size 
     shpNew.CellsU["Width"].FormulaU = "100mm"; 
     shpNew.CellsU["Height"].FormulaU = "80mm"; 
    } 
} 

注意GetRunningVisio是我與LinqPad使用擴展方法:

http://visualsignals.typepad.co.uk/vislog/2015/12/getting-started-with-c-in-linqpad-with-visio.html

......但這取決於你如何得到應用程序對象。

+0

約翰,那太棒了,它工作完美。但有什麼內置的將形狀放置在頁面的末尾,如shpNew.cellsu.end?或者是否需要運行一個宏來查找您在「sub Macro1」中顯示的座標? – Puneeth

+1

爲此,您需要向PinX/Y單元添加公式而不是常量值。對於PinX:'「ThePage!PageWidth-(Width-LocPinX)」'和PinY'「ThePage!PageHeight-(Height-LocPinY)」'。這將設置頁面左上角的形狀。 – JohnGoldsmith

+0

對不起,我的問題是放在頁面的底部,我相信用+修改公式會在頁面底部設置形狀,不是嗎? – Puneeth

相關問題