简体   繁体   English

将 Access 中的附加文档传输到 Word 模板

[英]Transfering attached documents in Access to a Word Template

I currently have a form in MSAccesss that gets users to attach any relevant documents and a script that runs an automation of populating data from Access into a Word Template.我目前在 MSAccesss 中有一个表单,可以让用户附加任何相关文档,还有一个脚本可以自动将 Access 中的数据填充到 Word 模板中。

The code that I have is not being flagged down as a bug or has any compiling errors, however, the attachment does not seem to be appearing on the generated word document.我拥有的代码没有被标记为错误或有任何编译错误,但是,附件似乎没有出现在生成的 word 文档中。

Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'create a new instance of word if not opened
    Set appWord = CreateObject("Word.Application")
End If

Dim doc As Word.Document
Set doc = Documents.Add(CurrentProject.Path & "\BCPPrototype_Template.dotm")

Dim rsa As DAO.Recordset
Set rsa = CurrentDb.OpenRecordset("heh_attachment")


If rsa.RecordCount > 0 Then
    doc.Bookmarks("call_tree").Range.InsertFile (CurrentProject.Path & "\" & rsa!FileName)
End If

Set doc = Nothing
rsa.close
set rsa = nothing

end sub

Edit:编辑:

Implementing what was suggested below, current code is实现下面的建议,当前代码是

Dim rsa As DAO.Recordset2, fd As DAO.Field2
Set rsa = CurrentDb.OpenRecordset("SELECT AttachmentFieldName FROM TableName")
Set fd = rsa("AttachmentFieldName")
fd("FileData").SaveToFile CurrentProject.Path
If rsa.RecordCount > 0 Then
    doc.Bookmarks("call_tree").Range.InsertFile (CurrentProject.Path & "\" & rsa!FileName)
end if 

but the file simply isn't appearing.但该文件根本没有出现。 I tried to comment the code to save the file and manually save it in the same folder too, same issue of file attachment not appearing occurs.我尝试注释代码以保存文件并手动将其保存在同一文件夹中,同样的文件附件问题未出现。 I also tried without the bookmark and simply referring to the cell range, same issue occurs.我也尝试不使用书签并简单地引用单元格范围,也会出现同样的问题。

ideally, what I am trying to accomplish is that, by inserting the file attachment in generated word document.理想情况下,我想要完成的是,通过在生成的 Word 文档中插入文件附件。 I can access the file from the generated word document by clicking on it.我可以通过单击从生成的 Word 文档中访问该文件。

If file is stored in an Attachment field, it must first be saved out to folder location.如果文件存储在附件字段中,则必须首先将其保存到文件夹位置。

Dim rsa As DAO.Recordset2, fd As DAO.Field2
Set rsa = CurrentDb.OpenRecordset("SELECT AttachmentFieldName FROM TableName")
Set fd = rsa("AttachmentFieldName")
fd("FileData").SaveToFile CurrentProject.Path

Follow with code to insert file into Word document.按照代码将文件插入 Word 文档。 Options for insertion are:插入选项包括:

  1. contents of a file (doc, docx, txt) can be inserted as more content in the Word document文件(doc、docx、txt)的内容可以作为更多内容插入到 Word 文档中
    doc.Range.InsertFile "file path/name here"

  2. embed an image file (jpg, bmp, tif, wim, png)嵌入图像文件(jpg、bmp、tif、wim、png)
    doc.InlineShapes.AddPicture "file path/name here"

  3. file can be embedded in an object frame that either displays content or an icon and double-click opens the original file from folder location文件可以嵌入到显示内容或图标的对象框架中,然后双击从文件夹位置打开原始文件
    doc.InlineShapes.AddOLEObject , "file path/name here"

  4. a hyperlink string that opens the original file from folder location从文件夹位置打开原始文件的超链接字符串
    doc.Hyperlinks.Add Anchor:=Selection.Range, Address:="file path/name here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM