【问题标题】:Run Macro in Word when picture is added/removed from Picture Content control在图片内容控件中添加/删除图片时在 Word 中运行宏
【发布时间】:2021-03-02 09:57:41
【问题描述】:

我正在使用以下宏来调整通过 word 中的图片内容控件功能添加的图像的大小,作为从图片内容控件中删除图像后发生的默认调整大小的解决方案(不保留大小并且图像框恢复为更大的尺寸)。

此宏会调整大小,但我希望了解如何在每次从图片内容控件框中添加或删除图像时自动运行宏?

Sub ResizePhotos()
Dim pic As InlineShape

For Each pic In ActiveDocument.InlineShapes
    With pic
        .LockAspectRatio = msoTrue
        If .Width > .Height Then
            .Height = InchesToPoints(0.5)
        Else
            .Width = InchesToPoints(0.5)
        End If
         .Range.Paragraphs(1).Alignment = wdAlignParagraphCenter
    End With
Next

结束子

【问题讨论】:

  • 添加或删除图片时不会触发事件。你能得到的最接近的可能是ContentControlOnExit
  • @TimothyRylatt 感谢您的反馈 - 您能否就编写代码提供任何指导来支持这一点?任何帮助将不胜感激
  • 为什么不先查找the documentation

标签: vba ms-word


【解决方案1】:

您只需要一个内容控制退出宏。例如:

Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
With CCtrl
  If .Type = wdContentControlPicture Then
    With .Range.InlineShapes(1)
      .LockAspectRatio = True
      .Height = InchesToPoints(0.5)
    End With
  End If
End With
Application.ScreenUpdating = True
End Sub

【讨论】:

    【解决方案2】:
        Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
    Application.ScreenUpdating = False
    With CCtrl
      If .Type = wdContentControlPicture Then
        With .Range.InlineShapes(1)
          .LockAspectRatio = True
          .Height = InchesToPoints(0.5)
        End With
      End If
    End With
    Application.ScreenUpdating = True
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多