【发布时间】:2016-03-10 00:27:04
【问题描述】:
我在 Microsoft Access 中有以下学生表:
name | email_address
--------------------
Student 1 | student1@example.com
Student 2 | student2@example.com
Student 3 | student3@example.com
我也有这样的 Microsoft Word 文档:
List of students
Name: __________________________________
Email address: ___________________________________
我想通过 VBA 代码完成一个新的 Microsoft Word 文档,使用以前的文档作为模板,因此输出如下:
List of students:
Name: Student 1
_________
Email address: student1@example.com
____________________
Name: Student 2
_________
Email address: student2@example.com
____________________
Name: Student 3
_________
Email address: student3@example.com
____________________
这里需要注意的是,我不能假设这张桌子只能容纳 3 名学生,如上例所示;它可能有 4、5、6……甚至可能是十亿!
我想通过 VBA 代码实现这一点,但不知道如何实现。到目前为止,我唯一的代码是:
Private Sub btnOpenDocument_Click()
OpenStudentListing
End Sub
Private Sub OpenStudentListing()
Dim appword As Word.Application
Dim doc As Word.Document
On Error Resume Next
Error.Clear
Set appword = GetObject(, "word.application")
If Err.Number <> 0 Then
Set appword = New Word.Application
appword.Visible = True
End If
filePath = ExtractTemplate(Student_Listing)
If IsNull(filePath) Then Exit Sub
Set doc = appword.Documents.Open(filePath)
'The document opens just fine, but now what?
End Sub
这甚至有可能实现吗?如果有,怎么做?
【问题讨论】:
-
为什么要使用VBA?使用 word 中的 mailmerge 功能,这将非常容易(google it)。