【问题标题】:Inserting Images Into a Word Document with VBA使用 VBA 将图像插入 Word 文档
【发布时间】:2020-02-19 15:01:29
【问题描述】:

我正在重用一些 VBA 代码将一批图像插入到 Word 文档中。 VBA 创建一个表格,然后在每个图像上方插入图像以及描述;目前是图像的文件名。

Sub AddPics()
    Application.ScreenUpdating = False

    Dim oTbl As Table, i As Long, j As Long, k As Long, StrTxt As String

    'Select and insert the Pics
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select image files and click OK"
        .Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
        .FilterIndex = 2

        If .Show = -1 Then

            'Add a 2-row by 2-column table with 7cm columns to take the images
            Set oTbl = Selection.Tables.Add(Selection.Range, 2, 2)
            With oTbl
                .AutoFitBehavior (wdAutoFitFixed)
                .Columns.Width = CentimetersToPoints(7)
                'Format the rows
                Call FormatRows(oTbl, 1)
            End With

            CaptionLabels.Add Name:="Picture"

            For i = 1 To .SelectedItems.Count

                j = Int((i + 1) / 2) * 2 - 1
                k = (i - 1) Mod 2 + 1

                'Add extra rows as needed
                If j > oTbl.Rows.Count Then
                    oTbl.Rows.Add
                    oTbl.Rows.Add
                    Call FormatRows(oTbl, j)
                End If

                'Insert the Picture
                ActiveDocument.InlineShapes.AddPicture _
                FileName:=.SelectedItems(i), LinkToFile:=False, _
                SaveWithDocument:=True, Range:=oTbl.Rows(j).Cells(k).Range

                'MsgBox (.SelectedItems(i).DateLastModified)


                'Get the Image name for the Caption
                StrTxt = Split(.SelectedItems(i), "\")(UBound(Split(.SelectedItems(i), "\")))
                StrTxt = ": " & Split(StrTxt, ".")(0)

                'Insert the Caption on the row below the picture
                With oTbl.Rows(j + 1).Cells(k).Range
                    .InsertBefore vbCr
                    .Characters.First.InsertCaption _
                    Label:="Picture", Title:=StrTxt, _
                    Position:=wdCaptionPositionBelow, ExcludeLabel:=False
                    .Characters.First = vbNullString
                    .Characters.Last.Previous = vbNullString
                End With

            Next
        Else
    End If

    End With
    Application.ScreenUpdating = True
    End Sub
    '
    Sub FormatRows(oTbl As Table, x As Long)
    With oTbl
    With .Rows(x)
    .Height = CentimetersToPoints(7)
    .HeightRule = wdRowHeightExactly
    .Range.Style = "Normal"
    End With
    With .Rows(x + 1)
    .Height = CentimetersToPoints(0.75)
    .HeightRule = wdRowHeightExactly
    .Range.Style = "Caption"
    End With
    End With
End Sub

我对 Application.FileDialog 对象不是很熟悉。在我看来,我仍然是 VBA 的新手。有没有办法像当前一样提取每个图像的 LastModifiedDate 并将其放入文档中代替文件名?

谢谢顺丰

【问题讨论】:

标签: vba ms-word


【解决方案1】:

您可以在 For 循环中添加/使用此代码:

Dim fs = Object
Set fs = CreateObject("Scripting.FileSystemObject")
debug.Print fs.GetFile(.SelectedItems(i)).DateLastModified

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多