如何旋转图像并将其移动到特定位置?

时间:2019-07-17 18:33:15

标签: excel vba

我正在尝试上传图片,然后将其旋转并将其移动到特定位置,但是我似乎无法使其旋转。我有这段代码是因为我想将图片与文档一起保存。

Sub tyh()
    ActiveSheet.Shapes.AddPicture _
    Filename:="C:\Users\dovi.dovi-PC\Desktop\ads bh\IMG-7042.jpg", _
    linktofile:=msoFalse, savewithdocument:=msoCTrue, _
    Left:=1200, Top:=0, Width:=350, Height:=604
End Sub

1 个答案:

答案 0 :(得分:0)

宏记录器确实可以在这里为您提供帮助-上传图片并查看它为旋转生成的代码。我的是这样的:

ActiveSheet.Shapes.Range(Array("Picture 1")).Select
Selection.ShapeRange.IncrementRotation 90

当然应该避免使用SelectionActiveSheet~.Select,但这取决于您的代码。这是无需选择即可旋转图片的好方法:

Sub TestMe()

    KillAllShapes Worksheets(1)
    ThisWorkbook.Worksheets(1).Pictures.Insert ("C:\SomePic.jpg")
    Dim myShape As Shape
    Set myShape = Worksheets(1).Shapes(1)

    With myShape
        .Top = Range("B5").Top
        .Left = Range("B5").Left
        .IncrementRotation 180 'or 90
    End With

End Sub

Sub KillAllShapes(wks As Worksheet)

    Dim sh As Shape
    For Each sh In wks.Shapes
        sh.Delete
    Next

End Sub

来自the ShapeRange.IncrementRotation documentation的这个看起来不错:

Set myDocument = Worksheets(1) 
With myDocument.Shapes(1).Duplicate 
    .Fill.PresetTextured msoTextureGranite 
    .IncrementLeft 70 
    .IncrementTop -50 
    .IncrementRotation 30 
End With
相关问题