Add new shape at certain position in visio page

Started by ExcelMVP, January 02, 2013, 12:30:52 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Michelle

Oops. got interrupted and lost my train of thought.  What I meant to add to my previous post was that when I run this macro:
Sub PlaceClose(shp As Visio.Shape)

Dim newshp As Shape

  Const xoffset = 100
  Const yoffset = 0
  'Do something that results in the newshape droped somewhere on the page
  'Now you can place the new shape relative to the old shape:

  newshp.Cells("PinX").Result("") = shp.Cells("PinX").Result("") + xoffset
  newshp.Cells("PinY").Result("") = shp.Cells("PinY").Result("") + yoffset
End Sub


I get an error: "Object Variable or With Block Variable not set"  And if I hit debug, the arrow points to this line:  newshp.Cells("PinX").Result("") = shp.Cells("PinX").Result("") + xoffset
and it's highlighted.
Unfortunatly, I have no idea what that means!

Michelle

Paul Herber

#16
Change line:
Application.ActiveWindow.Page.PasteToLocation shp.Cells("PinX").Result("#") + 10#, shp.Cells("PinY").Result("#") + 0, 0
to
newshp = Application.ActiveWindow.Page.PasteToLocation shp.Cells("PinX").Result("#") + 10#, shp.Cells("PinY").Result("#") + 0, 0


[My code here is incorrect - ignore]
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Michelle

Thanks for taking a look, Paul.  I did as you suggested.  Now the second shape does not place, and I get an error:  "Compile Error - Invalid use of property." 

The debugger shows this line: 
shp.Cells("PinX").Result ("#") + 10#, shp.Cells("PinY").Result("#") + 0, 0

with  .Result  highlighted in blue. 
Can you help? 

Michelle

Jumpy

#18
I still only have Visio 2007 so I didn't know PasteToLocation:

http://msdn.microsoft.com/en-us/library/ff768423%28v=office.14%29.aspx

I looked it up and saw that this procedure has no return value, so it doesn't give back a reference to the new shape. That is sad and not good thinking on part of the Visio developers. Because PasteToLocation can only be used with shapes it shouldn't have made a problem to create the function in a way that returns the pasted shape.

Nevertheless, it is as it is (as the people in my area used to say), and Pauls code won't work:
newshp = Application.ActiveWindow.Page.PasteToLocation ...

So if you create a new shape with copy and paste your code can't know the new shape.
So you should not use Copy and Paste. Instead lookup the name of the mastershape of the old shape and drop a new instance of that mastershape from the stencil to the page with good old page.Drop method:

http://msdn.microsoft.com/en-us/library/office/ms195970%28v=office.12%29.aspx

hth Jumpy

P.S.:
Perhaps you can make a screenshot or explain a little more, what you really wan't to do. What is your scenario, what do you do and what should happened automatically when you did what you did. Which shapes from which stencils (propably your own) are you working with or should work with your macro.

Paul Herber

Quote from: Jumpy on January 18, 2013, 07:40:28 AM

... and Pauls code won't work ...


Dang. You are right.
Got an idea though, after the shape has been dropped it will be the only shape in a selection, so
undo the change I suggested and insert afterwards:

newshp =  Application.ActiveWindow.Selection(1)

I think ...
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Michelle

Who debugged the VBA code and got the shape working as desired?  *This Chick*
LOL

I felt it was close, so I just started trying things...  I have it working just as I want... SO Happy!

In case you are curious:
Sub MyMacro4(shp As Visio.Shape)

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

    Application.ActiveWindow.Page = Application.ActiveDocument.Pages.ItemU("Page-3")

    ActiveWindow.DeselectAll
    ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(14), visSelect
    Application.ActiveWindow.Selection.Copy

    Application.ActiveWindow.Page = Application.ActiveDocument.Pages.ItemU("Site Layout")
 
    Application.ActiveWindow.Page.PasteToLocation shp.Cells("PinX") + 150, shp.Cells("PinY") + 10, 0

   
End Sub


Thanks for your patience with me!  I am in a funny place right now, I don't really feel like I know a thing about code, yet somehow I can sometimes make things happen.   ;)
Michelle

contact_ankit86

Hi,

I am facing a very strange problem. I am trying to copy paste a shape programmtically and then setting the PINX and PINY of that new shape using below code :

newshp.Cells("PINX") = oldshp.Cells("PINX")
newshp.Cells("PINY") = oldshp.Cells("PINY")

The problem is it places the new shape at correct location on one system but same code on a different system places at a wrong position. i am not sure what exactly the problem is. I have tried it on two desktops (shows same behaviour) and 2 laptops(shows different behaviour). Is there any setting on Desktop / laptops that is making Visio to behave different..?

Any thoughts..???



aledlund

I'd start with the Page Setup => Layout and Routing => and check to see if move shapes is checked.
al

Jumpy

And maybe the Default Property is in Inches and you have metric units in one of the sytems?

newshp.Cells("PINX").Result("mm") = oldshp.Cells("PINX").Result("mm")

or another unit instead of mm

Archangelos

I have a question. Let's say that a macro command added a shape in the page. How can we get the name of that shape?

For example, take a look at the picture. It has three rectangles that have been inserted manually.

1. Is it possible to change the name, to rename it from Rectangle.1 to something else?
2. If I add a shape via code can I define the name? If not, how can I get it in a variable?

metuemre

I don't know how you add a shape to a page via macro but let me give a simple example. Below code adds a rectangle to the activepage and rename it to "Sample"

Public Sub SampleRename()

Dim vsoShape As Visio.Shape
Set vsoShape = ActivePage.DrawRectangle(1, 1, 2, 2)
vsoShape.name = "Sample"

End Sub

vojo

set pinx and piny cells to designated location of shape or group origin.
(don't use locpinx or locpiny...that is shape location in relation to the designated origin)

Archangelos

Quote from: metuemre on October 19, 2016, 02:12:05 PM
I don't know how you add a shape to a page via macro but let me give a simple example. Below code adds a rectangle to the activepage and rename it to "Sample"

Public Sub SampleRename()

Dim vsoShape As Visio.Shape
Set vsoShape = ActivePage.DrawRectangle(1, 1, 2, 2)
vsoShape.name = "Sample"

End Sub

Interesting, thanks buddy.