首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让用户选择图像,然后将其插入Word PDF?

如何让用户选择图像,然后将其插入Word PDF?
EN

Stack Overflow用户
提问于 2019-06-13 23:23:12
回答 1查看 130关注 0票数 0

我正在寻找一种方法来启动文件资源管理器,以便用户可以选择图片或图片文件夹,然后将其插入到生成的pdf文件。

我使用了多个subs来使用预定的图片,但对于这个应用程序,图片每次都会不同。

代码语言:javascript
运行
复制
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文档。我需要在文字后面插入图片。

EN

回答 1

Stack Overflow用户

发布于 2019-06-14 01:58:21

您希望使用File Dialog允许用户选择图片。

下面是一个如何使用它的示例,您只需根据您的特定需求进行更新即可。请查看上面的文档以了解更多选项。

.AllowMultiSelect更改为True,然后您可以循环用户选择的每个文件。

代码语言:javascript
运行
复制
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。这将接受用户选择的文件路径。

使用你的代码,它可能看起来像这样:

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56583806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档