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

热点内容
设计小孩的衣服图片 浏览:426
淘宝网男生衣服图片 浏览:350
谢谢的动图片可爱 浏览:371
孤独的男生图片动漫 浏览:290
跟男生要照片的图片 浏览:613
动漫图片抱小熊的 浏览:716
身上起红块怎么办图片 浏览:892
发胖的男生图片 浏览:207
插画大师图片男生 浏览:630
图片男生帅气阳光 浏览:94
word图片保存太小了怎么办 浏览:272
闪电侠图片可爱卡通 浏览:814
如何消除微信图片的红色画笔 浏览:369
金牛座动漫图片简单 浏览:435
口臭男生图片 浏览:670
套路女生的段子图片 浏览:629
图片如何调ai格式 浏览:447
明星个性短发发型图片 浏览:603
关于卡通的画简单图片 浏览:658
古风帅哥美女真人图片 浏览:297