简体   繁体   English

使用VBA为应用程序打开Word模板文档的多个副本

[英]Open multiple copies of Word template document using VBA for applications

I am developing a database application where users open a Word template document and merge this with database records. 我正在开发一个数据库应用程序,用户可以在其中打开Word模板文档并将其与数据库记录合并。 I have successfully written code that does this for one record where I open a template file and replace items in the template document with DB data. 我已经成功编写了针对一条记录执行此操作的代码,在该记录中,我打开了模板文件,并用DB数据替换了模板文档中的项目。 I have tried opening the template document multiple times (once for each DB record), but this opens a Word dialogue box prompting the user to open the second and subsequent documents as read only (not very elegant) and I get a Word Normal.dotm error when each of the documents is closed. 我尝试多次打开模板文档(每个DB记录一次),但这会打开一个Word对话框,提示用户以只读方式打开第二个及后续文档(不是很优雅),并且我得到一个Word Normal.dotm关闭每个文档时出错。 So, how can I use one template document to create multiple word documents at the same time. 因此,如何使用一个模板文档同时创建多个word文档。 Two options as I see them are to 1) Save one doc in a new name before creating other documents or 2) Have one document with multiple pages (one per DB record) but to do this I would have to copy and paste the template document contents once for every record, but I don't see how to do this. 我看到的两个选择是:1)在创建其他文档之前以新名称保存一个文档,或者2)拥有一个包含多个页面的文档(每个数据库记录一个),但是要做到这一点,我必须复制并粘贴模板文档每条记录的内容一次,但是我不知道该怎么做。 Please keep in mind that I am experienced in database programming and only a rudimentary knowledge of VB for applications, so the more explicit you can be, the more useful it will be. 请记住,我在数据库编程方面有丰富的经验,并且只对VB具有基本的应用程序知识,因此您越明确,它就越有用。

Thanks in advance. 提前致谢。

This function will create a new document for each record based on a specified template. 此功能将基于指定的模板为每个记录创建一个新文档。 I'm using a preset bookmark in the Word template to add data from the database. 我在Word模板中使用预设书签来添加数据库中的数据。 This sub would run in the database. 该子程序将在数据库中运行。

Public Sub CreateDocs()

    Dim oWord As Word.Application
    Set oWord = New Word.Application
    Dim oDocument As Word.Document
    Dim oDatabase As DAO.Database
    Set oDatabase = CurrentDb

    ' where to save our files
    Dim strDocPath As String
    strDocPath = CurrentProject.Path & "\"

    ' preset bookmark in template
    Dim oBookMark As Word.Bookmark

    ' query with records
    Dim oRecordset As DAO.Recordset
    Set oRecordset = oDatabase.OpenRecordset("qrySomeQuery")

    ' template file
    Dim strTemplateName As String
    strTemplateName = "C:\users\you\Documents\Doc1.dotx"

    ' hide Word
    oWord.WindowState = wdWindowStateMinimize
    oWord.Visible = False

    While Not oRecordset.EOF
        ' create new document from template
        Set oDocument = oWord.Documents.Add(strTemplatePath, False, , False)

        ' get reference to bookmark in template
        Set oBookMark = oDocument.Bookmarks("bkmkSomePlace")

        ' add our data
        oBookMark.Range.Text = oRecordset.Fields(0).Value & vbTab & oRecordset.Fields(1).Value

        ' Save and close
        ' Saving using one of the query fields that is unique
        oDocument.SaveAs strDocPath & "generatedFile" & oRecordset.Fields(0).Value, wdFormatDocumentDefault
        oDocument.Close

        ' next record
        oRecordset.MoveNext
    Wend

    oWord.Close
    Set oWord = Nothing
    If Not oRecordset Is Nothing Then oRecordset.Close
    Set oRecordset = Nothing
    oDatabase.Close
    Set oDatabase = Nothing
    Set oDocument = Nothing
End Sub

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

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