Lm th? no ? t? ?ng ho t? t? Visual Basic.NET ? t?o m?t ti li?u m?i

D?ch tiu ? D?ch tiu ?
ID c?a bi: 316383 - Xem s?n ph?m m bi ny p d?ng vo.
?i v?i m?t Microsoft Visual C#.NET Phin b?n c?a bi vi?t ny, xem 316384.
?i v?i m?t Microsoft Visual Basic 6,0 phin b?n c?a bi vi?t ny, xem 313193.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

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.NET.

Ma mu

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.NET, h?y lm theo cc b?c sau:
  1. B?t ?u Microsoft Visual Studio.NET. Trn cc Tp tr?nh n, nh?p vo M?i sau b?m D an. D?i Cc lo?i d? n Nh?p vo Visual Basic d? n, sau b?m ?ng d?ng Windows d?i Khun mu. Form1 ?c t?o ra theo m?c ?nh.
  2. Thm m?t tham chi?u ?n cc Microsoft Word ?i t?ng th vi?n. thc hin vic nay, hay lam theo nhng bc sau:
    1. Trn cc D an tr?nh n, nh?p vo Thm ti li?u tham kh?o.
    2. Trn cc COM tab, xc ?nh v? tr cc Microsoft Word ?i t?ng th vi?n v nh?p vo Ch?n.

      Chu y Microsoft Office 2003 v cc phin b?n sau c?a vn ph?ng bao g?m chnh Interop h?i ?ng (PIAs). Microsoft Office XP khng bao g?m PIAs, nhng h? c th? ?c t?i v?.? bi?t thm chi ti?t v? Office XP PIAs, nh?p vo s? bi vi?t sau y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
      328912Microsoft Office XP chnh interop h?i ?ng (PIAs) c s?n ? t?i v?
    3. Nh?p vo Ok trong cc Thm ti li?u tham kh?o h?p tho?i ? ch?p nh?n c?a b?n l?a ch?n. N?u b?n nh?n ?c m?t nh?c ?n t?o ra hm bao cho cc th vi?n m b?n ? ch?n, b?m vo Co.
  3. Trn cc Xem tr?nh n, ch?n H?p cng c? hi?n th? h?p cng c?, v sau thm m?t nt ? Form1.
  4. B?m p Button1. C?a s? m? cho cc h?nh th?c xu?t hi?n.
  5. Trong c?a s? m?, thay th? m? sau
        Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    
        End Sub
    v?i:
        Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    
            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.
            oWord = CreateObject("Word.Application")
            oWord.Visible = True
            oDoc = oWord.Documents.Add
    
            'Insert a paragraph at the beginning of the document.
            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.
            oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
            oPara2.Range.Text = "Heading 2"
            oPara2.Format.SpaceAfter = 6
            oPara2.Range.InsertParagraphAfter()
    
            'Insert another paragraph.
            oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\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 and italic.
            Dim r As Integer, c As Integer
            oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\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.Item(1).Range.Font.Bold = True
            oTable.Rows.Item(1).Range.Font.Italic = True
    
            'Add some text after the table.
            'oTable.Range.InsertParagraphAfter()
            oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\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.
            oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\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.Item(1).Width = oWord.InchesToPoints(2)   'Change width of columns 1 & 2
            oTable.Columns.Item(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.Item("\endofdoc").Range.InsertParagraphAfter()
            Do
                oRng = oDoc.Bookmarks.Item("\endofdoc").Range
                oRng.ParagraphFormat.SpaceAfter = 6
                oRng.InsertAfter("A line of text")
                oRng.InsertParagraphAfter()
            Loop While Pos >= oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
            oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
            oRng.InsertBreak(Word.WdBreakType.wdPageBreak)
            oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
            oRng.InsertAfter("We're now on page 2. Here's my chart:")
            oRng.InsertParagraphAfter()
    
            'Insert a chart and change the chart.
            oShape = oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( _
                ClassType:="MSGraph.Chart.8", FileName _
                :="", LinkToFile:=False, DisplayAsIcon:=False)
            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.
            oRng = oDoc.Bookmarks.Item("\endofdoc").Range
            oRng.InsertParagraphAfter()
            oRng.InsertAfter("THE END.")
    
            'All done. Close this form.
            Me.Close()
    
        End Sub
  6. Thm m? sau vo pha trn c?a Form1.vb:
    Imports Word = Microsoft.Office.Interop.Word
  7. Nh?n F5 ? xy d?ng v ch?y chng tr?nh.
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.Item("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.Item("MyBookmark").Range.Style = "MyStyle"
- hay -
oWord.Selection.Style = "MyStyle"

THAM KH?O

? bi?t thm chi ti?t v? vi?c s? d?ng Visual Basic.NET ? t? ?ng ha Microsoft Word, nh?p vo s? bi vi?t sau y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
301656Lm th? no ? t? ?ng t? ? th?c hi?n m?t mail merge t? Visual Basic.NET
? 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

T? trong vn ph?ng
http://msdn2.Microsoft.com/en-US/Library/aa201330 (office.11) .aspx

M?t b? t?
http://msdn2.Microsoft.com/en-US/Library/aa201332 (office.11) .aspx

Thu?c tnh

ID c?a bi: 316383 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Office Word 2007
  • Microsoft Word 2002 Standard Edition
  • Microsoft Word 2010
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic 2008 Express Edition
  • Microsoft Visual Basic 2010 Express
T? kha:
kbexpertiseinter kbautomation kbhowtomaster kbmt KB316383 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:316383

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