Bài vi?t này ch?ng t? làm th? nào ð? s? d?ng Microsoft Word ð? t?o ra m?t
sáp nh?p thý tài li?u b?ng cách s? d?ng t? ð?ng hóa t? Microsoft Visual Basic.
Có hai cách ð? ki?m soát m?t máy ch? t? ð?ng hóa: b?i
b?ng cách s? d?ng h?u ràng bu?c ho?c b?ng cách s? d?ng ð?u ràng bu?c. V?i h?u ràng bu?c, phýõng pháp ðý?c không b? ràng bu?c
cho ð?n khi th?i gian ch?y và các máy ch? t? ð?ng hóa tuyên b? nhý ð?i tý?ng. V?i ð?u
ràng bu?c, ?ng d?ng c?a b?n bi?t t?i th?i gian thi?t k? lo?i chính xác c?a ð?i tý?ng nó s?
giao ti?p v?i và có th? tuyên b? các ð?i tý?ng c?a nó nhý là m?t lo?i h?nh c? th?. Ði?u này
m?u s? d?ng s?m ràng bu?c, mà ðý?c xem là t?t hõn trong h?u h?t trý?ng h?p v? nó
dành hi?u su?t l?n hõn và t?t hõn lo?i an toàn.
Ð? s?m liên k?t v?i
m?t máy ch? t? ð?ng hóa, 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 này ðý?c th?c hi?n thông qua các Tham khaÒo h?p tho?i. Ð? m? các Tham khaÒo h?p tho?i h?p, b?m vào Tham khaÒo trên các Dýò aìn tr?nh ðõn. Ð?i v?i m?u này, b?n ph?i thêm m?t
tham chi?u ð?n thý vi?n ki?u cho Word trý?c khi b?n có th? ch?y m?.
Xây d?ng m?u t? ð?ng hóa
B?t ð?u Visual Basic, và t?o ra m?t d? án m?i c?a tiêu chu?n EXE.
Form1 ðý?c t?o ra theo m?c ð?nh.
Thêm m?t CommandButton ki?m soát ð? Form1.
Trên các Dýò aìn tr?nh ðõn, nh?p vào Tham khaÒo ð? m? các Tham khaÒo h?p tho?i
h?p.
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.
Nh?p vào Ok ð? ðóng nh?ng Tham khaÒo h?p tho?i.
Trong c?a s? m? cho Form1, thêm 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
wrdDoc.Select
Set wrdSelection = wrdApp.Selection
Set wrdMailMerge = wrdDoc.MailMerge
' Create the MailMerge Data file.
CreateMailMergeDataFile
' 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"
wrdSelection.TypeParagraph
wrdMergeFields.Add wrdSelection.Range, "Address"
wrdSelection.TypeParagraph
wrdMergeFields.Add wrdSelection.Range, "CityStateZip"
InsertLines 2
' Right align the line, and insert a date field
' with the current date.
wrdSelection.ParagraphFormat.Alignment = _
wdAlignParagraphRight
wrdSelection.InsertDateTime _
DateTimeFormat:="dddd, MMMM dd, yyyy", _
InsertAsField:=False
InsertLines 2
' Align the rest of the document.
wrdSelection.ParagraphFormat.Alignment = _
wdAlignParagraphJustify
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, _
NumColumns:=4
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 = _
wdGray25
' 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", _
"Instructor"
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, _
Address:="http://www.ee.stateu.tld"
' 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
wrdApp.Selection.TypeParagraph
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
wrdDataDoc.Tables(1).Rows.Add
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.Save
wrdDataDoc.Close False
End Sub
ID c?a bài: 220607 - L?n xem xét sau cùng: 21 Thaìng Taìm 2011 - Xem xét 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? khóa:
kbautomation kbhowto kbmt KB220607 KbMtvi
Máy d?ch
QUAN TROòNG: Bài vi?t này ðý?c d?ch b?ng ph?n m?m d?ch máy c?a Microsoft ch? không ph?i do con ngý?i d?ch. Microsoft cung c?p các bài vi?t do con ngý?i d?ch và c? các bài vi?t do máy d?ch ð? b?n có th? truy c?p vào t?t c? các bài vi?t trong Cõ s? Ki?n th?c c?a chúng tôi b?ng ngôn ng? c?a b?n. Tuy nhiên, bài vi?t do máy d?ch không ph?i lúc nào c?ng hoàn h?o. Lo?i bài vi?t này có th? ch?a các sai sót v? t? v?ng, cú pháp ho?c ng? pháp, gi?ng nhý m?t ngý?i ný?c ngoài có th? m?c sai sót khi nói ngôn ng? c?a b?n. Microsoft không ch?u trách nhi?m v? b?t k? s? thi?u chính xác, sai sót ho?c thi?t h?i nào do vi?c d?ch sai n?i dung ho?c do ho?t ð?ng s? d?ng c?a khách hàng gây ra. Microsoft c?ng thý?ng xuyên c?p nh?t ph?n m?m d?ch máy này.
Nh?p chu?t vào ðây ð? xem b?n ti?ng Anh c?a bài vi?t này:220607