???? ID: 285176 - ????? ???????: 02 ?????? 2010 - ??????: 2.0

SQL ????? ?? XML ?? ????? ???? ??? ???????-???? ??? ???? ???? ?? ??? Word ?? ???????? ???? ?? ??? ???? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

?? ???? ????????? ???? ?? ?? ???????-???? ????????? ?? ???? ?? ??? ??? ??? ???? ???? ?? Microsoft Word ?? ???????? ???? ?? ??? ???? ????? Word ?? XML ???? ?? ???? ??? ???? ?? ??? ???? ????? ?? ??? ??? ????? ???? ?? ??? ??? ???? ???? ???? ??? ????? illustrates ?????? ????? ????? (ASP) ?? ??????? ?? ??? ???? ??? ????? ?? XML recordset ?? streams ???? ???????-???? ????????? ?? ??????? ?? ??? ??????? ?? ?? ???? ??? ?? ??? ???? ?? ??? ???? ???? ????? ?? ??? ??? ??????? ??? ????? ?? ????? ?? ???? ?? ??? Word ???????? ??? ????? ???????? ??? ???? ?? ??? ?? XML ???? ??? ?????? ???? ???

???? ???????

ActiveX ???? ???????? (ADO) 2.5 ?? ??? ??? XML ?????? ????? ???? ?? ??? recordset ???? ?? ?????? ???? ??? ASP ?? ??? ??? ????? ?? ????? ??, ?? ?? ???? ??? ???? ??????? ?? ??? ADO recordset ????? ?? XML ?? ??? ??? ???? ??????? ?? ??? ???? ???? ?????? ???? ???, ??????? XML recordset ????? ?? ?????? ?? ??? ??? ???? ?? ????? ?? ??? ADO ????? ?? ???? ???? XML ?? ADO ?? ????? ???? ?? ???????? ???????? ?? ???? ???? ?? ??? ?? ???? ?? "??????" ??? ?? ??????

Illustration ?? ????????, ?? ??? ????? ????? ???? ?????PubsMicrosoft SQL Server ??? ???????? ??? ??, ???? ??? ????? ???? ?? ???? ?? ???? ?? ??? ???? ??? ADO ??? ???? ??? ???? ??????? ?? ?? ??? ???? ???? ?? ??? ????????

?????

  1. ???? ??? ????? ?? ???? ??????? ?????????? WordMailMerge ??? ???? ?? ?? ??????
  2. ???? ??? ?? ??? ??? 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>
    					
  3. ???? ??? ?? ??? ??? 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
    %>
    					
  4. ????????? ????sServer???? ??? ????? ?? ????? ????, ?? ????????? ???? ?? ??? Default.asp ??? ??sSQLServer???? SQL ????? ?? ????? ???? ?? ??? Getdata.asp ??? ???
  5. Internet Explorer ?? ??????? ???? ?? ???? ?? ??? ??????? ????HTTP://Servername> /WordMailMerge/Default.asp(????Servername> ???? ??? ????? ?? ??? ??)?
  6. Word ???????? ?? ??? ???? ???? ?? ??? ??? ??? ?? ' ??? ????? ????? ??????? ?? ????? ???? ??, Word ??? ???? ?? ???????????? ?????? ???? ??? ??? ??? ????????? ?? ??? ????? ???? ???

??????

???? ??????? ?? ???, Microsoft ?????? ??? ??? ???? ????? ?? ??? ????? ???? ???????? ????? ????::
195826  (http://support.microsoft.com/kb/195826/ ) ???????-???? ????????? ?? CreateObject ????
258512  (http://support.microsoft.com/kb/258512/ ) ??? ?? ??? ???? ?? ??? ??? ??? ???? ????? ?? ??? Visual Basic ?? Word ?? ???????? ???? ?? ??? ???? ????
278260  (http://support.microsoft.com/kb/278260/ ) Word ???????? ?? ??? ?? ??? ???? ?? ??? ??? ??? ???? ????? ?? ??? Microsoft ????????? ???? (MFC) ?? ????? ???? ????
263247  (http://support.microsoft.com/kb/263247/ ) ??? ADO recordset ?? XML ?? ??????? ???? ?? ??? ???? ????
252767  (http://support.microsoft.com/kb/252767/ ) Visual Basic ??? ??? ADO recordset ?? ???????????? ?? XML ?? ??????? ???? ?? ??? ???? ????

???? ???? ???? ??:
  • Microsoft Office Word 2007
  • Microsoft Word 2000
  • Microsoft Active Server Pages 4.0
??????: 
kbmerge kbexpertiseinter kbautomation kbhowto kbmt KB285176 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:285176  (http://support.microsoft.com/kb/285176/en-us/ )