Get Dimensions of Cell Comment Background Image (.Shape.Fill.UserPicture) - excel

How do I get the dimensions of a cell comment background image?
I have created a comment composed solely of a background image using,
ActiveCell.Comment.Shape.Fill.UserPicture l_strFullPathOfImagePNG
I would like to be able to,
Select the cell.
Run a macro that resizes the comment so the background image is not fuzzy, but rather sized exactly pixel for pixel.
To do this I need to know the dimensions of the background image.
If I know the full path of the image file that created the background image, I can resize the cell comment to achieve what I want:
Sub CommentResizeToFitBackgroundImage()
Dim l_strFullPathOfImagePNG As String
Dim l_lImageWidth_pixels As Long, _
l_lImageHeight_pixels As Long
l_strFullPathOfImagePNG = "C:\……\BackgroundImage.png"
'Get the width and height in pixels of the image to be inserted
GetPNGDimensions _
l_strFullPathOfImagePNG, _
l_lImageWidth_pixels, _
l_lImageHeight_pixels
With ActiveCell.Comment.Shape
'0.75 scale factor was arrived at empirically
'and probably true only for my machine
.Width = l_lImageWidth_pixels * 0.75
.Height = l_lImageHeight_pixels * 0.75
End With
End Sub
However, I would like to do this to existing comments that no longer have a valid image file to read, so I need to get the dimensions of the background image.
.Comment.Shape.ScaleHeight 1, msoTrue looks promising, but fails with the error "The RelativeToOriginalSize argument applies only to a picture or an OLE object".
.Comment.Shape.Fill.Type = MsoFillType.msoFillPicture; so I do indeed have a picture, and not a solid background, gradient, pattern, or texture; so I don't know what the compiler is complaining about.
Lastly, evidently, .Comment.Shape.Fill.PictureEffects doesn't work for comments.

Related

Visio Shape Text positioning below the shape using Text Transform

I have a building a set of stencil shapes and I need the text to display below the shape. I am using custom formulas to generate the text, and as such the volume of text changes from use case to use case.
What I have come across is using the Text Transform set of properties, and I have tried the following with success for a single line of text:
TxtWidth = TEXTWIDTH(TheText)
TxtPinX = Width * 0.5
TxtLocPinX = TxtWidth * 0.5
TxtHeight = Height * 0
TxtPinY = Height * -0.2
TxtLocPinY = TxtHieght * 0.5
TxtAngle = 0 deg
The problem arises when there is more than a single line of text to display -> the text appears 'half above (inside) and half below' the bottom the shape.
I would like to place all the text, regardless of how many lines there are, underneath the shape.
What I have tried is to set the TxtPinY = some formula different from above eg/ Height * -(TxtHeight). This seems to always result in an 'error in formula'.
I am sure that this is something simple that I am missing, but I cannot figure it out.
Can anybody point me in the right direction?
Cheers and thanks for taking a look at this,
The Frog
You could try the TEXTHEIGHT function to get around this. Specify a reasonable maximum text widht as a second parameter for it:
TxtHeight = TEXTHEIGHT(TheText,100)
TxtPinY = 0
TxtLocPinY = TxtHeight
You can use the code provided with the stencil available in this post:
http://visguy.com/vgforum/index.php?topic=7461.msg31490#msg31490

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.

How to encircle Invalid data in Excel?

I want to show a circle around only invalid data.
i have done the complete steps shown in this link
But this circle shown is very big and covers the entire cell.
I want a small circle only covering the data not the entire cell's width.
Data validation is a built in Excel functionality. It checks whole cell value.
So it is not possible, using Data validation, to accomplish what your trying.
It MAY BE POSSIBLE using VBA, shapes, events and (hard) parsing character rendering. In your place, I would be glad with this very big circles!!! :)
I agree with #LS_dev. See this MS Article about changing data validation for printing. Try modifying it to loop through all your data validation and change the width and height.
You can probably do it with this part of the code by changing the width and height:
If Not c.Validation.Value Then
Set o = ActiveSheet.Shapes.AddShape(msoShapeOval, _
c.Left - 2, c.Top - 2, c.Width + 4, c.Height + 4)
o.Fill.Visible = msoFalse
o.Line.ForeColor.SchemeColor = 10
o.Line.Weight = 1.25

How to fix an Excel listbox that can't scroll the last element into view

