【问题标题】:How to have a user select images and then insert them into Word PDF?如何让用户选择图像,然后将它们插入 Word PDF?
【发布时间】:2019-10-28 05:50:18
【问题描述】:

我正在寻找一种方法来打开文件资源管理器,以便用户可以选择图片或图片文件夹,然后将其插入到生成的 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文档。我需要在文字之后插入图片。

【问题讨论】:

  • 您的实际问题是什么?
  • 是否有可能在运行上述代码时出现文件资源管理器,以便用户选择图片或图片文件夹。在他们选择图片后,它将被插入到上面代码生成的文档中
  • 如果是我该怎么做?

标签: vba image ms-word


【解决方案1】:

您想使用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

【讨论】:

  • @masoncowing 这就是您要寻找的答案吗?如果是这样,请接受此作为正确答案,否则将 cmets 添加到您所在的位置,以便访问此问题的人可以获得更多参考。
  • 这部分对选择文件很有帮助,但是我仍然不清楚如何将图片插入到我生成的word文档中。
  • @masoncowing 我添加了一小部分,向您展示如何添加图像。它只会接受用户选择的文件路径。这是一个链接,显示了Adding images to a Word Document 的许多其他方式
  • @masoncowing 最后一点对您回答问题有帮助吗?
  • 这确实帮助我将图片添加到 word 文档中,但是它将图片插入到我正在运行宏的文档中,而不是从运行宏生成的文档中。相当混乱,但我正在努力解决它。
猜你喜欢
  • 1970-01-01
  • 2012-05-12
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多