Resizing shapes with macro

Started by viki, December 09, 2011, 02:38:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

viki

Hi

I try to create the macro that would simply re-size all the selected shapes.  I got the Macro part simply by recording it.  I am missing the "Select" part to modify all the shapes I select in visio 2010.  I tried different combinations.  But in some cases the cells were guarded, in others few compile errors.
Sub Macro2()

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

    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Size & Position 2-D")
    Application.ActiveWindow.Page.Shapes.ItemFromID(6).CellsSRC(visSectionObject, visRowXFormOut, visXFormWidth).FormulaU = "1.25 in"
    Application.EndUndoScope UndoScopeID1, True

    Dim UndoScopeID2 As Long
    UndoScopeID2 = Application.BeginUndoScope("Size & Position 2-D")
    Application.ActiveWindow.Page.Shapes.ItemFromID(6).CellsSRC(visSectionObject, visRowXFormOut, visXFormHeight).FormulaU = "1.25 in"
    Application.EndUndoScope UndoScopeID2, True

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub


Could somebody please help me with this :-[

That is all code I have by now

aledlund

When a shape is dropped onto a page it is given a unique id for that page only. When you use "ItemFromID(6)" you are asking for shape number six, which may or may not be the shape you are looking for (it might not even be on the page). You have to wrap your shape modification code in something like this (don't forget to change this "Application.ActiveWindow.Page.Shapes.ItemFromID(6)" to this "visShape")

al



Public Sub selectionTest()

Dim visWindow As Visio.window
Set visWindow = Application.ActiveWindow
Dim visSelection As Visio.selection
Set visSelection = visWindow.selection
Dim visShape As Visio.Shape

If visSelection.Count > 0 Then

    For Each visShape In visSelection
        ' MsgBox visShape.ID
        ' do something with the selected shape
    Next visShape

End If

End Sub


viki

Quote from: aledlund on December 09, 2011, 03:30:40 PM
When a shape is dropped onto a page it is given a unique id for that page only. When you use "ItemFromID(6)" you are asking for shape number six, which may or may not be the shape you are looking for (it might not even be on the page). You have to wrap your shape modification code in something like this (don't forget to change this "Application.ActiveWindow.Page.Shapes.ItemFromID(6)" to this "visShape")

al



Public Sub selectionTest()

Dim visWindow As Visio.window
Set visWindow = Application.ActiveWindow
Dim visSelection As Visio.selection
Set visSelection = visWindow.selection
Dim visShape As Visio.Shape

If visSelection.Count > 0 Then

    For Each visShape In visSelection
        ' MsgBox visShape.ID
        ' do something with the selected shape
    Next visShape

End If

End Sub





It works just perfect! Thank you so much for your help!  :) :) :) 8)

There is just one but.  I am not sure if that is connected with the macros or anything, but once I apply macros few times, I lose "Undo" option for the entire document.  And if I change anything or delete by mistake, I seem to lose it forever as undo and redo errors are disabled.  Do you think it can be fixed in any way?

aledlund

I'd just remove the undoscope's from the macro.
al

viki

I did that and it worked out pretty well  :) Thank you VERY VERY much for your help. 

vojo

I suppose you do realize you can do this with precision without VBA

double click any of these to set dimensions
right click any of these for advanced features


viki

Quote from: vojo on December 12, 2011, 03:01:46 PM
I suppose you do realize you can do this with precision without VBA

double click any of these to set dimensions
right click any of these for advanced features

 Yes I do.  But that is rather a journey of a thousand miles.

And do you happen to know any good tutorial for VB.net Visio Application.  I have the basics of vb.net 2010, but those seem to be not enough for Visio macros... :)

aledlund

one of the beauties of the new v2010 sdk is that you can compare the same types of functions between vba and vb.net (since both have sample code). There are of course example projects in the sdk with the caveat that not all things may have equivalent projects. Some are in C# and some are in VB.NET. They tend to focus on add-ins and VSTO (I typically do most of mine in WinForms and the ActiveX drawing control).
al

saveenr

@viki - Visio has a built-in feature to resize all selected shapes and it requires no VBA script, code, or templates are needed.

In Visio 2010, under the View menu, Click the Task Panes button, and then enable the Size & Position task pane

Then select whatever shapes you want and then enter the width or height into the Size & Position task pane - the shapes will be resized to the exact dimensions you want as soon as you type in number. the Undo operation is fully supported - so you you need to revert any changes, Undo will restore all the shapes back to their previous dimensions

pavane

I have coded VBA macros which resize all shapes (height only) depending on the length of the text within the shape. It relies on the shape functions, and is not perfect, but better than nothing.