Lm th? no ? t? ?ng ho Microsoft Word ? th?c hi?n Mail Merge t? Visual Basic

D?ch tiu ? D?ch tiu ?
ID c?a bi: 220607 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny


Bi vi?t ny ch?ng t? lm th? no ? s? d?ng Microsoft Word ? t?o ra m?t sp nh?p th ti li?u b?ng cch s? d?ng t? ?ng ha t? Microsoft Visual Basic.


C hai cch ? ki?m sot m?t my ch? t? ?ng ha: b?i b?ng cch s? d?ng h?u rng bu?c ho?c b?ng cch s? d?ng ?u rng bu?c. V?i h?u rng bu?c, phng php ?c khng b? rng bu?c cho ?n khi th?i gian ch?y v cc my ch? t? ?ng ha tuyn b? nh ?i t?ng. V?i ?u rng bu?c, ?ng d?ng c?a b?n bi?t t?i th?i gian thi?t k? lo?i chnh xc c?a ?i t?ng n s? giao ti?p v?i v c th? tuyn b? cc ?i t?ng c?a n nh l m?t lo?i h?nh c? th?. i?u ny m?u s? d?ng s?m rng bu?c, m ?c xem l t?t hn trong h?u h?t tr?ng h?p v? n dnh hi?u su?t l?n hn v t?t hn lo?i an ton.

? s?m lin k?t v?i m?t my ch? t? ?ng ha, b?n ph?i ?t m?t tham chi?u ?n lo?i r?ng h? ph?c v? th vi?n. Trong Visual Basic, i?u ny ?c th?c hi?n thng qua cc Tham khao h?p tho?i. ? m? cc Tham khao h?p tho?i h?p, b?m vo Tham khao trn cc D an tr?nh n. ?i v?i m?u ny, b?n ph?i thm m?t tham chi?u ?n th vi?n ki?u cho Word tr?c khi b?n c th? ch?y m?.

