Find (and delete) images added from .emf files?

Started by KeithRuck, November 25, 2009, 10:31:31 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

KeithRuck

I recreate some org charts on a regular basis and use code to pull in updated images (.emf files) that are added to the org chart using the code at the bottom of this post.

Right now, I need to delete those images from an existing orgchart and replace them with modified ones. I'm good with VBA, but a novice to the Visio object model.

What it the best way to loop through all pages/shapes and find these shapes and delete them? Basically, how do I tell them apart from org chart boxes, connector lines, and other objects?

QuoteShpsCount = Application.ActivePage.Shapes.Count
    Set shpsObj = Application.ActivePage.Shapes
    For UseShpObj = ShpsCount To 1 Step -1
    Set shpObj = shpsObj(UseShpObj)
    If shpObj.Type = ??? Then shpObj.Delete
    Next UseShpObj

Thanks!
Keith

These images are added using:
                With Application.ActiveWindow
                    Set vsoShape = .Page.Import(MyFileName2)
                    On Error GoTo 0
                    vsoShape.Cells("PinX").FormulaU = b1
                    vsoShape.Cells("PinY").FormulaU = LowerPinY
                    vsoShape.Cells("Height").FormulaU = HalfHeight
                    vsoShape.Cells("Width").FormulaU = b4
                    vsoShape.SendToBack
                End With

KeithRuck

Ok, I managed to figure this out by debug.printing the names of each object, the found the one that had the right number of items for my page. I'm not sure this is the best way to do it, but it works. I welcome any suggestions for improvement in case I run into this problem again (including ways to flag these shapes when they are imported, so they will be clearly different from other shapes that might be pasted in manually)

Thanks!
Keith
Sub removeEMFshapes()
   
    ShpsCount = Application.ActivePage.Shapes.Count
    Set shpsObj = Application.ActivePage.Shapes ' root.Shapes
    For UseShpObj = ShpsCount To 1 Step -1
    Set shpObj = shpsObj(UseShpObj)
    If Left(shpObj.Name, 6) = "Sheet." Then shpObj.Delete
    Next
End Sub


Jumpy

#2
Hi,

you can also loop through the shapes on a page via:

Dim Shp As IVShape (or VisioShape)
Dim MyName As String

For Each Shp in Application.ActivePage.Shapes

'Do sth. with the shp, e.g.
MyName = shp.Name

Next


To flag your Picture-Shapes:
Because they are Shapes, too, you can add User-defined Cells to it, when you import the Shapes.
So you can e.g. add a cell: User.Type and set it's value to "MyPictures" or sth. like that.

If you have to delete the Shape(s), loop through all the shapes on the page.
Look if they have the User.Type cell.
Look if the value in the cell is the one you want deletet.
Delete the shape.