2016-07-05 138 views
0

我有用於更改形狀大小的VBA代碼,但是我想將數字轉換爲cm。有關如何轉換這些數字的任何建議?
另一個問題是,我想改變多個選定形狀的相同大小;你能幫我解決這個問題嗎?將形狀大小轉換爲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 

回答

3

根據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 
+0

好的謝謝。但我應該在哪裏分配我的代碼中的功能? – Norby

+0

將.e.g放在與test()子模塊相同的模塊中,然後可以像ConvertCmToPoint(10.5)那樣調用它。所以基本上你可以在你的初始代碼的「end sub」行下面複製和粘貼功能 – DAXaholic

+0

是的,我知道這一點。對不起,因爲Im新的VBA Im混淆了在哪裏調用該函數,我應該分配給變量還是?謝謝你的努力! – Norby