首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >图片旋转时VBA excel .Left .Top

图片旋转时VBA excel .Left .Top
EN

Stack Overflow用户
提问于 2021-03-08 19:04:24
回答 2查看 145关注 0票数 0

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

代码语言:javascript
运行
复制
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),在其他情况下,我似乎没有问题。

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

代码语言:javascript
运行
复制
"A7:N46"
EN

回答 2

Stack Overflow用户

发布于 2021-03-08 22:15:46

感谢您的快速响应!

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

这个想法是,用户选择图片(最多4张),并自动排列和调整大小,以适应一个预先确定的excel工作表。所以我做了4个场景(具有不同的范围)如何在一张纸上排列这些图片。这是在另一个例程中通过开关箱完成的。

到目前为止,这个代码可以处理非旋转的图片。从旋转的那一刻起,它就变得一团糟,图片“出血”了。如果有其他方法来解决我的问题,我很乐意在这里。

票数 0
EN

Stack Overflow用户

发布于 2021-08-12 12:36:46

我遇到了与这里描述的完全相同的问题。当发生旋转90或270时,尚不清楚形状范围的顶部和左侧属性会发生什么。

在旋转的情况下,我的解决方法是:

  • 创建新的隐藏工作表;
  • 插入旋转的图片;
  • 硬编码图片的位置;
  • 将行复制到所需的区域;

< code >H19删除临时工作表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66528633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档