改变形状大小的数据类型
Data type for changing size of shapes
我的 VBA 代码运行良好。我可以更改形状的多种尺寸,但只适用于四舍五入的数字。如果我想将形状的大小更改为 2.5 或 5.5 是行不通的,但变量的数据类型是双精度的。有什么想法吗?
谢谢
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
Sub test()
Dim objHeigh As Double
Dim objWidth As Double
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
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
Exit Sub
CheckErrors: MsgBox Err.Description
End Sub
因为你在
中使用了CInt
objHeigh = CInt(InputBox$("Assign a new size of Height", "Heigh"))
将结果转换为 integer
。
所以,答案应该如下:
objHeigh = CDbl(InputBox$("Assign a new size of Height", "Heigh"))
我的 VBA 代码运行良好。我可以更改形状的多种尺寸,但只适用于四舍五入的数字。如果我想将形状的大小更改为 2.5 或 5.5 是行不通的,但变量的数据类型是双精度的。有什么想法吗?
谢谢
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
Sub test()
Dim objHeigh As Double
Dim objWidth As Double
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
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
Exit Sub
CheckErrors: MsgBox Err.Description
End Sub
因为你在
中使用了CInt
objHeigh = CInt(InputBox$("Assign a new size of Height", "Heigh"))
将结果转换为 integer
。
所以,答案应该如下:
objHeigh = CDbl(InputBox$("Assign a new size of Height", "Heigh"))