OLEObject高度和宽度不一致 [英] OLEObject Height and Width are not consistent
问题描述
我目前将PDF文件和图像作为OLE对象附加到我的excel表,并试图控制它们的大小。 (我希望图标沿网格显示)
I am currently attaching PDF files and images to my excel sheet as OLE Objects, and trying to control the size of them. (I want the icons to appear along a grid)
问题是,即使每个OLEObject都应该符合以下规范,它们有时也是不同的大小。一些pdf有更大的长度或宽度,然后是图像文件。
The problem is that even though each OLEObject should meet the following specifications, they are sometimes different sizes. Some pdfs have greater lengths, or widths then image files.
如何确保它们是一致的?
How do I make sure they are consistent?
Public Sub OLEObjectNamesReturn()
Dim Count As Integer
Dim Space As Integer
Count = 23
Space = 0
For Each oleObj In ActiveSheet.OLEObjects
Select Case oleObj.Name
Case "CheckBox21"
Case "CheckBox22"
Case "CommandButton21"
Case "CommandButton22"
Case Else
Dim ObjectName As String
ObjectName = oleObj.Name
Set oCell = ActiveSheet.Range("P" & Count)
ActiveSheet.Shapes.Range(Array(ObjectName)).Select
ActiveSheet.Shapes(ObjectName).Height = 30
ActiveSheet.Shapes(ObjectName).Width = 30
ActiveSheet.Shapes(ObjectName).Top = oCell.Top + 7 + Space
ActiveSheet.Shapes(ObjectName).Left = oCell.Left + 7
Count = Count + 1
Space = Space + 15
End Select
Next
End Sub
推荐答案
默认情况下,形状的宽高比(关系W / H)已锁定 ...所以实际上你的。高度
和 .Width
设置将更改尺寸(除非它们从开始是平方)。如果你想要完美的正方形,无论你的形状的原始W / H比是多少,都可以解开宽高比。
By default shapes have their aspect ratio (relation W/H) locked ... so in fact both your .Height
and .Width
settings will change both dimensions (unless they are square from start). If you want perfect squares no matter what is the original W/H ratio of your shapes, unlock the aspect ratio.
建议:
Sub Test()
Dim OleObj As OLEObject
Set OleObj = ActiveSheet.OLEObjects(1) ' embedded PDF A4 ... not icon
OleObj.ShapeRange.LockAspectRatio = msoFalse
OleObj.Height = 30
OleObj.Width = 30
End Sub
测试原始A4尺寸的PDF ...一个不必喜欢最后的样子; - )
Tested wit a PDF originally A4 size ... one doesn't have to like the final look ;-)
如果要保持宽高比,但仍希望将OLEObject适合30x30网格,则需要将一个单一设置应用于较大的维度,例如
If you want to maintain the aspect ratio but still want to fit your OLEObject into a 30x30 grid, you need to apply one single setting to the larger dimension, e.g.
' ....
If OleObj.Width > OleObj.Height Then
OleObj.Width = 30
Else
OleObj.Height = 30
End If
' ....
然后 - 水平居中添加对象(30 - OLEObj.Width)/ 2
to oCell.Left
etc etc ...
Then - to horizontally center the object you'd add (30 - OLEObj.Width)/2
to oCell.Left
etc etc ...
这篇关于OLEObject高度和宽度不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!