首页 > 解决方案 > 如何旋转图像并将其移动到特定位置?

问题描述

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

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

标签: excelvba

解决方案


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

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

当然,应该避免使用 theSelection和 the ActiveSheet~.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

ShapeRange.IncrementRotation 文档中的这个看起来不错:

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

推荐阅读