【问题标题】:VBA excel .Left .Top when picture is rotated旋转图片时VBA excel .Left .Top
【发布时间】:2021-06-06 06:20:36
【问题描述】:

我尝试了无数次,但我仍然有这个问题。我尝试使用 excel 中的宏将图片放入给定范围内。只要图片不旋转(方向= 0)就没有问题。但从图片旋转的那一刻起,一切都流血了。我已经发现图片的高度和宽度也是旋转的。但是 .top 和 .left 会发生什么?这是我的代码:

Function fotoInsert(ByVal PictureFileName As String, ByVal rng As Range)

    Set pic = ActiveSheet.Pictures.Insert(PictureFileName)
    
    If pic.ShapeRange.Rotation = 90 Then
        With pic
              'keep original aspect ratio
              .ShapeRange.LockAspectRatio = msoTrue
              
              'Picture's aspect is less than rng aspect then adjust the picture's width to fit rng
             If (pic.Width \ pic.Height) <= (rng.Height \ rng.Width) Then
                 .Width = rng.Height - 1 'pictures' width is the larger height, by this line it fits exactly into range width
                 .Left = rng.Left + ((rng.Width - pic.Height) / 2)
                 .Top = rng.Top + 1
             Else 'Picture's aspect is greater than rng aspect then adjust the picture's height to fit rng
                 .Width = rng.Height - 1 'picture's heigth is larger than its width, this line makes it exactly fit int range height
                 .Top = rng.Top
                 .Left = rng.Left
             End If
        .Placement = xlMoveAndSize
        .PrintObject = True
        End With
    Else
        With pic
         'keep original aspect ratio
         .ShapeRange.LockAspectRatio = msoTrue
         
         'Picture's aspect is less than rng aspect then adjust the picture's width to fit rng
            If (.Height \ .Width) <= (rng.Height \ rng.Width) Then
                
                .Width = rng.Width - 1 'pictures' width is the larger height, by this line it fits exactly into range width
                .Left = rng.Left + 1 'position at left range border
                .Top = rng.Top + ((rng.Height - pic.Height) / 2) 'position in center of range height
            Else 'Picture's aspect is greater than rng aspect then adjust the picture's height to fit rng
                .Top = rng.Top + 1 'position at upper border of the range[/color]
                .Height = rng.Height - 1 'picture's heigth is larger than its width, this line makes it exactly fit int range height
                .Left = rng.Left + ((rng.Width - pic.Width) / 2) 'position in center of range width
            End If
        .Placement = xlMoveAndSize
        .PrintObject = True 'make sure picture gets printed
        End With
    End If
End Function

所以问题出现在第一个 If 条件(旋转 = 90) 在其他情况下,我似乎没有问题。

我用来测试的典型范围是:

"A7:N46"

【问题讨论】:

  • 您好 Infra,欢迎来到 SO。这似乎有很多代码只是为了定位图像,我认为可能有一个更简单的解决方案。您可以在帖子中添加确切的说明您需要做什么以及遇到什么问题吗?您是否想从字面上调整图像大小以适合一系列单元格?为什么旋转是个问题,您收到旋转的图像吗?

标签: excel vba image rotation


【解决方案1】:

感谢您的快速响应!

我尝试做的是将任何给定的图片放在预先确定的范围内。所以是的,我也会收到旋转的图片。

这个想法是用户选择图片(最多 4 张)并自动将它们排列和调整大小以适合一张预先确定的 Excel 工作表。因此,我制作了 4 个场景(具有不同的范围)如何将这些图片排列在一张纸上。这是通过 switch case 在另一个例程中完成的。

到目前为止,此代码适用于未旋转的图片。从那里开始旋转的那一刻起,它就变得凌乱了,图片“溢出”了。如果有其他方法可以解决我的问题,我很高兴在这里。

【讨论】:

    【解决方案2】:

    我遇到了与这里描述的完全相同的问题。目前尚不清楚当旋转 90 或 270 度时 shaperange 的 top 和 left 属性会发生什么。

    在轮换的情况下,我的解决方法是:

    • 创建一个新的隐藏工作表;
    • 插入旋转后的图片;
    • 硬编码图片的位置;
    • 将行复制到您想要的范围内;
    • 删除临时工作表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多