我正在寻找一种启动文件浏览器的方法,以便用户可以选择图片或图片文件夹,然后将其插入生成的pdf文档中。
我已经使用多个潜艇使用预定的图片,但是对于此应用程序,图片每次都会有所不同。
def encode(numbers, choices):
#0 <= numbers[i] < choices[i] for all i
n = numbers[0]
for x,y in zip(numbers[1:],choices[1:]):
n = n*y + x
return n
def decode(n,choices):
numbers = []
for d in choices[:0:-1]:
n,r = divmod(n,d)
numbers.append(r)
numbers.append(n)
return numbers[::-1]
我具有上面的代码来创建word文档。我需要在文字后面插入图片。
答案 0 :(得分:0)
您要使用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