Sub ExportPDFAsImageAndInsert() Dim InputFilePath As String Dim OutputFolder As String Dim OutputFilePath As String Dim currentDoc As Document Dim vPics As Variant, k As Long ' 声明当前Word文档变量 ' 获取当前活动的Word文档 Set currentDoc = ActiveDocument ' 创建文件对话框对象以选择PDF文件路径 With Application.FileDialog(msoFileDialogFilePicker) .Title = "选择PDF文件" .Filters.Clear .Filters.Add "PDF文件", "*.pdf" If .Show = -1 Then InputFilePath = .SelectedItems(1) Else Exit Sub ' 用户取消选择 End If End With ' 创建文件夹对话框对象以选择图片输出文件夹路径 With Application.FileDialog(msoFileDialogFolderPicker) .Title = "选择图片输出文件夹" If .Show = -1 Then OutputFolder = .SelectedItems(1) Else Exit Sub ' 用户取消选择 End If End With OutputFilePath = SavePDFAs(InputFilePath, OutputFolder, "jpeg") ' 调用 GetFilePathsInFolder 函数并将返回的数组赋值给 vPics 变量 ' 插入图片到当前Word文档中 vPics = GetFilePathsInFolder(OutputFolder) If IsEmpty(vPics) Then MsgBox "空文件夹。" Exit Sub End If For k = UBound(vPics) To LBound(vPics) currentDoc.InlineShapes.AddPicture (vPics(k)) Next End Sub 以上代码中,插入图片的顺序反了,请改成正序
时间: 2024-02-10 09:23:22 浏览: 107
要将图片插入顺序改为正序,你需要将循环的方向从从大到小改为从小到大。以下是修改后的代码示例:
```vb
Sub ExportPDFAsImageAndInsert()
Dim InputFilePath As String
Dim OutputFolder As String
Dim OutputFilePath As String
Dim currentDoc As Document
Dim vPics As Variant, k As Long ' 声明当前Word文档变量
' 获取当前活动的Word文档
Set currentDoc = ActiveDocument
' 创建文件对话框对象以选择PDF文件路径
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "选择PDF文件"
.Filters.Clear
.Filters.Add "PDF文件", "*.pdf"
If .Show = -1 Then
InputFilePath = .SelectedItems(1)
Else
Exit Sub ' 用户取消选择
End If
End With
' 创建文件夹对话框对象以选择图片输出文件夹路径
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "选择图片输出文件夹"
If .Show = -1 Then
OutputFolder = .SelectedItems(1)
Else
Exit Sub ' 用户取消选择
End If
End With
OutputFilePath = SavePDFAs(InputFilePath, OutputFolder, "jpeg")
' 调用 GetFilePathsInFolder 函数并将返回的数组赋值给 vPics 变量
vPics = GetFilePathsInFolder(OutputFolder)
If IsEmpty(vPics) Then
MsgBox "空文件夹。"
Exit Sub
End If
' 插入图片到当前Word文档中
For k = LBound(vPics) To UBound(vPics)
currentDoc.InlineShapes.AddPicture (vPics(k))
Next
End Sub
```
这段代码将图片的插入顺序从大到小改为从小到大,通过将循环的起始索引 `LBound(vPics)` 设置为数组的第一个索引,终止索引 `UBound(vPics)` 设置为数组的最后一个索引,实现了正序插入图片的效果。
希望这个修改能满足你的需求。如果还有其他问题,请随时提问。
阅读全文