批量导入图片到word并添加文件名

Posted aozima

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量导入图片到word并添加文件名相关的知识,希望对你有一定的参考价值。

Sub InsertPic()
    Dim myfile As FileDialog
    Set myfile = Application.FileDialog(msoFileDialogFilePicker)
    With myfile
        .InitialFileName = "F:\"
        If .Show = -1 Then
            For Each fn In .SelectedItems

                Set mypic = Selection.InlineShapes.AddPicture(FileName:=fn, SaveWithDocument:=True)
                ‘按比例调整相片尺寸
                WidthNum = mypic.Width
                c = 10         ‘在此处修改相片宽,单位厘米
                mypic.Width = c * 28.35
                mypic.Height = (c * 28.35 / WidthNum) * mypic.Height
                If Selection.Start = ActiveDocument.Content.End - 1 Then  ‘如光标在文末
                    Selection.TypeParagraph    ‘在文末添加一空段
                Else
                    Selection.MoveDown
                End If
                Selection.Text = Basename(fn)    ‘函数取得文件名
                Selection.EndKey

                If Selection.Start = ActiveDocument.Content.End - 1 Then  ‘如光标在文末
                    Selection.TypeParagraph    ‘在文末添加一空段
                Else
                    Selection.MoveDown
                End If
            Next fn
        Else
        End If
    End With
    Set myfile = Nothing
End Sub

Function Basename(FullPath)    ‘取得文件名
    Dim x, y
    Dim tmpstring
    tmpstring = FullPath
    x = Len(FullPath)
    For y = x To 1 Step -1
        If Mid(FullPath, y, 1) = "\" Or _
           Mid(FullPath, y, 1) = ":" Or _
           Mid(FullPath, y, 1) = "/" Then
            tmpstring = Mid(FullPath, y + 1)
            Exit For
        End If
    Next
    Basename = Left(tmpstring, Len(tmpstring) - 4)
End Function

 

以上是关于批量导入图片到word并添加文件名的主要内容,如果未能解决你的问题,请参考以下文章

利用VBA如何将批量图片导入WORD的指定表格格式里?

利用VBA如何将批量图片导入WORD的指定表格格式里?

word用vba批量导入图片文件时,怎样保留图片原有大小,而不是统一固定大小?跪谢!下为您原来提供的代码

文件夹照片批量插入word

java中怎么让导入的excle表中每一条数据对应文件夹中对应的图片

将多套图片批量分别插入对应的word中