我是宏的新手,我正在尝试执行以下操作:
将硬盘驱动器上的文件夹中的所有图像导入到Word文件中-每个图像都有其自己的页面,并在保持其比例的同时调整大小以填充页面(A4纵向)。
我已经设法对每个图像进行“导入”部分,并使用以下代码获取自己的页面:
Sub BilderImport()
Dim Path As String
Dim Img As Object
Dim fs As Object
Dim f As Object
Dim fc As Object
Dim i As Integer
Dim fsize As Integer
Path = "C:\tmp"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(Path)
Set ff = f.Files
i = 0
fsize = ff.Count
For Each Img In ff
If Right(Img.Name, 4) = ".bmp" Or Right(Img.Name, 4) = ".jpg" Or Right(Img.Name, 4) = ".gif" Or Right(Img.Name, 4) = ".png" Then
i = i + 1
Selection.InlineShapes.AddPicture FileName:=Img
Selection.InsertBreak Type:=wdLineBreak 'Add file name below every img
Selection.TypeText Text:=Img.Name
If i < fsize Then 'No line break after the last img
Selection.InsertBreak Type:=wdPageBreak 'Inserts line break after every img
End If
End If
Next
End Sub
我现在遇到的一个问题:Word还在每张图像后添加一个换行符,如果图像已经很高,则此换行符跳至下一页,由于我的中断,该行添加了额外的空白页已经添加。
如何预防或检查?
答案 0 :(得分:1)
尝试:
Sub BilderImport()
Dim Path As String
Dim fs As Object
Dim ff As Variant
Dim Img As Variant
Dim i As Long
Dim fsize As Long
Path = "C:\tmp"
Set fs = CreateObject("Scripting.FileSystemObject")
Set ff = fs.GetFolder(Path).Files
i = 0
fsize = ff.Count
With ActiveDocument
For Each Img In ff
Select Case Right(Img.Name, 4)
Case ".bmp", ".jpg", ".gif", ".png"
i = i + 1
.Characters.Last.InlineShapes.AddPicture FileName:=Img
.Characters.Last.InsertBefore Chr(11) & Img.Name & Chr(12) End Select
Next
'No page break after last pic
.Characters.Last.Previous.Delete
End With
End Sub
答案 1 :(得分:0)
有两种解决方法。我希望将两者结合使用:
根据图像大小,第二点就足够了。
第三种可能性:输入图像之前和之后,您必须检查页数。但这会使整个宏变慢。