Lm th? no ? t? ?ng ho Microsoft Word b?ng cch s? d?ng Visual Basic ? t?o m?t ti li?u m?i

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

TM T?T

Bi vi?t t?ng b?c ny m t? cch t?o ra m?t m?i ti li?u trong Word b?ng cch s? d?ng t? ?ng ha t? Visual Basic.

M?u m?

M?u m? trong bi vi?t ny ch?ng t? lm th? no ? lm vi?c sau:
  • Chn o?n vn v?i vn b?n v ?nh d?ng.
  • Duy?t v s?a ?i cc ph?m vi khc nhau trong v?ng m?t ti li?u.
  • Chn b?ng, ?nh d?ng b?ng v c cc b?ng v?i d? li?u.
  • Thm m?t bi?u ?.
? t?o m?t ti li?u Word m?i b?ng cch s? d?ng t? ?ng ha t? tr?c quan C b?n, lm theo cc b?c sau:
  1. Trong Visual Basic, 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. Trn cc D an tr?nh n, nh?p voTham khao, b?m vo m?t trong cc ty ch?n sau y v b?m Ok:
    • Cho Office Word 2007, nh?p vo Microsoft Word 12,0 ?i t?ng th vi?n.
    • ?i v?i Word 2003, b?m vo Microsoft Word 11,0 ?i t?ng th vi?n.
    • ?i v?i Word 2002, b?m vo Microsoft Word 10,0 Th vi?n ?i t?ng.
    • ?i v?i Word 2000, b?m vo Microsoft Word 9,0 Th vi?n ?i t?ng.
  3. Thm m?t i?u khi?n CommandButton Form1.
  4. Thm m? sau vo cc s? ki?n b?m vo cho Command1:
        Dim oWord As Word.Application
        Dim oDoc As Word.Document
        Dim oTable As Word.Table
        Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
        Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
        Dim oRng As Word.Range
        Dim oShape As Word.InlineShape
        Dim oChart As Object
        Dim Pos as Double
    
        'Start Word and open the document template.
        Set oWord = CreateObject("Word.Application")
        oWord.Visible = True
        Set oDoc = oWord.Documents.Add
        
        'Insert a paragraph at the beginning of the document.
        Set oPara1 = oDoc.Content.Paragraphs.Add
        oPara1.Range.Text = "Heading 1"
        oPara1.Range.Font.Bold = True
        oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
        oPara1.Range.InsertParagraphAfter
        
        'Insert a paragraph at the end of the document.
        '** \endofdoc is a predefined bookmark.
        Set oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks("\endofdoc").Range)
        oPara2.Range.Text = "Heading 2"
        oPara2.Format.SpaceAfter = 6
        oPara2.Range.InsertParagraphAfter
        
        'Insert another paragraph.
        Set oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks("\endofdoc").Range)
        oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:"
        oPara3.Range.Font.Bold = False
        oPara3.Format.SpaceAfter = 24
        oPara3.Range.InsertParagraphAfter
        
        'Insert a 3 x 5 table, fill it with data and make the first row
        'bold,italic.
        Dim r As Integer, c As Integer
        Set oTable = oDoc.Tables.Add(oDoc.Bookmarks("\endofdoc").Range, 3, 5)
        oTable.Range.ParagraphFormat.SpaceAfter = 6
        For r = 1 To 3
            For c = 1 To 5
                oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
            Next
        Next
        oTable.Rows(1).Range.Font.Bold = True
        oTable.Rows(1).Range.Font.Italic = True
        
        'Add some text after the table.
        'oTable.Range.InsertParagraphAfter
        Set oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks("\endofdoc").Range)
        oPara4.Range.InsertParagraphBefore
        oPara4.Range.Text = "And here's another table:"
        oPara4.Format.SpaceAfter = 24
        oPara4.Range.InsertParagraphAfter
        
        'Insert a 5 x 2 table, fill it with data and change the column widths.
        Set oTable = oDoc.Tables.Add(oDoc.Bookmarks("\endofdoc").Range, 5, 2)
        oTable.Range.ParagraphFormat.SpaceAfter = 6
        For r = 1 To 5
            For c = 1 To 2
                oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
            Next
        Next
        oTable.Columns(1).Width = oWord.InchesToPoints(2)   'Change width of columns 1 & 2.
        oTable.Columns(2).Width = oWord.InchesToPoints(3)
        
        'Keep inserting text. When you get to 7 inches from top of the
        'document, insert a hard page break.
        Pos = oWord.InchesToPoints(7)
        oDoc.Bookmarks("\endofdoc").Range.InsertParagraphAfter
        Do
            Set oRng = oDoc.Bookmarks("\endofdoc").Range
            oRng.ParagraphFormat.SpaceAfter = 6
            oRng.InsertAfter "A line of text"
            oRng.InsertParagraphAfter
        Loop While Pos >= oRng.Information(wdVerticalPositionRelativeToPage)
        oRng.Collapse (wdCollapseEnd)
        oRng.InsertBreak wdPageBreak
        oRng.Collapse wdCollapseEnd
        oRng.InsertAfter "We're now on page 2. Here's my chart:"
        oRng.InsertParagraphAfter
        
        'Insert a chart and change the chart.
        Set oShape = oDoc.Bookmarks("\endofdoc").Range.InlineShapes.AddOLEObject( _
            ClassType:="MSGraph.Chart.8", FileName _
            :="", LinkToFile:=False, DisplayAsIcon:=False)
        Set oChart = oShape.OLEFormat.Object
        oChart.charttype = 4 'xlLine = 4
        oChart.Application.Update
        oChart.Application.Quit
        '... If desired, you can proceed from here using the Microsoft Graph 
        'Object model on the oChart object to make additional changes to the
        'chart.
        oShape.Width = oWord.InchesToPoints(6.25)
        oShape.Height = oWord.InchesToPoints(3.57)
        
        'Add text after the chart.
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        oRng.InsertParagraphAfter
        oRng.InsertAfter "THE END."
    
        'All done. Unload this form.
        Unload Me
    					
  5. Nh?n F5 ? ch?y chng tr?nh v sau nh?p vo Command1.
Sau khi m? hon thnh, ki?m tra cc ti li?u ? ?c t?o ra T?ng em. Ti li?u ch?a hai trang ?nh d?ng o?n, b?ng, v m?t b?ng x?p h?ng.

S? d?ng m?t m?u

N?u b?n ang s? d?ng t? ?ng ha ? xy d?ng ti li?u m l t?t c? trong m?t ?nh d?ng ph? bi?n, b?n c th? h?ng l?i t? b?t ?u qu tr?nh v?i m?t ti li?u m?i d?a trn m?t m?u preformatted. S? d?ng m?t m?u v?i l?i c?a b?n Khch hng t? ?ng ha c hai l?i th? ng k? trong vi?c xy d?ng m?t ti li?u t? khng c g?:
  • B?n c th? c ki?m sot l?n hn cc ?nh d?ng v v? tr c?a cc ?i t?ng trong ti li?u c?a b?n.
  • B?n c th? xy d?ng ti li?u c?a b?n v?i m? t hn.
B?ng cch s? d?ng m?t m?u, b?n c th? tinh ch?nh v? tr c?a b?ng, o?n vn, v cc ?i t?ng trong ti li?u, c?ng nh bao g?m ?nh d?ng trn cc ?i t?ng. B?ng cch s? d?ng t? ?ng ha, b?n c th? t?o m?t ti li?u m?i d?a trn m?u c?a b?n v?i m? nh sau:
oWord.Documents.Add "<Path to your template>\MyTemplate.dot"
				
Trong m?u c?a b?n, b?n c th? xc ?nh trang nh d?u ? t? ?ng ha c?a b?n khch hng c th? i?n vo vn b?n bi?n t?i m?t ?a i?m c? th? trong ti li?u, nh sau:
oDoc.Bookmarks("MyBookmark").Range.Text = "Some Text Here"
				
M?t l?i th? ? s? d?ng m?t m?u l b?n c th? t?o v c?t gi? ?nh d?ng phong cch m b?n mu?n p d?ng t?i th?i gian ch?y, nh sau:
oDoc.Bookmarks("MyBookmark").Range.Style = "MyStyle"
				
- hay -
oWord.Selection.Style = "MyStyle"
				

THAM KH?O

? bi?t thm chi ti?t, nh?p vo s? bi vi?t d?i y ? xem cc bi vi?t trong c s? ki?n th?c Microsoft:
220607 Lm th? no ? t? ?ng ho Microsoft Word th?c hi?n Mail Merge t? Visual Basic
285332 Lm th? no ? t? ?ng ho Word 2002 v?i Visual Basic ? t?o ra m?t Mail Merge
285333 Thng bo: Word 2002 MailMerge s? ki?n Code Demonstration
? bi?t thm chi ti?t, xem Microsoft sau y Cc trang Web c?a nh pht tri?n m?ng (MSDN):
Vn ph?ng Microsoft pht tri?n v?i Visual Studio
http://msdn2.Microsoft.com/en-US/Library/aa188489 (office.10) .aspx
(c) Microsoft Corporation 2001, t?t c? cc quy?n. Nh?ng ng gp c?a Lori B. Turner, Microsoft Corporation.

Thu?c tnh

ID c?a bi: 313193 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Office Word 2007
  • Microsoft Word 2002 Standard Edition
  • Microsoft Word 2000 Standard Edition
T? kha:
kbexpertiseinter kbautomation kbhowtomaster kbmt KB313193 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:313193

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