如何使用vba从access数据库中将图片插入word doc

时间:2018-04-08 05:27:20

标签: vba ms-access ms-word

我试图从访问附件字段中读取图片,然后将其插入到Word文档中。如下面的代码所示,我可以将附件读入文件,然后将其插入到Word文档中,但效率低下,我想要一种直接执行此操作的方法。任何想法?

Option Compare Database

Sub picloader()

    Dim appword As Word.Application
    Dim doc As Word.Document

    On Error Resume Next
    Err.Clear

    Set appword = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Set appword = New Word.Application

    End If

    appword.Visible = True
    appword.Activate

    'word doc to save picture
    Set doc1 = appword.Documents.Open("D:\file1.docx")


    Dim db As Database
    Set db = CurrentDb

    Dim picpath As String
    Dim people As String
    Dim rspictures As Variant

    'Dim tmp As Variant

    picpath = "D:\file1.jpg"

    Dim rs As DAO.Recordset

    people = "select * from persons"
    Set rs = CurrentDb.OpenRecordset(people)

    If Not (rs.EOF And rs.BOF) Then
        rs.MoveFirst

        Set rspictures = rs.Fields("Picture").Value

           While Not rspictures.EOF
                rspictures.Fields("FileData").SaveToFile (picpath)

                rspictures.MoveNext
           Wend
    End If

    Dim wrdPic As Word.InlineShape

    Set wrdPic = doc1.Bookmarks("picture").Range.InlineShapes.AddPicture(picpath, LinkToFile:=False, SaveWithDocument:=True)

             wrdPic.ScaleHeight = 10
             wrdPic.ScaleWidth = 10


    Exit Sub
    errHandler:
    MsgBox Err.Number & ": " & Err.Description

    End Sub

0 个答案:

没有答案