A killer problem I've had in excel UIs since as long as I can remember, is with listbox scrolling.
When you have more elements in a listbox that can be displayed, a scoll bar will appear. In certain conditions, however, scrolling the bar all the way to the bottom of the list and releasing it, will "jump" the bar a notch upwards, and you won't be able to see the last item in the list. This is illustrated here:
There are many forum posts presenting this issue, and the solution has always been "Set the integral height property to false, and then set it to true again." What this does is slightly resize the listbox so that it the height is rounded to the height of a single row, and then no items are left hidden.
With lstbox
.IntegralHeight = False
.Height = myHeight
.IntegralHeight = True
End With
There are certain cases, however, where this does not work. If you are:
Programatically setting the height of your listbox
NOT using simple listbox selection (fmMultiSelectSingle)
Then simply setting integral height to false and then true after or between changes to height will make an adjustment to the height of your listbox, but when you go to scroll down, the problem will remain - the last item cannot be seen.
The key to this frustrating question is that while everyone else on the internet is confirming that the 'integralHeight' solution works for them, these very special cases are frustrated wondering why it doesn't work for them. So how do they get their fix?
Something I had to discover for myself, and which cannot be found anywhere else (which is why I'm posting it here), is that this problem had the added dimension of being dependent on the selection method. While I cannot fathom how the way the scroll bar works is related to not only the height and integral height property, but also the .MultiSelect property, I have found that it is.
When the .IntegralHeight method shown above does not work, the following method somehow does:
With lstbox
.IntegralHeight = False
.Height = myHeight
.IntegralHeight = True
.MultiSelect = fmMultiSelectSingle
.MultiSelect = fmMultiSelectExtended
End With
By simply changing the .MultiSelect property to fmMultiSelectSingle, and then restoring it back to the selection style desired, the height of the listbox will be automatically adjusted by the .IntegralHeight property to a slightly different height than when these actions aren't performed - the difference results in the scroll bar working correctly:
I hope the discovery of this special case and more precise workaround saves someone the hours of frustration and experimentation I had to go through.
i know this is very old post.
but i've been through a lot to fix this problem, so i just wanna share my tip. :)
first of all,
integralheight method doesn't work when worksheet zoom level is not 100%.
it will change listbox height and width, location, etc. (even if you set object property 'doesn't move or reseize with cell')
and when you try to take it its original size and location with code to fix this, this time its last item can't be seen
my tip is simple.
there's combination between font size and listbox height.
if your font size is 6-10(arial, regular), listbox height goes well with multiples of 12.75 (btw my list box style is 1 : ListStyle, 1-fmListStyleOption. it could be different with style 0)
as long as height is same with these multiples of 12.75, there will be no problem.
in case of font size 12(arial, regular), it's multiples of 13.55
so if there's no restiction about listbox size in your project, just resizing it slightly depending on your font size gives more comfort. :)
I had to anchor the position since my ListBox was walking across the page:
With ListBox1
.IntegralHeight = False
.IntegralHeight = True
.Height = 45
.Width = 69
.Top = 0
.Left = 1255.5
End With
With lstbox
`.Height = myHeight`
`.MultiSelect = fmMultiSelectExtended`
`.MultiSelect = fmMultiSelectSingle`
End With
This worked for me. No need of setting Integral height property
In my case the solution was this method:
with listbox
.IntegralHeight = False
.Height = myHeight
.Width = myWidth
.IntegralHeight = True
.Height = myHeight
.Width = myWidth
end with
Enjoy.
found ridiculously simple way to resolve this issue. adjust your height up or down a little bit so bottom line of list box is between check boxes, then you can scroll down to last item even if IntegralHeight is set to false
Thanks Alain. Your fix worked well for me.
I found a subsequent problem related to the height of the ListBox when resized, that it varied in an unpredictable way depending on the initial height. The resized height was different again when displayed on another machine with 125% text scaling. For example, if I set a height between 358 and 370, the resized height is either 370.65 or 371.4 on my machine but on the machine with 125% text scaling, it is 360.1, 370.25 or 380.45. With such large variability, the result was that the ListBox could obscure other controls below it.
The fix was to start with the maximum height I wanted and reduce the initial height until the resized height was less than the maximum height I wanted. I do this whenever I display that ListBox.
Hmax = 372 'Target Height
H1 = Hmax
With SteelForm.Controls.Item("ListBox1")
Do
H1 = H1 - 1
.IntegralHeight = False
.Height = H1
.IntegralHeight = True
.MultiSelect = fmMultiSelectSingle
.MultiSelect = fmMultiSelectExtended
DoEvents
Loop Until .Height < Hmax
End With
What I've seen in the past on forums is just adding an extra blank row to your list box. That should do it.
Just set the Integral Height property to True

In Excel, VBA - How can we lock resizing of a rectangle

I have an excel sheet that contains two rectangles and text in other cells.
I need to allow users to only edit the text in the rectangle. They should not be able to change the size of the object.
Applying lock on the rectangle locks the object as well as the text.
Does anyone know how I can achieve this?
Why not create two objects, one being a rectangle that is locked, and one being a text box that is not locked? This is really simplistic, but a possible answer.
Another idea would be to have the rectangle equal a set cell, and let them enter their text in the cell and it would transfer over even when the rectangle is locked.
As far as I am aware Excel does not accommodate Events for shapes and so there is no simple way of detecting a change in a shape size and then resizing the shape.
It is possible to emulate what you are asking for by using a workaround.
Imagine you have two rectangles on your spreadsheet called 'Rectangle 1 and 'Rectangle 2'. When a user finishes updating the text in any given box they must then click the spreadsheet to move out of 'edit' mode for the shape. You can detect this using the Workbook_SheetSelectionChange event.
The following module allows you to set the size of the rectangles as constants and will resize the rectangles accordingly:
Const Rect1Height As Integer = 50
Const Rect1Width As Integer = 200
Const Rect2Height As Integer = 50
Const Rect2Width As Integer = 200
Sub SetRectangleSize()
Dim Rect1 As Shape
Dim Rect2 As Shape
Set Rect1 = ActiveSheet.Shapes("Rectangle 1")
Set Rect2 = ActiveSheet.Shapes("Rectangle 2")
Rect1.Height = Rect1Height
Rect1.Width = Rect1Width
Rect2.Height = Rect1Height
Rect2.Width = Rect1Width
End Sub
Now all you need to do is to call this sub from a workbook level event:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
SetRectangleSize
End Sub
Each time a user updates the text in one of the rectangles they will click back on the spreadsheet and the event is fired, resulting in the rectangles being sized according to the constant height and width parameters that you have defined.

Resources