好代码,当然不能只知道收藏,分享给需要的朋友。摘录请注明:
引自:http://miaozk2006.blog.163.com/blog/static/38247058201372684539815/
1、WORD中首先把某待转的某一图片调整到要求的大小,点击这张自己设定的样本图片,打开宏编辑器,拷入下列代码。直接运行即可。
注明:本代码摘息网络,调试测试通过。
Sub 按选择的图片样本设置图片大小()
Dim s
Dim r, SelectShape As Boolean
If Selection.InlineShapes.Count = 1 Then
SelectShape = True
ElseIf Selection.ShapeRange.Count = 1 Then
SelectShape = True
End If
If SelectShape And Selection.InlineShapes.Count = 1 Then
Set s = Selection.InlineShapes(1)
r = Array(s.Height, s.Width)
ElseIf SelectShape Then
Set s = Selection.ShapeRange(1)
r = Array(s.Height, s.Width)
End If
If SelectShape Then
For Each s In ActiveDocument.InlineShapes
s.Height = r(0)
s.Width = r(1)
Next s
For Each s In ActiveDocument.Shapes
If InStr(s.Name, "Picture") > 0 Then
s.Height = r(0)
s.Width = r(1)
End If
Next s
MsgBox "已经完成对图片大小的设置"
Else
MsgBox "没有选定样本图片"
End If
End Sub
2、在宏代码更改图片的大小,进行全文批量设置。具体代码如下:
Dim iSha As InlineShape
For Each iSha In ActiveDocument.InlineShapes
If iSha.Type = wdInlineShapePicture Then
iSha.LockAspectRatio = msoFalse '不锁定纵横比
iSha.Width = CentimetersToPoints(16) '宽5CM
iSha.Height = CentimetersToPoints(24.5) '高5CM
End If
Next
End Sub
相关文章参考:
VBA语言基础
各种Excel VBA的命令
VB6 中将数据导出到 Excel 提速之法
VBA语法200句
各种Excel VBA的命令
Execl编程详解
VB软件操作EXCEL教程:※VB全面控制Excel