如何在VBA中调整Word Picture Content Control尺寸并设置默认大小
关于Word图片内容控件尺寸操作的解决方案
可以获取内容控件尺寸,也可以通过代码固定控件大小,即使删除内部图片后也不会自动收缩,也支持预设默认空控件尺寸,具体实现方法如下:
1. 读取内容控件当前尺寸
直接读取控件内现有内嵌图片的宽高参数即可,单位为磅:
Dim picCC As ContentControl ' 替换为你自己的内容控件引用,也可以通过Tag/Title属性定位 Set picCC = ActiveDocument.ContentControls.Item("你的控件标题") Dim currentW As Single, currentH As Single If picCC.Range.InlineShapes.Count > 0 Then currentW = picCC.Range.InlineShapes(1).Width currentH = picCC.Range.InlineShapes(1).Height End If
2. 删除图片后保持原有尺寸
删除图片前先记录当前尺寸,赋值给控件的占位图片属性即可:
' 删除原有图片 picCC.Range.InlineShapes.Item(1).Delete ' 给占位图设置之前记录的尺寸,控件会保持大小不变 With picCC.PlaceholderPicture .Width = currentW .Height = currentH ' 如需锁定长宽比可打开下面的配置 '.LockAspectRatio = msoTrue End With
3. 预设空控件的默认尺寸
无需插入图片,直接设置占位图的尺寸即可让空内容控件保持固定大小:
' 示例:将空图片内容控件尺寸设为宽6厘米、高4厘米 With picCC.PlaceholderPicture .Width = CentimetersToPoints(6) .Height = CentimetersToPoints(4) .LockAspectRatio = msoFalse End With ' 新插入图片内容控件时也可以直接指定默认大小 Dim newPicCC As ContentControl Set newPicCC = ActiveDocument.ContentControls.Add(wdContentControlPicture, Selection.Range) With newPicCC.PlaceholderPicture .Width = CentimetersToPoints(5) .Height = CentimetersToPoints(5) End With
注意事项
- 以上方法适配Word 2013及更高版本,低版本Word无
PlaceholderPicture属性,可插入1px透明占位图片替代实现固定尺寸 - 不要将内容控件所在段落的行高设为固定值,否则会导致控件显示被裁切
内容的提问来源于stack exchange,提问作者Bruce Jakeway
相关产品推荐
相关产品推荐

