How to move a rotated shape to a specific location in excel 2010 using vba - vba

I've written some VBA code that automatically creates a chart. One of the axes on this chart doesn't use normal labels but a graphic. I've stored the graphic as an image and I use the .Copy and .Paste methods to get a copy of this image onto the chart.
Here is where it gets confusing. I need to rotate the image to get it aligned with the axis (using the .rotation property). But when I set the .top and .left properties the shape doesn't end up where I would expect. In fact setting the properties to 0 and 0 doesn't do what I would expect either. I've tried changing the order of the way I set the properties on the image object but it only appears in a different (wrong) location.
I'm sure I'm missing some vital aspect of how VBA/Excel is placing the object relative to what I'm setting the top and left properties to. Basically my goal is to make the image on the left side of the chart with the same width as the plot area's height (since the image is rotated I theorize this will make it the same size).
This code does not work:
Sheets(ImageSheet).Shapes("agreement").Copy
c.Chart.Paste
c.Chart.Shapes(1).Rotation=270
c.Chart.Shapes(1).width = c.Chart.PlotArea.height
c.Chart.shapes(1).left = 5
c.Chart.Shapes(1).top = c.Chart.PlotArea.top
I've also tried this code
c.chart.Shapes(1).top = c.chart.PlotArea.top + c.Chart.PlotArea.height
because I thought maybe it was calculating the "top" as the upper-left corner of the image object when it is not rotated (rotating 270 degrees makes this point in a place where it should align with the bottom of the plot area). But that doesn't do what I expected either.
The image is a skinny rectangle that acts as a label for the axis. The chart will end up being laid out like this: http://imgur.com/NrSXR and the axis label image would be something like this http://imgur.com/08EWU
What am I missing here?

Is it possible for you to align your chart into a position where the shape could rest align/on a cell?
IF YES then here is a suggestion:-
You could position shape into a cell. Then adjust the size to what you need. And rotate.
Then change its bring forward property be shown on the Chart.
Next Group Chart and the Shape
PS: I recorded a macro. However it's best if you could show us what your the exact picture (=how your sheeet/chart/image should look like) of your question.

I ended up rotating and resizing the image before copying and pasting to the chart and then positioning it. I had to use the IncrementLeft and IncrementTop methods rather than setting the left and top properties directly because that did not have the desired effect.
When doing the paste into the chart the object always ended up in the upper left hand-corner so I could increment to the left by the small amount I wanted as a margin I wanted there and increment the top by the value of PlotArea.top to align it with the plot area.
I was also surprised that when creating the copy of my image it retained the "name" i referred to it as when I copied it to the new sheet and chart. This was especially useful for positioning the image once it was on the chart.
I also needed execute this code at the very end of my procedure, after everything else had been positioned and aligned, or when I positioned the data labels for one of my series they wouldn't appear correctly.
Here is the code that I ended up using:
'make a copy of the label image and refer to it with a variable for convenience
Sheets(ImageSheet).Shapes("maturity").Copy
i = Sheets(ImageSheet).Shapes.Count
Sheets(ImageSheet).Paste
Dim axisImage As Shape
Set axisImage = Sheets(ImageSheet).Shapes(i + 1)
'rotate and resize the image
With axisImage
.Rotation = 270
.width = c.Chart.PlotArea.height
End With
'cut and paste the new image to the chart
axisImage.Cut
c.Chart.Paste
'position it in the chart using IncrementTop and IncrementLeft because setting the properties directly does not have the desired effect
c.Chart.Shapes("maturity").IncrementTop c.Chart.PlotArea.top
c.Chart.Shapes("maturity").IncrementLeft c.Chart.PlotArea.left - c.Chart.Shapes("maturity").height

Related

Inserting images automatically on slides referencing model #? MICROSOFT POWERPOINT

I have about 100 slides here on powerpoint that look like this:
Slide example screenshot
I need to insert an image into the text box on each slide with the sizing of
height = 5, width = 3.64
and positioning of:
Horizontal = 6" / Vertical = 1.2" (Both from top left corner)
For good measure, here are all of the image format settings:
Image format settings screenshot
The image files are the same as the model number, such as MHS1306.jpg.
Is there any way to use a macro to automatically reference the model number, insert the corresponding image, and then resize it to the correct size?
All of the images are in the same folder.
The image just needs to be in that position, the text box isn't important, just shows where the image should go.
I am very new to VBA and scripts, and basically know nothing about it yet, so I would appreciate any help.
Thanks.

Rectangular selection of shape in visio using vba

