Word2013批量修改图片尺寸

0x00 前言

保持图片原始比例(锁定横纵比例)的情况下,批量调整为适应页面尺寸的图片大小。

名词

LockAspectRatio - 锁定横纵比例, MsoTrue if the specified shape retains its original proportions when you resize it. MsoFalse if you can change the height and width of the shape independently of one another when you resize it. Read/write MsoTriState.

wdInlineShapeLinkedPicture - 嵌入式连接图片(一般浏览器复制过来的图片类型)

wdInlineShapePicture - 嵌入式图片

0x01 使用宏

P.S. 2019.8.8 更新代码
代码如下:

Sub ResizePhotos()
Dim Shap As InlineShape
Dim maxWith
maxWith = CentimetersToPoints(16.79)

For Each Shap In ActiveDocument.InlineShapes

    Debug.Print Shap.Type; "Shap.Type"; wdInlineShapePicture

    If (Shap.Type = wdInlineShapeLinkedPicture) Or (Shap.Type = wdInlineShapePicture) Then

        If Shap.Width > maxWith Then
 
            ' Shap.LockAspectRatio = msoTrue
            Debug.Print "before width: "; Shap.Width
           Debug.Print "before Height: "; Shap.Height
           oW = Shap.Width
           oH = Shap.Height
           aspect = oH / oW 'aspect ratio
           nH = aspect * CentimetersToPoints(16.79) 'new width
           Shap.Width = CentimetersToPoints(16.79)
           Shap.Height = nH
           Debug.Print "after width: "; Shap.Width
           Debug.Print "after Height: "; Shap.Height

        End If
    End If

Next

End Sub

其中, 16.79 为厘米单位的A4页面宽度,CentimetersToPoints为厘米转像素的函数

1. 打开隐藏的宏工具

点击左上角的文件-选项-自定义功能区,勾选左侧的"开发工具"
在这里插入图片描述
此时,文档功能区出现开发工具
在这里插入图片描述

2. 创建宏函数

点击宏,或者使用快捷点Alt + F8,打开宏管理,创建名为ResizePhotos的宏函数
在这里插入图片描述
复制粘贴代码即可
在这里插入图片描述

运行宏函数

方法一、 在编辑窗口按F5即可
在这里插入图片描述
方法二、在管理窗口选中函数,点击运行
在这里插入图片描述

0x02 使用F4重复上一次action

  1. 右击第一幅图片,打开"设置大小与位置"
  2. 调整宽度到16.79厘米,确定
    在这里插入图片描述
  3. 点击下一张图片,按F4键,图片即可被修改(MAC 使用command + Y)
  4. 依次修改后面的图片即可

0x03 Tips

  1. 宏自带纵横比锁定变量,但是设置为True时,并没有生效,所以自己算了一遍
  2. 图片的类型需要注意,不确定的时候可以打印出来
  3. 宏是vb语言,之前没学过,但是上手还蛮快的

0x04 参考文献

https://superuser.com/questions/940771/how-can-i-resize-multiple-images-in-a-ms-word-document
https://zhuanlan.zhihu.com/p/42588748
https://answers.microsoft.com/en-us/office/forum/office_2010-word/creating-a-macro-for-resizing-several-photos-in/b251f117-8d17-4522-bac4-d128c32587ba
https://docs.microsoft.com/en-us/office/vba/api/word.wdinlineshapetype

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值