Shape size in centimetre not consistent - excel

I create a shape in Excel specifying certain dimensions:
With ActiveSheet.Shapes.AddShape(msoShapeRectangle, 96, 30, 144, 60)
.Height = Application.CentimetersToPoints(2) '2cm height
.Width = Application.CentimetersToPoints(4) '4cm width
End With
I then print the sheet as a PDF document using Adobe PDF printer. (Excel sheet size and printer sheet size are both set to A4).
The problem is that the size of the shape on the PDF is not the same as what is in Excel. On the PDF the width and height are different.
What is causing this problem and how do I fix it?
Edit:
Shape in Excel
Shape in PDF, measured using Adobe Acrobat measuring tools
Many thanks!

Related

Changing diagram size to specific size in mm with VBA

I have an existing Excel file I need to work with. It contains a line graph where measurement results are plotted. All the referencing is done via names. The graph is called "ChartResult".
Obviously Excel discriminates the graph area (the "outer") and the plot area (the "inner") where the graph is plotted. Please correct me if I'm wrong, also learning the preferred english nomenclature would be of great help.
My goal is to print the (page containing the) table so that the division/auxiliary lines have a specific distance from each other. My thinking was that if I define the scale of the axis (the max and min values) and define a size of the graph I would acheive this goal.
However in Excel I can only type in the size of the whole graph area, which is the outer thing, so not helpful when I want to define the size of the graph, the inner thing.
I started using VBA to acheive this but haven't been succesful:
Sub Groesse_eingeben()
ActiveSheet.ChartObjects("ChartResult").Activate
ActiveChart.SeriesCollection(1).Select
ActiveChart.PlotArea.Select
Selection.Left = 0
Selection.Top = 0
Selection.Width = 200
Selection.Height = 200
End Sub
This code is changing the size an position of the graph but not to what I expected it to be. Is the input of Selection.XY in pixels or mm? I naively assumed mm but my graph becomes smaller than 200x200 mm, around 60x60 mm.
Thank you!
Chris
System:
Microsoft® Excel® 2016 MSO (Version 2204 Build 16.0.15128.20128)
Win10 Pro 21H2
Update:
Ok, the input size is points. But how to specify the exact size of the graph?
On the screenshot there are two dotted borders: One, the inner, is the actual size of the visible graph the other is the size of the graph object. To cause more confusion: Both are within the graph area, which I called "outer" area above :)
How can I input the exact numbers for the actual visible graph?
You can control the outer width of the chart (ChartObject) with the .Width property, and the inner width of the chart with the .Chart.PlotArea.Width property.
Here is a sub that takes a chart and widths as inputs, and updates the chart:
Private Sub SetChartWidths(Ch As ChartObject, OuterWidth As Long, InnerWidth As Long)
' Set the outer width of the chart
Ch.Width = OuterWidth
' Set the inner width (plot area width)
Ch.Chart.PlotArea.Width = InnerWidth
End Sub
EDIT START
And here is how you can use the sub:
Private Sub UseSubroutine()
' Store the chart object in a variable
Dim LineChart As ChartObject
Set LineChart = ThisWorkbook.Sheets("Sheet1").ChartObjects(1)
' Run the sub
SetChartWidths Ch:=LineChart, OuterWidth:=200, InnerWidth:=150
End Sub
If you're having trouble using the SetChartWidths sub, and it is in a different Module than the code you're calling it from, you can remove the Private from the front to change the Scope of the sub.
EDIT END
As for what widths to use, that will be up to you.

Change chart height without resizing plotArea

