新浪博客

Word中如何用VBA 将所有图片加上边框

2011-08-12 16:54阅读:
Word中如何用VBA 将所有图片加上边框
1 在word下你可以自己录制想要的代码
例如:选中一张图片,工具-宏-录制新宏,然后,格式-边框和阴影,给图片加上黑边框,然后,alt+F11打开VB编译器,就看到代码了。

2.picCount = ActiveDocument.InlineShapes.Count '计算文件中图片数目
在Word中,插入的图片已被转化为 InlineShape 对象。
之后用For循环语句,给所有图片加黑色边框。
单个图片加边框的语句,你自己可以录制一个宏看看,将录制的宏代码拷贝到For循环中修改一下即可。
3.计算图片数目是为了For循环用的,有多少图片就要循环多少次,给所有图片都加上边框。
For i = 1 to picCount
......
Next i
中间就是上面那段代码,把InlineShapes(1)改为InlineShapes(i)

全部的代码如下:
Sub Macro1()
picCount = ActiveDocument.InlineShapes.Count
For i = 1 To picCount
With Selection.InlineShapes(i)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With
ShowVisualBasicEditor = True
Next i
End Sub

上面会出现错误 不知道为什么?
但是 将代码改成下面的就行了
Sub Macro1()
For Each iShape In ActiveDocument.InlineShapes
With iShape
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With
ShowVisualBasicEditor = True

Next iShape
End Sub

我的更多文章

下载客户端阅读体验更佳

APP专享