Xy d?ng m?u t? ?ng ha

  1. B?t ?u Visual Basic, v t?o ra m?t d? n m?i c?a tiu chu?n EXE. Form1 ?c t?o ra theo m?c ?nh.
  2. Thm m?t CommandButton ki?m sot ? Form1.
  3. Trn cc D an tr?nh n, nh?p vo Tham khao ? m? cc Tham khao h?p tho?i h?p.
  4. Ch?n Microsoft Word 11,0 ?i t?ng th vi?n ? t?o ra m?t tham chi?u ?n th vi?n ki?u Microsoft Office Word 2003. ?i v?i Word 2002, ch?n Microsoft Word 10,0 ?i t?ng Th vi?n. ?i v?i Word 2000, ch?n Microsoft Word 9,0 ?i t?ng Th vi?n. ?i v?i Word 97, ch?n Microsoft Word 8,0 ?i t?ng th vi?n.
  5. Nh?p vo Ok ? ng nh?ng Tham khao h?p tho?i.
  6. Trong c?a s? m? cho Form1, thm m? sau y:
    Option Explicit
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Private Sub Command1_Click()
      Dim wrdSelection As Word.Selection
      Dim wrdMailMerge As Word.MailMerge
      Dim wrdMergeFields As Word.MailMergeFields
      Dim StrToAdd As String
    ' Create an instance of Word, and make it visible.
      Set wrdApp = CreateObject("Word.Application")
      wrdApp.Visible = True
    ' Add a new document.
      Set wrdDoc = wrdApp.Documents.Add
      Set wrdSelection = wrdApp.Selection
      Set wrdMailMerge = wrdDoc.MailMerge
    ' Create the MailMerge Data file.
    ' Create a string, and insert it into the document.
      StrToAdd = "State University" & vbCr & _
               "Electrical Engineering Department"
      wrdSelection.ParagraphFormat.Alignment = wdAlignParagraphCenter
      wrdSelection.TypeText StrToAdd
      InsertLines 4
    ' Insert merge data.
      wrdSelection.ParagraphFormat.Alignment = wdAlignParagraphLeft
      Set wrdMergeFields = wrdMailMerge.Fields
      wrdMergeFields.Add wrdSelection.Range, "FirstName"
      wrdSelection.TypeText " "
      wrdMergeFields.Add wrdSelection.Range, "LastName"
      wrdMergeFields.Add wrdSelection.Range, "Address"
      wrdMergeFields.Add wrdSelection.Range, "CityStateZip"
      InsertLines 2
    ' Right align the line, and insert a date field
    ' with the current date.
      wrdSelection.ParagraphFormat.Alignment = _
      wrdSelection.InsertDateTime _
            DateTimeFormat:="dddd, MMMM dd, yyyy", _
      InsertLines 2
    ' Align the rest of the document.
      wrdSelection.ParagraphFormat.Alignment = _
      wrdSelection.TypeText "Dear "
      wrdMergeFields.Add wrdSelection.Range, "FirstName"
      wrdSelection.TypeText ","
      InsertLines 2
      ' Create a string, and insert it into the document.
      StrToAdd = "Thank you for your recent request for next " & _
          "semester's class schedule for the Electrical " & _
          "Engineering Department. Enclosed with this " & _
          "letter is a booklet containing all the classes " & _
          "offered next semester at State University.  " & _
          "Several new classes will be offered in the " & _
          "Electrical Engineering Department next semester.  " & _
          "These classes are listed below."
      wrdSelection.TypeText StrToAdd
      InsertLines 2
      ' Insert a new table with 9 rows and 4 columns.
      wrdDoc.Tables.Add wrdSelection.Range, NumRows:=9, _
      With wrdDoc.Tables(1)
        ' Set the column widths
        .Columns(1).SetWidth 51, wdAdjustNone
        .Columns(2).SetWidth 170, wdAdjustNone
        .Columns(3).SetWidth 100, wdAdjustNone
        .Columns(4).SetWidth 111, wdAdjustNone
        ' Set the shading on the first row to light gray.
        .Rows(1).Cells.Shading.BackgroundPatternColorIndex = _
        ' Format the first row in bold.
        .Rows(1).Range.Bold = True
        ' Center the text in Cell (1,1).
        .Cell(1, 1).Range.Paragraphs.Alignment = wdAlignParagraphCenter
        ' Fill each row of the table with data.
        FillRow wrdDoc, 1, "Class Number", "Class Name", "Class Time", _
        FillRow wrdDoc, 2, "EE220", "Introduction to Electronics II", _
           "1:00-2:00 M,W,F", "Dr. Jensen"
        FillRow wrdDoc, 3, "EE230", "Electromagnetic Field Theory I", _
           "10:00-11:30 T,T", "Dr. Crump"
        FillRow wrdDoc, 4, "EE300", "Feedback Control Systems", _
           "9:00-10:00 M,W,F", "Dr. Murdy"
        FillRow wrdDoc, 5, "EE325", "Advanced Digital Design", _
           "9:00-10:30 T,T", "Dr. Alley"
        FillRow wrdDoc, 6, "EE350", "Advanced Communication Systems", _
           "9:00-10:30 T,T", "Dr. Taylor"
        FillRow wrdDoc, 7, "EE400", "Advanced Microwave Theory", _
           "1:00-2:30 T,T", "Dr. Lee"
        FillRow wrdDoc, 8, "EE450", "Plasma Theory", _
           "1:00-2:00 M,W,F", "Dr. Davis"
        FillRow wrdDoc, 9, "EE500", "Principles of VLSI Design", _
           "3:00-4:00 M,W,F", "Dr. Ellison"
      End With
      ' Go to the end of the document.
      wrdApp.Selection.GoTo wdGoToLine, wdGoToLast
      InsertLines 2
      ' Create a string, and insert it into the document.
      StrToAdd = "For additional information regarding the " & _
                 "Department of Electrical Engineering, " & _
                 "you can visit our Web site at "
      wrdSelection.TypeText StrToAdd
      ' Insert a hyperlink to the Web page.
      wrdSelection.Hyperlinks.Add Anchor:=wrdSelection.Range, _
      ' Create a string, and insert it into the document.
      StrToAdd = ".  Thank you for your interest in the classes " & _
                 "offered in the Department of Electrical " & _
                 "Engineering.  If you have any other questions, " & _
                 "please feel free to give us a call at " & _
                 "555-1212." & vbCr & vbCr & _
                 "Sincerely," & vbCr & vbCr & _
                 "Kathryn M. Hinsch" & vbCr & _
                 "Department of Electrical Engineering" & vbCr
      wrdSelection.TypeText StrToAdd
    ' Perform mail merge.
      wrdMailMerge.Destination = wdSendToNewDocument
      wrdMailMerge.Execute False
    ' Close the original form document.
      wrdDoc.Saved = True
      wrdDoc.Close False
    ' Notify user we are done.
      MsgBox "Mail Merge Complete.", vbMsgBoxSetForeground
    ' Release references.
      Set wrdSelection = Nothing
      Set wrdMailMerge = Nothing
      Set wrdMergeFields = Nothing
      Set wrdDoc = Nothing
      Set wrdApp = Nothing
    ' Clean up the temp file.
      Kill "C:\DataDoc.doc"
    End Sub
    Public Sub InsertLines(LineNum As Integer)
      Dim iCount As Integer
    ' Insert "LineNum" blank lines.
      For iCount = 1 To LineNum
      Next iCount
    End Sub
    Public Sub FillRow(Doc As Word.Document, Row As Integer, _
                       Text1 As String, Text2 As String, _
                       Text3 As String, Text4 As String)
      With Doc.Tables(1)
        ' Insert the data into the specific cell.
        .Cell(Row, 1).Range.InsertAfter Text1
        .Cell(Row, 2).Range.InsertAfter Text2
        .Cell(Row, 3).Range.InsertAfter Text3
        .Cell(Row, 4).Range.InsertAfter Text4
      End With
    End Sub
    Public Sub CreateMailMergeDataFile()
      Dim wrdDataDoc As Word.Document
      Dim iCount As Integer
    ' Create a data source at C:\DataDoc.doc that contains the field data.
      wrdDoc.MailMerge.CreateDataSource Name:="C:\DataDoc.doc", _
            HeaderRecord:="FirstName, LastName, Address, CityStateZip"
    ' Open the file to insert the data.
      Set wrdDataDoc = wrdApp.Documents.Open("C:\DataDoc.doc")
      For iCount = 1 To 2
      Next iCount
    ' Fill in the data.
      FillRow wrdDataDoc, 2, "Steve", "DeBroux", _
            "4567 Main Street", "Buffalo, NY  98052"
      FillRow wrdDataDoc, 3, "Jan", "Miksovsky", _
            "1234 5th Street", "Charlotte, NC  98765"
      FillRow wrdDataDoc, 4, "Brian", "Valentine", _
            "12348 78th Street  Apt. 214", "Lubbock, TX  25874"
    ' Save and close the file.
      wrdDataDoc.Close False
    End Sub
  7. B?m phm F5 v Click vo nt ? ch?y cc m?u.


? bi?t thm thng tin v? vn ph?ng t? ?ng ha, truy c?p vo cc pht tri?n vn ph?ng Microsoft h? tr? site sau:

Thu?c tnh

ID c?a bi: 220607 - L?n xem xt sau cng: 21 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
  • Microsoft Word 2002 Standard Edition
  • Microsoft Word 2000 Standard Edition
  • Microsoft Word 97 Standard Edition
T? kha:
kbautomation kbhowto kbmt KB220607 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:220607

Cung cp Phan hi


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com