WORD批量插入图片和文件名


来源:网络整理
发布时间:2019-11-12 10:42:00
查看次数:

内容提要:因为需要,找了两个VBA代码。


另一个
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


转载自:http://club.excelhome.net/forum.php?mod=viewthread&tid=778982&page=1#pid5315103

第1页 第2页

相关文章
·四行选项变一行的快捷方法11-14
·小册子打印代码07-07
·word 如何自动生成目录08-02
·Win10电脑账号密码设置、修改、取消12-06
·堪称「神器」的电脑软件/网站02-13
·分类汇总后复制的问题05-27
·去掉word中的隔行回车03-23
·文件夹设置为系统隐藏文件11-19
·让Windows XP自动登录的三种方法09-14
·批量设置PowerPoint字体及行间距——VBA宏10-02
最新文章
阅读排行