导航:首页 > 文字图片 > word宏vba插图片代码

word宏vba插图片代码

发布时间:2024-12-30 16:16:12

1. word vba 插入图片

Sub 批量插入图片()
Dim myfile As FileDialog
Set myfile = Application.FileDialog(msoFileDialogFilePicker)
With myfile
.InitialFileName = "E:\工作文件" ‘这里输入你要插入图片的目标文件夹
If .Show = -1 Then
For Each Fn In .SelectedItems
Selection.Text = Basename(Fn) '这两句移到这里
Selection.EndKey
If Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末
Selection.TypeParagraph '在文末添加一空段
Else
Selection.MoveDown
End If
Set MyPic = Selection.InlineShapes.AddPicture(FileName:=Fn, SaveWithDocument:=True) '按比例调整相片尺寸
WidthNum = MyPic.Width
c = 6 '在此处修改相片宽,单位厘米
MyPic.Width = c * 28.35
MyPic.Height = (c * 28.35 / WidthNum) * MyPic.Height
If Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末
Selection.TypeParagraph '在文末添加一空段
Else
Selection.MoveDown
End If
Next Fn
Else
End If
End With
Set myfile = Nothing
End Sub
Function Basename(FullPath) '取得文件名
Dim x, y
Dim tmpstring
tmpstring = FullPath
x = Len(FullPath)
For y = x To 1 Step -1
If Mid(FullPath, y, 1) = "\" Or _
Mid(FullPath, y, 1) = ":" Or _
Mid(FullPath, y, 1) = "/" Then
tmpstring = Mid(FullPath, y + 1)
Exit For
End If
Next
Basename = Left(tmpstring, Len(tmpstring) - 4)
End Function
执行此代码后,弹出的选择对话框, 全选目标文件夹下的所有图片文件之后,点击确定。然后静静的等待电脑完成处理工作,次数word会进入无响应状态。图片越多,无响应的时间越长。

2. 如何运用vba将指定图片插入word中

例如,新建一个4行1列的表格,然后在Cell(3, 1)内插入图片:
Sub Macro1()
Dim mysel
mysel = ActiveDocument.Tables(1).Cell(3, 1)
mysel.InlineShapes.AddPicture FileName:="C:\a.jpg", LinkToFile:=True, SaveWithDocument:=True
End Sub

3. 寻找WORD VBA高手解决WORD批量插入图片程序的问题

Selection.Text = Basename(Fn) '这两句移到这里
Selection.EndKey

这两句移一下位置,其它不变。

Sub 批量插入图片()
Dim myfile As FileDialog
Set myfile = Application.FileDialog(msoFileDialogFilePicker)
With myfile
.InitialFileName = "D:\111"
If .Show = -1 Then
For Each Fn In .SelectedItems
Selection.Text = Basename(Fn) '这两句移到这里
Selection.EndKey
Set mypic = Selection.InlineShapes.AddPicture(FileName:=Fn, SaveWithDocument:=True)
'按比例调整相片尺寸
WidthNum = mypic.Width
c = 18 '在此处修改相片宽,单位厘米
mypic.Width = c * 28.35
mypic.Height = (c * 28.35 / WidthNum) * mypic.Height
If Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末
Selection.TypeParagraph '在文末添加一空段
Else
Selection.MoveDown
End If
If Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末
Selection.TypeParagraph '在文末添加一空段
Else
Selection.MoveDown
End If
Next Fn
Else
End If
End With
Set myfile = Nothing
End Sub
Function Basename(FullPath) '取得文件名
Dim x, y
Dim tmpstring
tmpstring = FullPath
x = Len(FullPath)
For y = x To 1 Step -1
If Mid(FullPath, y, 1) = "\" Or _
Mid(FullPath, y, 1) = ":" Or _
Mid(FullPath, y, 1) = "/" Then
tmpstring = Mid(FullPath, y + 1)
Exit For
End If
Next
Basename = Left(tmpstring, Len(tmpstring) - 4)
End Function

4. 在word中如何用VBA实现插入多张图片

要从两个方面考虑:
1、如何把现有文档中的图片导出?这个代码片断可以参考一下:
Set ImageStream = CreateObject("ADODB.Stream")
With ImageStream
.Type = 1
.Open
.Write ActiveDocument.InlineShapes(1).Range.EnhMetaFileBits
.SaveToFile "d:\Temp\Output.bmp"
.Close
End With
Set ImageStream = Nothing

2、如何把已经到处的图片导入到新文档中?这个函数调用可以参考一下:
ActiveDocument.InlineShapes.AddPicture

如果还是不明白的话,请补充提问。

_____
补充:
请问楼主你运行那个代码片断得到的*完整*错误信息是什么?报错的时候,系统一般会把光标移动到出错的那行代码上,你看到的是哪一行代码出错呢?
我看你贴上来的代码应该是没有什么语法上的错误,“Exit For”的确是不应该要的,但那也不是语法错误啊。

5. 想在word页面中,批量插入图片,使得每页有4张图片均匀分布在页面中,该怎么做呢

这是统一设置word文档中的图片样式,你的问题我不会,但是希望这个例子会对你有点提示。

使用宏:
一,在word中按alt+f11组合键,进入VBA模式
二,在左边的工程资源管理器中找到你的word文档,在其上右键/添加/模块
三,把下面代码复制,粘贴进去.
四,更改数值, 改一下宽度和高度数值(10),点运行(类似播放按钮.)或f5,即可设置文档中全部图片
Sub Macro()
Mywidth=10'10为图片宽度(厘米)
Myheigth=10'10为图片高度(厘米)
For Each iShape In ActiveDocument.InlineShapes
iShape.Height = 28.345 * Myheigth
iShape.Width = 28.345 * Mywidth
Next iShape
End Sub

阅读全文

与word宏vba插图片代码相关的资料

热点内容
验孕棒如何看出怀孕图片 浏览:565
整齐发型图片 浏览:972
遮脸男生图片是帅哥吗 浏览:641
刺客伍六七素描版简单图片 浏览:782
中年干练发型图片 浏览:254
潮男刘海发型图片 浏览:583
黑木纹高清图片 浏览:87
男士的短发型图片 浏览:740
速写衣服褶皱局部图片 浏览:984
word中怎么随心放置图片 浏览:734
安踏衣服新款图片女士 浏览:400
时尚胖妹发型图片 浏览:319
word文档里的图片会变位置 浏览:958
动漫女生美腿穿上丝袜的图片 浏览:25
微信怎么打印图片出来 浏览:397
美女亲亲图片 浏览:614
ps怎么做菱形图片 浏览:234
铅笔q版男生图片 浏览:812
美女内裤脏的图片 浏览:171
把图片批量转换成word文字 浏览:174