导航

word 插入图片,调整大小

Posted on 2011-06-06 21:07  半点忧伤  阅读(5025)  评论(1编辑  收藏  举报

Sub Macro1()
    ActiveDocument.Tables(1).Cell(3, 1).Select
    Selection.InlineShapes.AddPicture FileName:="C:\a.jpg", LinkToFile:=True, SaveWithDocument:=True
End Sub
HF

Private   Sub   Command1_Click()
        Dim   xApp   As   New   Word.Application
        Dim   xDoc   As   Document
        Dim   xShape   As   InlineShape
       
        Set   xApp   =   New   Word.Application
       
        '添加文档并返回文档对象
        Set   xDoc   =   xApp.Documents.Add
       
        '插入一个图形Shape并返回其对象引用
        Set   xShape   =   xDoc.InlineShapes.AddPicture( "D:\2003   document\My   Pictures\1_151_115.jpg ")
       
        '设置Shape的大小
        xShape.Width   =   200
        xShape.Height   =   200
       
        Set   xShape   =   Nothing
       
        '保存
        xDoc.SaveAs   "c:\dfgh.doc "
       
        xDoc.Close
        xApp.Quit
       
        Set   xDoc   =   Nothing
        Set   xApp   =   Nothing
End   Sub


这是上面代码的注释,可能对你的理解有一些帮助。如果需要多次插入图片,重复调用
        '插入一个图形Shape并返回其对象引用
        Set   xShape   =   xDoc.InlineShapes.AddPicture( "D:\2003   document\My   Pictures\1_151_115.jpg ")
       
        '设置Shape的大小
        xShape.Width   =   200
        xShape.Height   =   200
       
        Set   xShape   =   Nothing
就可以了。

'*****************************

哈哈~终于可以了 O YEAH~ 感谢wangz的耐心介绍!另外我找到了另外一种方式,互联网的资源~分享如下:

word批量修改图片大小——固定长宽篇
这部分要说的是把word中的所有图片修改成固定的并且相同的长和宽!
1、打开word,工具-宏-宏(或者直接按Alt+F8)进入宏的界面,如下面所示,输入一个宏名,宏名自己起,能记住就行!
2、宏名起好了,单击“创建”进入Visual Basic 编辑器,输入如下代码并保存
Sub setpicsize() '设置图片大小
Dim n'图片个数
On Error Resume Next '忽略错误

For n = 1 To ActiveDocument.InlineShapes.Count 'InlineShapes类型图片
ActiveDocument.InlineShapes(n).Height = 400 '设置图片高度为 400px
ActiveDocument.InlineShapes(n).Width = 300 '设置图片宽度 300px
Next n

For n = 1 To ActiveDocument.Shapes.Count 'Shapes类型图片
ActiveDocument.Shapes(n).Height = 400 '设置图片高度为 400px
ActiveDocument.Shapes(n).Width = 300 '设置图片宽度 300px
Next n
End Sub

3、返回word,工具-宏-宏(或者直接按Alt+F8),再次进入宏的界面,选择刚才编辑好的宏,并单击“运行”按钮,就可以了!(图片多时,可能会花一些时间)word批量修改图片大小——按比例缩放篇
这部分要说的是把word中的所有图片按比例缩放!
具体操作同上,只是代码部分稍做修改,代码如下:
Sub setpicsize() '设置图片大小
Dim n'图片个数
Dim picwidth
Dim picheight
On Error Resume Next '忽略错误

For n = 1 To ActiveDocument.InlineShapes.Count 'InlineShapes类型图片
picheight = ActiveDocument.InlineShapes(n).Height
picwidth = ActiveDocument.InlineShapes(n).Width
ActiveDocument.InlineShapes(n).Height = picheight * 1.1 '设置高度为1.1倍
ActiveDocument.InlineShapes(n).Width = picwidth * 1.1 '设置宽度为1.1倍
Next n
For n = 1 To
ActiveDocument.Shapes.Count 'Shapes类型图片
picheight = ActiveDocument.Shapes(n).Height
picwidth = ActiveDocument.Shapes(n).Width
ActiveDocument.Shapes(n).Height = picheight * 1.1 '设置高度为1.1倍
ActiveDocument.Shapes(n).Width = picwidth * 1.1 '设置宽度为1.1倍
Next n
End Sub
'********************************

今天收获很大,又找到了批量给图片加边框的方法,分享一下:

Dim i As Integer

For i = 1 To ActiveDocument.InlineShapes.Count


With ActiveDocument.InlineShapes(i)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth100pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth100pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth100pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth100pt
.Color = wdColorAutomatic
End With
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth100pt
.DefaultBorderColor = wdColorAutomatic
End With
Next i

End sub

'****************************