我正在寻找一种方法来启动文件资源管理器,以便用户可以选择图片或图片文件夹,然后将其插入到生成的pdf文件。
我使用了多个subs来使用预定的图片,但对于这个应用程序,图片每次都会不同。
Set wdApp = New Word.Application
With wdApp
.Visible = True
.Activate
.Documents.Add
With .Selection
.BoldRun
.Font.Size = 20
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.TypeText TextBox2.Value
.TypeText Chr(13)
.BoldRun
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Font.Size = 16
.TypeText "Report"
.TypeText Chr(13)
我有上面的代码来创建word文档。我需要在文字后面插入图片。
发布于 2019-06-14 01:58:21
您希望使用File Dialog允许用户选择图片。
下面是一个如何使用它的示例,您只需根据您的特定需求进行更新即可。请查看上面的文档以了解更多选项。
将.AllowMultiSelect
更改为True
,然后您可以循环用户选择的每个文件。
Private Sub TestingFileDialog()
Dim Fd As FileDialog
Set Fd = Application.FileDialog(msoFileDialogFilePicker)
With Fd
'GENERAL SETTINGS
.AllowMultiSelect = False
.Title = "Please select a picture"
.ButtonName = "Choose a picture"
.InitialFileName = "c:\"
.InitialView = msoFileDialogViewList
'FILE FILTERS
.Filters.Clear
.Filters.Add "JPG", "*.JPG"
.Filters.Add "PNG", "*.PNG"
'SHOW FORM, WILL RETURN TRUE IF USER SELECTS A FILE
If .Show = True Then
'Get file name
Dim FilePath As String
FilePath = .SelectedItems(1)
'do whatever you need to from here...
Else
'No file chosen, do something else.
End If
End With
End Sub
要添加图像,请使用wdApp.Selection.InlineShapes.AddPicture
。这将接受用户选择的文件路径。
使用你的代码,它可能看起来像这样:
Sub AddImagesToWordDocument()
Set wdApp = New Word.Application
With wdApp
.Visible = True
.Activate
.Documents.Add
With .Selection
.BoldRun
.Font.Size = 20
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.TypeText TextBox2.Value
.TypeText Chr(13)
.BoldRun
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Font.Size = 16
.TypeText "Report"
.TypeText Chr(13)
Dim Fd As FileDialog
Set Fd = Application.FileDialog(msoFileDialogFilePicker)
Fd.AllowMultiSelect = True
Fd.Filters.Clear
Fd.Filters.Add "JPG", "*.JPG"
Fd.Filters.Add "PNG", "*.PNG"
If Fd.Show = True Then
'Loop files and add them
Dim FileIndex As Long
For FileIndex = 1 To Fd.SelectedItems.Count
.InlineShapes.AddPicture Fd.SelectedItems(FileIndex)
Next FileIndex
Else
'No file chosen, do something else if need be.
End If
End With
End With
End Sub
https://stackoverflow.com/questions/56583806
复制相似问题