导航:首页 > 文字图片 > 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插图片代码相关的资料

热点内容
楚慈文字图片 浏览:535
word让插入的图片都在同一位置 浏览:297
女长碎发型图片背影 浏览:785
慢慢长夜文字图片 浏览:169
朱蒂老师图片高清 浏览:67
微信头像大全高清星空图片 浏览:382
如何把word文档的图片导出 浏览:689
直头发发型图片女 浏览:981
word里的图片复制粘贴桌面 浏览:16
60岁发型图片大全 浏览:209
好可爱的女战士图片 浏览:546
动漫女孩图片卖全 浏览:380
如何将图片内表格转成word 浏览:957
小纹身文字图片大全 浏览:866
已生女孩的四维图片吧 浏览:105
世上最美丽的衣服图片 浏览:581
卡通动漫图片素材高清图片 浏览:226
男生头发干枯图片 浏览:511
玉衣服搭配图片 浏览:578
怎么还不开学的说说带图片 浏览:247