将形状大小转换为cm

时间:2016-07-05 10:35:15

标签: vba powerpoint powerpoint-vba

我有用于更改形状大小的VBA代码,但我想将数字转换为cm。有关如何转换这些数字的任何建议吗? 另一个问题是我想为多个选定的形状改变相同的尺寸;你能帮我这个吗?

非常感谢!

Sub test()
    Dim objHeigh As Integer
    Dim objWidth As Integer
    Dim oSh As Shape

    On Error GoTo CheckErrors

    With ActiveWindow.Selection.ShapeRange
        If .Count = 0 Then
            MsgBox "You need to select a shape first"
            Exit Sub
        End If
    End With

    For Each oSh In ActiveWindow.Selection.ShapeRange
        objHeigh = oSh.Height
        objWidth = oSh.Width

        objHeigh = CInt(InputBox$("Assign a new size of Height", "Heigh", objHeigh))
        ' give the user a way out
        If objHeigh = 0 Then
            Exit Sub
        End If

        If objName <> "" Then
            oSh.Name = objName
        End If

        objWidth = CInt(InputBox$("Assign a new size of Width", "Width", objWidth))
        ' give the user a way out
        If objWidth = 0 Then
            Exit Sub
        End If

        oSh.Height = CInt(objHeigh)
        oSh.Width = CInt(objWidth)
    Next
    Exit Sub

    CheckErrors: MsgBox Err.Description
End Sub 

1 个答案:

答案 0 :(得分:4)

根据MSDN,相应形状属性的高度/宽度以点为单位指定:

  

以磅为单位返回或设置指定对象的高度。   读/写。

在那个页面上,他们专门展示了一个例子,并指出1英寸有72分

的事实
  

此示例将指定表中第二行的高度设置为100   积分(每英寸72点)。

因此我认为依靠这个事实是安全的,只需编写一个函数来自行转换它:

Function ConvertPointToCm(ByVal pnt As Double) As Double
    ConvertPointToCm = pnt * 0.03527778
End Function

Function ConvertCmToPoint(ByVal cm As Double) As Double
    ConvertCmToPoint = cm * 28.34646
End Function

就您对多个对象的大小调整的问题而言,我不确定我是否完全理解您的问题。我以某种方式解释它,以便将提示移出For循环中的用户应该给你想要的结果(如果这实际上是你想要的结果:):)

objHeigh = CInt(InputBox$("Assign a new size of Height", "Heigh"))
' give the user a way out
If objHeigh = 0 Then
    Exit Sub
End If
objHeigh = ConvertCmToPoint(objHeigh)

objWidth = CInt(InputBox$("Assign a new size of Width", "Width"))
' give the user a way out
If objWidth = 0 Then
    Exit Sub
End If
objWidth = ConvertCmToPoint(objWidth)

For Each oSh In ActiveWindow.Selection.ShapeRange
    If objName <> "" Then
        oSh.Name = objName
    End If

    oSh.Height = CInt(objHeigh)
    oSh.Width = CInt(objWidth)
Next
相关问题