Delete Visio Shape From Excel VBA

Started by Lars73, January 04, 2011, 07:54:15 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Lars73

Hi,

I do not know if this is the right place to post this question - might be that the section on code is actually better.

I am looking for VBA code to delete a Visio shape from Excel VBA. Can anybody help?

Thanks.

aledlund

I'm a little confused when you refer to VBA and Excel VBA. Perhaps a little more information on what you are attempting to accomplish might help us in better understanding what you are looking for.
al

Lars73

Adedlund,

I am in the process of making a tool in Excel that -besides making all kinds of calculations - will produce a specific Visio drawing based on the calculation results in Excel.

My idea was to make a generic Visio drawing, and delete parts that are not applicable (so: opening generic drawing and deleting certain parts all triggered from Excel VBA).

I have managed the part to open the generic Visio drawing, to save it under a new name and to delete complete Visio pages that are not applicable at all. Now all I further need is to be able to delete certain shapes that are not required from the generic drawing.

Hope this clarified my intentions a bit. if not, please let me know.

aledlund

thanks for the update. Under the covers Visio handles objects like pages and shapes the same way. The code is simply visShape.delete (of course after assigning visShape to the one you want to get rid of).

http://msdn.microsoft.com/en-us/library/ff765184.aspx

al

Lars73

Al,

Assigning visShape to the shape I want to delete is exactly the part where I get stuck: what is the correct syntax for that?

Should I use the shape ID?

Further note that since I want to call from Excel, I need the syntax to delete a shape on a specific Visio page (so not just on ActivePage)

Lars

aledlund

#5
first you set the page

http://msdn.microsoft.com/en-us/library/ff765386.aspx

shape.id may not be unique in a document (only guaranteed on a page).

dim vsoDocument as visio.document
set vsoDocument = application.activedocument
dim vsoPage as visio.page
set vsoPage = application.activepage
' or
' set vsoPage = vsodocument.pages("mypage")
dim vsoShape as visio.shape
' go look for shape named myshape
set vsoShape = vsoPage.shapes("myshape")
vsoShape.Delete


Lars73

Al,

This worked fine (with a little effort to make it specific). Ultimately used the code below. Thanks for the help.



Dim VisioApp As Object
Dim vsoPage As Visio.Page
Dim vsoShape As Visio.Shape

Set VisioApp = GetObject(, "Visio.Application")

VisioApp.Documents.Open "path + filename Visio document"

Set vsoPage = VisioApp.ActiveDocument.Pages("Visio Pagename")
Set vsoShape = vsoPage.Shapes("Shape Name")
vsoShape.Delete