Is it possible to select shapes in Visio by specifying the coordinates of a selection rectangle? If so, how does one do this?
I need to select and delete any shape in a specific location on a Visio page.
I would like to be able to specify the coordinates of a lower left corner and an upper right corner on the page and have vba tell me the id's or handles or something that would allow me to delete these shapes as I need to place a new shape in that particular location. I am looking for something like
shapes = MyVisioPage.SelectByRectangularCrossingBox(lowerleftX,lowerleftY,upperrightX,upperrightY)
You could actually draw a rectangle with those coordinates and then use Shape.SpatialNeighbors to find out all shapes in that rectangle.. Something like this (VBA):
Function SelectByRectangularCrossingBox(page, _
lowerleftX, lowerleftY, upperrightX, upperrightY) As Selection
scopeId = page.Application.BeginUndoScope("try")
Set rc = page.DrawRectangle(lowerleftX, lowerleftY, upperrightX, upperrightY)
Set SelectByRectangularCrossingBox = rc.SpatialNeighbors(visSpatialContain, 0.01, 0)
page.Application.EndUndoScope scopId, False
End Function
The code is wrapped around with BeginUndoScope/EndUndoScope to cancel changes.

How to replace image of a picture shape in PowerPoint?

Is there any way to replace the image of an existing picture shape in PowerPoint from code?
Thanks
If it's a linked image, either overwrite the linked image file with a new file or point the link at a new file.
Or if the shape is picture filled, you can fill it with a different picture instead. That can distort the picture if its proportions don't match the shape's proportions; instead, you can change the aspect ratio of the shape to match that of the picture if need be.
To change the picture fill, assuming a reference to the shape in osh:
With osh.Fill
.UserPicture ("C:\path\to\picture.jpg")
End With
To work out the size of the picture so you can adjust the shape to it, you can either use external image processing software or insert the picture into PPT, giving -1 for height and width parameters and pick up the .Height and .Width of the newly inserted picture, then delete it.
Or insert a new picture, pick up the old picture's size/position/other proprties and apply them to the new picture, delete the old picture, move the new one to the old one's Z-order.

MS Charting Annotations refuse to align to mouse position

Can anyone tell me why the below is happening please? The red line signifies where the annotation should be positioned (along X axis), but it's always rendered right on the left edge... I did a few searches in Google and SO, and found an answer which implies that PixelPositionToValue(Mouse.X) would sort it, but even using this it ends up exactly the same.
Private Sub AssignNewDownTime()
Dim sStr As String = InputBox("Please enter downtime reason")
Dim annot As New Charting.RectangleAnnotation()
annot.ClipToChartArea = "Chart1"
annot.BackColor = Color.DarkRed
annot.ForeColor = Color.White
annot.AllowMoving = True
annot.AllowAnchorMoving = False
annot.AllowSelecting = False
annot.IsMultiline = False
annot.AllowTextEditing = False
annot.IsSizeAlwaysRelative = False
annot.X = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(StartMousePoint.X)
annot.Y = 10
annot.Width = 25
annot.Text = sStr & " /X: " & annot.X & "Y:" & annot.Y
Chart1.Annotations.Add(annot)
Chart1.Invalidate()
End Sub
Gah! This always happens... I pull my hair out for hours, post on SO and within 5 minutes I've fixed it. Anyway, for future generations that may also pull their hair out, here's the solution:
Annotations X and Y aren't set to that of the chart, so whereas the chart will range from 0.0 to 1.0, annotations default range is 0 to 100. Nightmare! There are a couple of ways around this, I chose:
annot.AxisX = Chart1.ChartAreas(0).AxisX
Which sets the X axis of the annotation to mimic that of your chart. Thus the values and limits will be correct. As soon as I did this it worked instantly. You can of course set the AxisY of the annotation as well, but beware that in charting the Y is bottom to top. More information can be found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.annotation(v=vs.110).aspx - specifically (which I wish I'd read first...):
Annotations are commonly used to comment or elaborate on chart elements such as data points. Annotations can also be used to draw custom shapes.
By default, annotations are positioned using relative coordinates, with (0,0) representing the top-left corner, and (100,100) representing the bottom-right corner of the chart image. It is also possible to switch from this relative coordinate system to a system that uses axis values. With an axis coordinate system, X and Y, which represent the position of the top-left corner of an annotation, are set using X axis and Y axis values, instead of values that range from 0-100.
There are two ways to use axis values when you specify the position and size of an annotation:
Set the AxisX, AxisY or both of these annotation properties to the AxisX and AxisY property values of a ChartArea object.
Use the AnchorDataPoint property to anchor the annotation to a data point. In this case, its positioning is automatically calculated.
All annotations are derived from the Annotation class, which can be used to set attributes common to all Annotation objects, such as color, position, anchoring and so forth.
Anyway, hope this helps folk out.

How to get a DataVisualization.Charting Chart to show before points are added?

How may I get a DataVisualization.Charting Chart to show its axes and gridlines before points are added? Currently the chart area is blank even if axes scale limits are set manually, and I can see no good reason for this.
Set the Axis.Enabled properties to 'AxisEnabled.True' instead of 'AxisEnabled.Auto'
chartArea.AxisX.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
chartArea.AxisY.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;