用VBA将“C:\Users\wuxil\OneDrive\桌面\大衣柜进料图纸\大衣柜进料图纸”的JPG格式图片,逐页插入到word,word页面横向,图片大小200x187
时间: 2023-10-02 11:12:03 浏览: 137
您可以使用以下VBA代码将JPG格式的图片逐页插入到Word文档中,并设置页面横向和图片大小为200x187:
```vba
Sub InsertImagesToWord()
Dim wordApp As Object
Dim wordDoc As Object
Dim filePath As String
Dim imgPath As String
Dim imgShape As Object
' 设置文件路径和图片路径
filePath = "C:\Users\wuxil\OneDrive\桌面\大衣柜进料图纸\大衣柜进料图纸"
' 创建Word应用程序对象
Set wordApp = CreateObject("Word.Application")
' 打开一个新的Word文档
Set wordDoc = wordApp.Documents.Add
' 设置页面横向
wordDoc.PageSetup.Orientation = 1
' 循环插入图片
imgPath = Dir(filePath & "\*.jpg")
Do While imgPath <> ""
' 插入图片
Set imgShape = wordDoc.Shapes.AddPicture(FileName:=filePath & "\" & imgPath, _
LinkToFile:=False, SaveWithDocument:=True)
' 调整图片大小
imgShape.LockAspectRatio = msoFalse ' 不锁定纵横比例
imgShape.Width = 200
imgShape.Height = 187
' 添加分页符
wordApp.Selection.InsertBreak Type:=7
' 继续下一页插入图片
imgPath = Dir
Loop
' 显示Word应用程序窗口
wordApp.Visible = True
' 清理对象
Set imgShape = Nothing
Set wordDoc = Nothing
Set wordApp = Nothing
End Sub
```
阅读全文