摘要
本文演示如何从客户端脚本自动执行 Microsoft Word,以便对标签执行邮件合并。 Word 没有将 XML 数据用作邮件合并的数据源的直接方法。 此示例演示了将 XML 记录集从 Web 服务器流式传输到客户端的 Active Server Pages (ASP) 代码。 客户端脚本将此 XML 数据转换为本地到客户端的分隔文本文件,然后使用本地文本文件作为邮件合并数据源自动执行邮件合并。
更多信息
(ADO) 2.5 及更高版本的 ActiveX 数据对象允许以 XML 格式保留记录集数据。 在 Web 服务器上使用 ASP,可以从数据库生成 ADO 记录集,并将数据作为 XML 返回到客户端。 反过来,客户端可以使用 ADO 读取 XML 记录集并根据需要操作数据。 有关将 ADO 与 XML 配合使用的其他资源的链接,请参阅本文的“参考”部分。
出于说明目的,下面的示例使用 Microsoft SQL Server 中的示例 Pubs 数据库。 但是,类似的代码可能用于执行与可以建立 ADO 连接的任何数据库的邮件合并。
示例
在 Web 服务器上,创建名为 WordMailMerge 的虚拟目录。
使用记事本创建名为 Default.asp 的文件,其中包含下面给出的代码。 将文件保存到 WordMailMerge 虚拟目录。
<%@ Language=VBScript %> <HTML> <BODY> <SCRIPT LANGUAGE=VBScript> Sub CreateDataDoc(oApp) ' Declare variables. Dim sServer,oDoc,oRS,sTemp,sHead,oRange,oField ' Place your server's name here. sServer = "<servername>" ' Create a new document. Set oDoc = oApp.Documents.Add ' Create a new recordset. Set oRS = CreateObject("ADODB.Recordset") ' Open the XML recordset from the server oRS.Open "http://" & sServer & "/WordMailMerge/Getdata.asp" ' Convert the recordset to a string. sTemp = oRS.GetString(2, -1, vbTab) ' 2 = adClipString ' Append the field names to the front of the string. For Each oField In oRS.Fields sHead = sHead & oField.Name & vbTab Next ' Strip off the last tab. sTemp = Mid(sHead, 1, Len(sHead) - 1) & vbCrLf & sTemp ' Get a range object and insert the text into the document. Set oRange = oDoc.Range oRange.Text = sTemp ' Convert the text to a table. oRange.ConvertToTable vbTab ' Save the document to a temp file. oDoc.SaveAs "C:\data.doc" ' Close the document (no save). oDoc.Close False End Sub Sub ButtonClick() Dim oApp Dim oDoc Dim oMergedDoc ' Create an instance of Word. Set oApp = CreateObject("Word.Application") ' Create our data file. CreateDataDoc oApp ' Add a new document. Set oDoc = oApp.Documents.Add With oDoc.MailMerge ' Add our fields. .Fields.Add oApp.Selection.Range, "au_fname" oApp.Selection.TypeText " " .Fields.Add oApp.Selection.Range, "au_lname" oApp.Selection.TypeParagraph .Fields.Add oApp.Selection.Range, "city" oApp.Selection.TypeText ", " .Fields.Add oApp.Selection.Range, "state" oApp.Selection.TypeParagraph .Fields.Add oApp.Selection.Range, "zip" oApp.Selection.TypeParagraph ' Create an autotext entry. Dim oAutoText Set oAutoText = oApp.NormalTemplate.AutoTextEntries.Add _ ("MyLabelLayout", oDoc.Content) oDoc.Content.Delete .MainDocumentType = 1 ' 1 = wdMailingLabels ' Open the saved data source. .OpenDataSource "C:\data.doc" ' Create a new document. oApp.MailingLabel.CreateNewDocument "5160", "", _ "MyLabelLayout", , 4 ' 4 = wdPrinterManualFeed .Destination = 0 ' 0 = wdSendToNewDocument ' Execute the mail merge. .Execute oAutoText.Delete End With ' Close the mail merge edit document. oDoc.Close False ' Get the current document. Set oMergedDoc = oApp.ActiveDocument ' Show Word to the user. oApp.Visible = True ' Uncomment these lines to save the merged document locally. 'oMergedDoc.SaveAs "C:\test.doc" 'oMergedDoc.Close False 'oApp.Quit False End Sub </SCRIPT> <INPUT type=button value="Create Word Document" onclick="VBScript:ButtonClick"> </BODY> </HTML>
使用记事本创建一个名为 Getdata.asp 的文件,其中包含下面提供的代码。 将文件保存到 WordMailMerge 虚拟目录。
<%@ Language=VBScript %> <% Dim oConn,oRS,strConn,sSQLServer ' Build the connection string. Replace <username> and <strong password> with ' the username and password of an account that has permissions on the database. sSQLServer = "<servername>" strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;" & _ "User ID=<username>;Password=<strong password>;Initial Catalog=pubs;Data Source=" & sSQLServer ' Set our return content type. Response.ContentType = "text/xml" ' Create a connection. set oConn = Server.CreateObject("ADODB.Connection") ' Open the connection. oConn.Open strConn ' Execute the SQL statement. set oRS = oConn.Execute(“SELECT * FROM AUTHORS”) ' Save the recordset in the Response object. oRS.Save Response,1 %>
将 Default.asp 中的 sServer 变量更改为指向 Web 服务器,并将 Getdata.asp 中的 sSQLServer 变量更改为指向SQL Server。
启动 Internet Explorer 并浏览到 https://servername/WordMailMerge/Default.asp (其中服务器名是 Web 服务器) 的名称。
单击网页上的按钮以自动执行 Word 并执行邮件合并。 自动化完成后,Word 将显示一个新文档,其中包含邮件合并导致的邮件标签。
参考
有关详细信息,请单击以下文章编号以查看 Microsoft 知识库中的文章:
258512 如何从 Visual Basic 自动执行 Word 以创建邮件标签的邮件合并