I am trying to resize an excel chart with VBA.
Is it possible to change ActiveChart.ChartArea.Height without affecting the size of the PlotArea? Whenever I attempt to change the chart height it looks like the plot area is automatically resized, which is an undesired outcome.
I have tried the following sequence, when downsizing a graph:
Changing plotarea to fixed desired height;
Changing chart height to fixed desired height;
Changing plotarea to fixed desired height;
This sequence does not yield expected results, as (1) the chart is not changed to specified height, and (2) the plotarea height is output correctly, but its positioning within the chart (.InsideTop) has changed.
Please, test the next way of dealing with a chart dimensions. The scenario involves the next process: firstly memorizing the PlotArea dimensions (Height/Width), then play with the chart (Object) dimensions, reset the PlotArea ones and set its Position to Automatic. Excel tries guessing what you want accomplishing and it looks/is more probable that both chart elements to be modified proportionally:
Sub testActiveChartDimensions()
Dim ch As Chart, plHeight As Double, plWidth As Double
Set ch = ActiveChart 'plays with a selectded chart
plHeight = ch.PlotArea.height: plWidth = ch.PlotArea.width 'memorize the plot area dimensions
ch.Parent.height = ch.Parent.height * 2: ch.Parent.width = ch.Parent.width * 2 'resize the chartObject
ch.PlotArea.height = plHeight: ch.PlotArea.width = plWidth 'reset the initial dimensions for plot area
' you can set any other dimensions (just to be lower than the new chart dimensions...)
ch.PlotArea.Position = xlChartElementPositionAutomatic 'center it on the chart object
End Sub

Excel VBA / Importing WMF image with default size result huge image

adding this same question question to this forum as well.
Next behaviour started a few months ago in some computers after updates to Office / Excel.
Excel 2016 and 365 versions affected at least.
If you insert WMF image to excel using following macro, (-1 means use the images default widht and height) the image goes very, very big.
If you open the image in any other image application the size can be like 11cm * 10cm but in Excel it is like 800 cm * 558 cm.
(Other image formats are still working as excpected)
Sub InsertPicture(picture As String)
Dim x As Integer
x = ActiveWindow.Zoom
ActiveWindow.Zoom = 100
Set p = ActiveSheet.Shapes.AddPicture(picture, False, True, Selection.Left, Selection.Top, -1, -1)
ActiveWindow.Zoom = x
Set p = Nothing
End Sub
Example: Installed Office 2016 from ISO to empty virtual machine - WMF images was working. Installed all office updates, after that WMF was not working.
Seems to be so that Excel image import does calculate size correctly from Metafile header where dpiX and dpiY are in one example 8640 and Height is 25040 and width is 32760 --> size in Excel is 661cm * 866 cm

Pasting images (excel Table) in PowerPoint

I'm trying to paste excel table as picture into PPT though VBA
but the images are not in the desired shape as codded
The Require coordinates are:
Height 14.80
Width 23.28
Top 2.13
Left 5.3
and the output coordinates are like:
H-18.73 CM
W-23.28 CM
Horizontal-5.3 CM
Vertical-2.13
below is my code:
Range(cel.Value).Copy
newPowerPoint.ActiveWindow.View.GotoSlide cel.Offset(0, -1).Value
Set activeSlide = newPowerPoint.ActivePresentation.Slides(cel.Offset(0, -1).Value)
activeSlide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile).Select
newPowerPoint.ActiveWindow.Selection.ShapeRange.Height = Application.CentimetersToPoints(14.80)
newPowerPoint.ActiveWindow.Selection.ShapeRange.Width = Application.CentimetersToPoints(23.28)
newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = Application.CentimetersToPoints(2.13)
newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = Application.CentimetersToPoints(5.3)
Good workaround is to place a shape where you want it and use Debug.print to read the position in points.

Set the font of PPT text from VBA Excel

I am trying to copy the excel content to PPT. using this command
`pptSlide.Shapes.PasteSpecial DataType:=ppPasteHTML, Link:=msoFalse '2 = ppPasteEnhancedMetAEile
Set myShape = pptSlide.Shapes(pptSlide.Shapes.Count)
If myShape.Height <> ExcRng.Height Then
myShape.Table.ScaleProportionally ExcRng.Height / 285
End If`
While I am doing it sets the font of the content gets bigger or smaller depending on the amount of text in the shape.
Can some one tell me if I can fix the size of the font to "8" irrespective of the amount of content.
Since its not a textarea but just a shape.
Shapes containing text in Powerpoint have the property Autosize:
ActiveWindow.Selection.ShapeRange().TextFrame.Autosize = ppAutoSizeNone
With this you can make the text stick to the size you determine in
ActiveWindow.Selection.ShapeRange().TextFrame.TextRange.Font.Size

Resources